/** * Parse a base 10 numeric from a sequence of ASCII digits into an int. * <para /> * Digit sequences can begin with an optional run of spaces before the * sequence, and may start with a '+' or a '-' to indicate sign position. * Any other characters will cause the method to stop and return the current * result to the caller. * * @param b * buffer to scan. * @param ptr * position within buffer to start parsing digits at. * @param ptrResult * optional location to return the new ptr value through. If null * the ptr value will be discarded. * @return the value at this location; 0 if the location is not a valid * numeric. */ internal static int parseBase10(byte[] b, int ptr, MutableInteger ptrResult) { int r = 0; int sign = 0; try { int sz = b.Length; while (ptr < sz && b[ptr] == ' ') ptr++; if (ptr >= sz) return 0; switch (b[ptr]) { case ((byte)'-'): sign = -1; ptr++; break; case ((byte)'+'): ptr++; break; } while (ptr < sz) { byte d = b[ptr]; if ((d < (byte)'0') || (d > (byte)'9')) break; r = r * 10 + (d - (byte)'0'); ptr++; } } catch (IndexOutOfRangeException) { // Not a valid digit. } if (ptrResult != null) ptrResult.value = ptr; return sign < 0 ? -r : r; }
/// <summary> /// Parse a base 10 numeric from a sequence of ASCII digits into a long. /// <para /> /// Digit sequences can begin with an optional run of spaces before the /// sequence, and may start with a '+' or a '-' to indicate sign position. /// Any other characters will cause the method to stop and return the current /// result to the caller. /// </summary> /// <param name="b">Buffer to scan.</param> /// <param name="ptr"> /// Position within buffer to start parsing digits at. /// </param> /// <param name="ptrResult"> /// Optional location to return the new ptr value through. If null /// the ptr value will be discarded. /// </param> /// <returns> /// The value at this location; 0 if the location is not a valid /// numeric. /// </returns> internal static long parseLongBase10(byte[] b, int ptr, MutableInteger ptrResult) { long r = 0; int sign = 0; try { int sz = b.Length; while (ptr < sz && b[ptr] == ' ') ptr++; if (ptr >= sz) return 0; switch (b[ptr]) { case (byte)'-': sign = -1; ptr++; break; case (byte)'+': ptr++; break; } while (ptr < sz) { int v = b[ptr] - (byte)'0'; if (v < 0) break; r = (r * 10) + v; ptr++; } } catch (IndexOutOfRangeException) { // Not a valid digit. } if (ptrResult != null) ptrResult.value = ptr; return sign < 0 ? -r : r; }
internal virtual void parseHeader() { // Parse "@@ -236,9 +236,9 @@ protected boolean" // byte[] buf = Buffer; var ptr = new MutableInteger { value = RawParseUtils.nextLF(buf, _startOffset, (byte)' ') }; OldStartLine = -1 * RawParseUtils.parseBase10(buf, ptr.value, ptr); OldLineCount = buf[ptr.value] == ',' ? RawParseUtils.parseBase10(buf, ptr.value + 1, ptr) : 1; NewStartLine = RawParseUtils.parseBase10(buf, ptr.value + 1, ptr); NewLineCount = buf[ptr.value] == ',' ? RawParseUtils.parseBase10(buf, ptr.value + 1, ptr) : 1; }