public static long parseLong(final CharSequence s) {
    final int n = s.length();  // Will NPE if necessary.
    if (n == 0) {
      throw new NumberFormatException("Empty string");
    }
    char c = s.charAt(0);  // Current character.
    int i = 1;  // index in `s'.
    if (c < '0' && (c == '+' || c == '-')) {  // Only 1 test in common case.
      if (n == 1) {
        throw new NumberFormatException("Just a sign, no value: " + s);
      } else if (n > 20) {  // "+9223372036854775807" or "-9223372036854775808"
          throw new NumberFormatException("Value too long: " + s);
      }
      c = s.charAt(1);
      i = 2;  // Skip over the sign.
    } else if (n > 19) {  // "9223372036854775807"
      throw new NumberFormatException("Value too long: " + s);
    }
    long v = 0;  // The result (negated to easily handle MIN_VALUE).
    do {
      if ('0' <= c && c <= '9') {
        v -= c - '0';
      } else {
        throw new NumberFormatException("Invalid character '" + c
                                        + "' in " + s);
      }
      if (i == n) {
        break;
      }
      v *= 10;
      c = s.charAt(i++);
    } while (true);
    if (v > 0) {
      throw new NumberFormatException("Overflow in " + s);
    } else if (s.charAt(0) == '-') {
      return v;  // Value is already negative, return unchanged.
    } else if (v == Long.MIN_VALUE) {
      throw new NumberFormatException("Overflow in " + s);
    } else {
      return -v;  // Positive value, need to fix the sign.
    }
  }
public static long parseLong ( final CharSequence s ) { final int n = s . length ( ) ; if ( n == 0 ) { throw new NumberFormatException ( "Empty string" ) ; } char c = s . charAt ( 0 ) ; int i = 1 ; if ( c < '0' && ( c == '+' || c == '-' ) ) { if ( n == 1 ) { throw new NumberFormatException ( "Just a sign, no value: " + s ) ; } else if ( n > 20 ) { throw new NumberFormatException ( "Value too long: " + s ) ; } c = s . charAt ( 1 ) ; i = 2 ; } else if ( n > 19 ) { throw new NumberFormatException ( "Value too long: " + s ) ; } long v = 0 ; do { if ( '0' <= c && c <= '9' ) { v -= c - '0' ; } else { throw new NumberFormatException ( "Invalid character '" + c + "' in " + s ) ; } if ( i == n ) { break ; } v *= 10 ; c = s . charAt ( i ++ ) ; } while ( true ) ; if ( v > 0 ) { throw new NumberFormatException ( "Overflow in " + s ) ; } else if ( s . charAt ( 0 ) == '-' ) { return v ; } else if ( v == Long . MIN_VALUE ) { throw new NumberFormatException ( "Overflow in " + s ) ; } else { return - v ; } }
Parses an integer value as a long from the given character sequence.
<p>
This is equivalent to {@link Long#parseLong(String)} except it's up to
100% faster on {@link String} and always works in O(1) space even with
{@link StringBuilder} buffers (where it's 2x to 5x faster).
@param s The character sequence containing the integer value to parse.
@return The value parsed.
@throws NumberFormatException if the value is malformed or overflows.