/// <summary> /// Parse a string field /// </summary> static bool ParseString(Action <object> add, TextVisiter input, FormatSpecifier spec) { // Skip any whitespace input.MovePastWhitespace(); // Parse string characters var start = input.Position; while (!input.EndOfText && !char.IsWhiteSpace(input.Peek())) { input.MoveAhead(); } // Don't exceed field width if (spec.Width > 0) { var count = input.Position - start; if (spec.Width < count) { input.MoveAhead(spec.Width - count); } } // Extract token if (input.Position > start) { if (!spec.NoResult) { add(input.Extract(start, input.Position)); } return(true); } return(false); }
/// <summary> /// Parse an octal field /// </summary> static bool ParseOctal(Action <object> add, TextVisiter input, FormatSpecifier spec) { // Skip any whitespace input.MovePastWhitespace(); // Parse digits var start = input.Position; while (IsValidDigit(input.Peek(), 8)) { input.MoveAhead(); } // Don't exceed field width if (spec.Width > 0) { var count = input.Position - start; if (spec.Width < count) { input.MoveAhead(spec.Width - count); } } // Extract token if (input.Position > start) { if (!spec.NoResult) { add(Unsigned(input.Extract(start, input.Position), spec.Modifier, 8)); } return(true); } return(false); }
/// <summary> /// Parse integer field /// </summary> static bool ParseDecimal(Action <object> add, TextVisiter input, FormatSpecifier spec) { // Skip any whitespace input.MovePastWhitespace(); // Parse leading sign var radix = 10; var start = input.Position; if (input.Peek() == '+' || input.Peek() == '-') { input.MoveAhead(); } else if (input.Peek() == '0') { if (char.ToLower(input.Peek(1)) == 'x') { radix = 16; input.MoveAhead(2); } else { radix = 8; input.MoveAhead(); } } // Parse digits while (IsValidDigit(input.Peek(), radix)) { input.MoveAhead(); } // Don't exceed field width if (spec.Width > 0) { var count = input.Position - start; if (spec.Width < count) { input.MoveAhead(spec.Width - count); } } // Extract token if (input.Position > start) { if (!spec.NoResult) { add(spec.Type == Types.Decimal ? Signed(input.Extract(start, input.Position), spec.Modifier, radix) : Unsigned(input.Extract(start, input.Position), spec.Modifier, radix)); } return(true); } return(false); }
/// <summary> /// 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. /// </summary> /// <param name="input">String to parse</param> /// <param name="format">Specifies rules for parsing input</param> public static int Parse(string input, string format, out IList <object> values) { var inp = new TextVisiter(input); var fmt = new TextVisiter(format); var results = new List <object>(); var spec = new FormatSpecifier(); var count = 0; // Process input string as indicated in format string while (!fmt.EndOfText && !inp.EndOfText) { if (ParseFormatSpecifier(fmt, spec)) { // Found a format specifier var parser = _typeParsers.First(tp => tp.Type == spec.Type); if (parser.Parser(results.Add, 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 values = results; return(count); }
/// <summary> /// Parse a floating-point field /// </summary> static bool ParseFloat(Action <object> add, TextVisiter input, FormatSpecifier spec) { // Skip any whitespace input.MovePastWhitespace(); // Parse leading sign var start = input.Position; if (input.Peek() == '+' || input.Peek() == '-') { input.MoveAhead(); } // Parse digits var hasPoint = false; while (char.IsDigit(input.Peek()) || input.Peek() == '.') { if (input.Peek() == '.') { if (hasPoint) { break; } hasPoint = true; } input.MoveAhead(); } // Parse exponential notation if (char.ToLower(input.Peek()) == 'e') { input.MoveAhead(); if (input.Peek() == '+' || input.Peek() == '-') { input.MoveAhead(); } while (char.IsDigit(input.Peek())) { input.MoveAhead(); } } // Don't exceed field width if (spec.Width > 0) { var count = input.Position - start; if (spec.Width < count) { input.MoveAhead(spec.Width - count); } } // Because we parse the exponential notation before we apply any field-width constraint, it becomes awkward to verify // we have a valid floating point token. To prevent an exception, we use TryParse() here instead of Parse(). // Extract token if (input.Position > start && double.TryParse(input.Extract(start, input.Position), out var result)) { if (!spec.NoResult) { add(spec.Modifier == Modifiers.Long || spec.Modifier == Modifiers.LongLong ? result : result); } return(true); } return(false); }