public static long parseLongBase10 ( final CharSequence str , final int start , final int end ) { // Current position and character. int pos = start ; char cur = str . charAt ( pos ) ; // Match sign boolean isNegative = ( cur == ' ' ) ; // Carefully consume the - character, update c and i: if ( ( isNegative || ( cur == ' ' ) ) && ( ++ pos < end ) ) { cur = str . charAt ( pos ) ; } // Begin parsing real numbers! if ( ( cur < ' ' ) || ( cur > ' ' ) ) { throw NOT_A_NUMBER ; } // Parse digits into a long, remember offset of decimal point. long decimal = 0 ; while ( true ) { final int digit = cur - ' ' ; if ( ( digit >= 0 ) && ( digit <= 9 ) ) { final long tmp = ( decimal << 3 ) + ( decimal << 1 ) + digit ; if ( tmp < decimal ) { throw PRECISION_OVERFLOW ; } decimal = tmp ; } else { // No more digits, or a second dot. break ; } if ( ++ pos < end ) { cur = str . charAt ( pos ) ; } else { break ; } } if ( pos != end ) { throw TRAILING_CHARACTERS ; } return isNegative ? - decimal : decimal ; }
Parse a long integer from a character sequence .