예제 #1
0
파일: CLibUtils.cs 프로젝트: soywiz/ilcc
        public static string sprintf_hl(string Format, params object[] Params)
        {
            var Out = "";
            var FormatReader = new StringReader(Format);
            var ParamsQueue = new Queue<object>(Params);
            while (FormatReader.HasMore())
            {
                var Char = FormatReader.ReadChar();
                switch (Char)
                {
                    case '%':
                        {
                            int LongCount = 0;
                            string LeftString = "";
                            string DecimalString = "";
                            int PaddingDirection = +1;
                            bool ReadingDecimalDigits = false;
                            string NumberOfIntegerDigitsString = "";
                            string NumberOfDecimalDigitsString = "";
                            char PaddingChar = ' ';
                            while (FormatReader.HasMore())
                            {
                                Char = FormatReader.ReadChar();
                                if (IsNumber(Char))
                                {
                                    if (ReadingDecimalDigits)
                                    {
                                        NumberOfDecimalDigitsString += Char;
                                    }
                                    else
                                    {
                                        if (NumberOfIntegerDigitsString.Length == 0 && Char == '0')
                                        {
                                            PaddingChar = Char;
                                        }
                                        else
                                        {
                                            NumberOfIntegerDigitsString += Char;
                                        }
                                    }

                                }
                                else if (Char == '-')
                                {
                                    PaddingDirection = -1;
                                }
                                else if (Char == '.')
                                {
                                    ReadingDecimalDigits = true;
                                }
                                else if (Char == 'l')
                                {
                                    LongCount++;
                                }
                                else if (IsAlpha(Char))
                                {
                                    switch (Char)
                                    {
                                        case 'c':
                                            {
                                                LeftString = "" + (char)(int)ParamsQueue.Dequeue();
                                            }
                                            goto EndFormat;
                                        case 'x':
                                        case 'X':
                                            {
                                                LeftString = Convert.ToString(Convert.ToInt64(ParamsQueue.Dequeue()), 16);
                                                if (Char == 'X')
                                                {
                                                    LeftString = LeftString.ToUpperInvariant();
                                                }
                                                else if (Char == 'x')
                                                {
                                                    LeftString = LeftString.ToLowerInvariant();
                                                }
                                            }
                                            goto EndFormat;
                                        case 'u':
                                            {
                                                LeftString = Convert.ToString(ToUInt32(ParamsQueue.Dequeue()), NeutralCultureInfo);
                                            }
                                            goto EndFormat;
                                        case 'd':
                                            {
                                                LeftString = Convert.ToString(ToInt32(ParamsQueue.Dequeue()), NeutralCultureInfo);
                                            }
                                            goto EndFormat;
                                        case 's':
                                            {
                                                LeftString = GetStringFromPointer((sbyte *)ConvertToIntPtr(ParamsQueue.Dequeue()).ToPointer());
                                            }
                                            goto EndFormat;
                                        case 'f':
                                            var Parts = Convert.ToString(ParamsQueue.Dequeue(), NeutralCultureInfo).Split('.');
                                            LeftString = Parts[0];
                                            if (Parts.Length > 1)
                                            {
                                                DecimalString = Parts[1];
                                            }
                                            goto EndFormat;
                                        default:
                                            throw (new NotImplementedException(String.Format("Unknown '{0}' in '{1}'", Char, Format)));
                                    }
                                }
                                else
                                {
                                    PaddingChar = Char;
                                }
                            }
                        EndFormat: ;

                            {
                                var NumberOfIntegerDigits = (NumberOfIntegerDigitsString.Length > 0) ? int.Parse(NumberOfIntegerDigitsString) : 0;
                                var NumberOfDecimalDigits = (NumberOfDecimalDigitsString.Length > 0) ? int.Parse(NumberOfDecimalDigitsString) : 0;

                                Out += LeftString;
                                if (NumberOfDecimalDigits > 0)
                                {
                                    if (DecimalString.Length > NumberOfDecimalDigits) DecimalString = DecimalString.Substring(0, NumberOfDecimalDigits);
                                    Out += ".";
                                    Out += DecimalString.PadRight(NumberOfDecimalDigits, '0');
                                }
                                else if (DecimalString.Length > 0)
                                {
                                    Out += ".";
                                    Out += DecimalString;
                                }
                                else
                                {
                                    if (PaddingDirection < 0)
                                    {
                                        Out = Out.PadRight(NumberOfIntegerDigits, PaddingChar);
                                    }
                                    else if (PaddingDirection > 0)
                                    {
                                        Out = Out.PadLeft(NumberOfIntegerDigits, PaddingChar);
                                    }
                                }
                            }
                        }
                        break;
                    default:
                        Out += Char;
                        break;
                }
            }
            return Out;
        }