Пример #1
0
 public static TokenHandler <T> CreateParameterizedTokenHandler(ParameterizedFormatterFactory <T> formatterFactory)
 {
     return(delegate(string template, ref int currentIndex)
     {
         string parameter = string.Empty;
         if (template[currentIndex] == '(')
         {
             int parameterStart = ++currentIndex;
             while (true)
             {
                 while (currentIndex < template.Length && template[currentIndex++] != ')')
                 {
                     ;
                 }
                 if (currentIndex == template.Length)
                 {
                     return null;
                 }
                 if (template[currentIndex] == '}')
                 {
                     break;
                 }
             }
             parameter = template.Substring(parameterStart, currentIndex - parameterStart - 1);
         }
         currentIndex++;
         return formatterFactory(parameter);
     });
 }
Пример #2
0
        /// <summary>
        /// Utility method to create a handler for tokens with parameters surrounded by parenthesis.
        /// </summary>
        /// <param name="formatterFactory">The factory delegate to create a formatter based on the token parameter.</param>
        /// <returns>A token handler.</returns>
        public static TokenHandler <T> CreateParameterizedTokenHandler(ParameterizedFormatterFactory <T> formatterFactory)
        {
            return(delegate(string template, ref int currentIndex)
            {
                string parameter = string.Empty;

                if (template[currentIndex] == '(')
                {
                    int parameterStart = ++currentIndex;

                    while (true)
                    {
                        // skip until ending ')' is found
                        while (currentIndex < template.Length && template[currentIndex++] != ')')
                        {
                            ;
                        }

                        // template finished?
                        if (currentIndex == template.Length)
                        {
                            return null;
                        }

                        if (template[currentIndex] == '}')
                        {
                            break;                                      // got to the end of the template
                        }
                    }

                    // then the stopped looking after finding a ')'
                    parameter = template.Substring(parameterStart, currentIndex - parameterStart - 1);
                }

                // should be looking at the last char in the template
                currentIndex++;                 // advance the current index
                return formatterFactory(parameter);
            });
        }