/// <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> public 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); }
/** * 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. */ public 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> public 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; }
/** * 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. */ public 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; }