public boolean toInt ( IntWrapper intWrapper ) { if ( numBytes == 0 ) { return false ; } byte b = getByte ( 0 ) ; final boolean negative = b == ' ' ; int offset = 0 ; if ( negative || b == ' ' ) { offset ++ ; if ( numBytes == 1 ) { return false ; } } final byte separator = ' ' ; final int radix = 10 ; final int stopValue = Integer . MIN_VALUE / radix ; int result = 0 ; while ( offset < numBytes ) { b = getByte ( offset ) ; offset ++ ; if ( b == separator ) { // We allow decimals and will return a truncated integral in that case. // Therefore we won't throw an exception here (checking the fractional // part happens below.) break ; } int digit ; if ( b >= ' ' && b <= ' ' ) { digit = b - ' ' ; } else { return false ; } // We are going to process the new digit and accumulate the result. However, before doing // this, if the result is already smaller than the stopValue(Integer.MIN_VALUE / radix), then // result * 10 will definitely be smaller than minValue, and we can stop if ( result < stopValue ) { return false ; } result = result * radix - digit ; // Since the previous result is less than or equal to stopValue(Integer.MIN_VALUE / radix), // we can just use `result > 0` to check overflow. If result overflows, we should stop if ( result > 0 ) { return false ; } } // This is the case when we've encountered a decimal separator. The fractional // part will not change the number, but we will verify that the fractional part // is well formed. while ( offset < numBytes ) { byte currentByte = getByte ( offset ) ; if ( currentByte < ' ' || currentByte > ' ' ) { return false ; } offset ++ ; } if ( ! negative ) { result = - result ; if ( result < 0 ) { return false ; } } intWrapper . value = result ; return true ; }
Parses this UTF8String to int .