/// <summary> /// Reads stream to match it against an amount, number or indefinite article. Sets any parsed value to usage.Amount /// </summary> /// <param name="stream"> /// </param> /// <param name="matchdata"> /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool Read(Stream stream, MatchData matchdata) { // We probably have to detect a potential numeric match up front and then parsewith Validate, // otherwise scan till first space and check if it's a number (do we support numbers with spaces?) var matchPos = stream.Position; var origPos = matchPos; int curByte; matchdata.Amount = null; var curBuffer = string.Empty; var invalidNumeric = new Regex(@"[^0-9 /.-]"); while ((curByte = stream.ReadByte()) >= 0) { float parsedAmtHigh; float? parsedAmtLow; curBuffer += (char)curByte; if (Validate(curBuffer, out parsedAmtLow, out parsedAmtHigh)) { matchPos = stream.Position; matchdata.Amount = new Amount { SizeLow = parsedAmtLow, SizeHigh = parsedAmtHigh }; } else { if (invalidNumeric.IsMatch(curBuffer)) { stream.Seek(origPos, SeekOrigin.Begin); var numericParser = new NumericToken(); if (numericParser.Read(stream, matchdata)) { matchPos = stream.Position; } break; } } } stream.Seek(matchPos, SeekOrigin.Begin); return matchdata.Amount != null; }
/// <summary> /// Reads stream to match it against an amount, number or indefinite article. Sets any parsed value to usage.Amount /// </summary> /// <param name="stream"></param> /// <param name="matchdata"></param> /// <returns></returns> public bool Read(Stream stream, MatchData matchdata) { //We probably have to detect a potential numeric match up front and then parsewith Validate, otherwise scan till first space and check if it's a number (do we support numbers with spaces?) var matchPosition = stream.Position; var originalPosition = matchPosition; int currentByte; matchdata.Amount = null; var curBuffer = String.Empty; var invalidNumeric = new Regex(@"[^0-9 /.-]"); while ((currentByte = stream.ReadByte()) >= 0) { float parsedAmtHigh; float?parsedAmtLow; curBuffer += (char)currentByte; if (Validate(curBuffer, out parsedAmtLow, out parsedAmtHigh)) //Try to parse buffer as a valid number or fraction { matchPosition = stream.Position; matchdata.Amount = new Amount(); matchdata.Amount.SizeLow = parsedAmtLow; matchdata.Amount.SizeHigh = parsedAmtHigh; } else //Stop processing as soon as we hit something that can no longer be an amount { if (invalidNumeric.IsMatch(curBuffer)) //Buffer has something in it other than 0-9 /. { //Hold on, let's try parsing this buffer using the NumericToken parser stream.Seek(originalPosition, SeekOrigin.Begin); var numericParser = new NumericToken(); if (numericParser.Read(stream, matchdata)) { matchPosition = stream.Position; } break; } } } stream.Seek(matchPosition, SeekOrigin.Begin); return(matchdata.Amount != null); //We found a valid amount here }
/// <summary> /// Reads stream to match it against an amount, number or indefinite article. Sets any parsed value to usage.Amount /// </summary> /// <param name="stream"></param> /// <param name="matchdata"></param> /// <returns></returns> public bool Read(Stream stream, MatchData matchdata) { //We probably have to detect a potential numeric match up front and then parsewith Validate, otherwise scan till first space and check if it's a number (do we support numbers with spaces?) var matchPosition = stream.Position; var originalPosition = matchPosition; int currentByte; matchdata.Amount = null; var curBuffer = String.Empty; var invalidNumeric = new Regex(@"[^0-9 /.-]"); while ((currentByte = stream.ReadByte()) >= 0) { float parsedAmtHigh; float? parsedAmtLow; curBuffer += (char) currentByte; if (Validate(curBuffer, out parsedAmtLow, out parsedAmtHigh)) //Try to parse buffer as a valid number or fraction { matchPosition = stream.Position; matchdata.Amount = new Amount(); matchdata.Amount.SizeLow = parsedAmtLow; matchdata.Amount.SizeHigh = parsedAmtHigh; } else //Stop processing as soon as we hit something that can no longer be an amount { if (invalidNumeric.IsMatch(curBuffer)) //Buffer has something in it other than 0-9 /. { //Hold on, let's try parsing this buffer using the NumericToken parser stream.Seek(originalPosition, SeekOrigin.Begin); var numericParser = new NumericToken(); if (numericParser.Read(stream, matchdata)) { matchPosition = stream.Position; } break; } } } stream.Seek(matchPosition, SeekOrigin.Begin); return (matchdata.Amount != null); //We found a valid amount here }