string TranslateFormatString()

in Source/Tx.Windows/EtwTdh/EtwTdhEventInfo.cs [398:453]


        string TranslateFormatString(string messageFormat)
        {
            if (messageFormat == null)
            {
                return null;
            }

            string format = messageFormat.Replace("%n", "\n");
            format = format.Replace("%t", "    ");

            StringBuilder sb = new StringBuilder();
            int startIndex = 0;
            while (startIndex < format.Length - 1)
            {
                int percentIndex = format.IndexOf('%', startIndex); // no more arguments

                SkipEscapedPercent:

                if (percentIndex < 0)
                {
                    string last = format.Substring(startIndex);
                    sb.Append(last);
                    break;
                }
                if (format[percentIndex + 1] == '%') // special case %% means % escaped
                {
                    percentIndex = format.IndexOf('%', percentIndex + 2);
                    goto SkipEscapedPercent;
                }

                string prefix = format.Substring(startIndex, percentIndex - startIndex);
                sb.Append(prefix);

                int beginNumberIndex = percentIndex + 1;
                int endNumberIndex = beginNumberIndex;
                while (endNumberIndex < format.Length)
                {
                    if (format[endNumberIndex] < '0' || format[endNumberIndex] > '9')
                    {
                        break;
                    }

                    endNumberIndex++;
                }

                string s = format.Substring(beginNumberIndex, endNumberIndex - beginNumberIndex);
                int index = int.Parse(s) - 1; // The C# convention is to start from 0 and teh % notation in the manifests starts from 1
                sb.Append('{');
                sb.Append(index);
                sb.Append('}');

                startIndex = endNumberIndex;
            }

            return sb.ToString();
        }