1

Topic: Bug in Second Language

I found a bug in the Second Language library version 1.0.0 also available via NuGet.

The bug is in SecondLanguage\SpecialFormatters.cs at the bottom of the file:

        /// <summary>
        /// Applies C printf style formatting, using a specific format provider.
        /// </summary>
        /// <param name="provider">The format provider to use, or <c>null</c> for the system default.</param>
        /// <param name="format">The format string.</param>
        /// <param name="args">Arguments to replace the format string's placeholders with.</param>
        /// <returns>The formatted translated string.</returns>
        public static string CustomC(IFormatProvider provider, string format, params object[] args)
        {
            Throw.If.Null(format, "format").Null(args, "args");

            int i = 0;
            Func<object> getArg = () =>
                {
                    object arg = i >= 0 && i < args.Length ? args[i] : null;
                    i++; return arg;
                };
            return CPrintfRegex.Replace(format, match =>
            {
                string parameter = match.Groups["parameter"].Value,
                    flags = match.Groups["flags"].Value,
                    widthString = match.Groups["width"].Value,
                    precisionString = match.Groups["precision"].Value,
                    length = match.Groups["length"].Value,
                    type = match.Groups["type"].Value;

                if (match.Value == "%%") { return "%"; }

                if (parameter != "") { i = int.Parse(parameter) - 1; }

                int width = 0;
                if (widthString != "") { width = widthString == "*" ? getArg() as int? ?? 0 : int.Parse(widthString); }

                int? precision = null;
                if (precisionString != "") { precision = precisionString == "*" ? getArg() as int? : int.Parse(precisionString); }

                object value = getArg() ?? "";
                string s = value.ToString() ?? "";

                if ("xX".Contains(type) && value is IFormattable)
                {
                    s = ((IFormattable)value).ToString(type, provider);
                }

                if (precision < 0) { precision = 0; }
                if ("fFeEgG".Contains(type) && value is IFormattable)
                {
                    s = ((IFormattable)value).ToString(type + precision.ToString(), provider);
                }
                else if (s.Length > precision)
                {
                    s = s.Substring(0, (int)precision);
                }

                int padCount = Math.Abs(width) - s.Length;
                var padChar = flags.Contains("0") ? '0' : ' ';
                if (padCount > 0)
                {
                    bool left = flags.Contains("-") ^ (width < 0);
                    var padding = new string(padChar, padCount);
                    if (left) { s += padding; } else { s = padding + s; }
                }

                i++; return s;
            });
        }
 

The above is the original code. The

i++;

before

return s;

has to be removed.

If you don't remove it, simple tests like the following won't work:

            Assert.AreEqual("100 200", printf("%s %s", 100, 200));

2

Re: Bug in Second Language

Thank you for reporting this bug.