Exemplo n.º 1
0
        /// <summary>
        /// <seealso cref="T:System.String.Format"/>.  This version takes a string builder and outputs to that instead of returning a new string.
        /// </summary>
        public static void Format(IFormatProvider provider, string format, IDictionary<string, object> context, MissingValueHandler handler, StringBuilder result)
        {
            if (format == null)
                throw new ArgumentNullException("format");

            // Nothing to see here.  Move along.
            if (format.IndexOf('{') < 0)
            {
                result.Append(format);
                return;
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            int ptr = 0;
            int start = ptr;
            while (ptr < format.Length)
            {
                char c = format[ptr++];

                if (c == '{')
                {
                    result.Append(format, start, ptr - start - 1);

                    // check for escaped open bracket or whitespace

                    if (format[ptr] == '{' || Char.IsWhiteSpace(format[ptr]))
                    {
                        start = ptr++;
                        continue;
                    }

                    // parse specifier

                    int width, formatStart = ptr;
                    bool left_align;
                    string arg_format, arg_name;

                    ParseFormatSpecifier(format, ref ptr, out arg_name, out width, out left_align, out arg_format);
                    object arg = null;
                    if (!context.ContainsKey(arg_name))
                    {
                        if (handler == null)
                        {
                            throw new FormatException(String.Format("The named argument {{{0}}} must exist in the context.", arg_name));
                        }
                        else
                        {
                            arg = handler(arg_name, format, formatStart, ptr);
                        }
                    }
                    else
                        arg = context[arg_name];

                    string str;
                    if (arg == null)
                        str = "";
                    else if (arg is IFormattable)
                        str = ((IFormattable)arg).ToString(arg_format, provider);
                    else
                        str = arg.ToString();

                    // pad formatted string and append to result
                    if (width > (str == null ? 0 : str.Length))
                    {
                        string pad = new String(' ', width - str.Length);

                        if (left_align)
                        {
                            result.Append(str);
                            result.Append(pad);
                        }
                        else
                        {
                            result.Append(pad);
                            result.Append(str);
                        }
                    }
                    else
                        result.Append(str);

                    start = ptr;
                }
                else if (c == '}' && ptr < format.Length && format[ptr] == '}')
                {
                    result.Append(format, start, ptr - start - 1);
                    start = ptr++;
                }
                else if (c == '}')
                {
                    throw new FormatException("Input string was not in a correct format.");
                }
            }

            if (start < format.Length)
                result.Append(format.Substring(start));
        }
Exemplo n.º 2
0
        /// <summary>
        /// <seealso cref="T:System.String.Format"/>
        /// </summary>
        public static string Format(IFormatProvider provider, string format, IDictionary<string, object> context, MissingValueHandler handler)
        {
            if (format == null)
                throw new ArgumentNullException("format");

            // Nothing to see here.  Move along.
            if (format.IndexOf('{') < 0)
                return format;

            StringBuilder result = new StringBuilder(format.Length);
            Format(provider, format, context, handler, result);
            return result.ToString();
        }