CNCGUI.ScanFormatted.Parse C# (CSharp) Method

Parse() public method

Parses the input string according to the rules in the format string. Similar to the standard C library's sscanf() function. Parsed fields are placed in the class' Results member.
public Parse ( string input, string format ) : int
input string String to parse
format string Specifies rules for parsing input
return int
        public int Parse(string input, string format)
        {
            TextParser inp = new TextParser(input);
            TextParser fmt = new TextParser(format);
            List<object> results = new List<object>();
            FormatSpecifier spec = new FormatSpecifier();
            int count = 0;

            // Clear any previous results
            Results.Clear();

            // Process input string as indicated in format string
            while (!fmt.EndOfText && !inp.EndOfText)
            {
                if (ParseFormatSpecifier(fmt, spec))
                {
                    // Found a format specifier
                    TypeParser parser = null;
                    foreach(TypeParser tp in _typeParsers)
                        if (tp.Type == spec.Type)
                        {
                            parser = tp;
                            break;
                        }
                    if (parser == null)
                        break;

                    if (parser.Parser(inp, spec))
                        count++;
                    else
                        break;
                }
                else if (Char.IsWhiteSpace(fmt.Peek()))
                {
                    // Whitespace
                    inp.MovePastWhitespace();
                    fmt.MoveAhead();
                }
                else if (fmt.Peek() == inp.Peek())
                {
                    // Matching character
                    inp.MoveAhead();
                    fmt.MoveAhead();
                }
                else break;    // Break at mismatch
            }

            // Return number of fields successfully parsed
            return count;
        }