| 158 | } |
| 159 | |
| 160 | long parseLong(String s) throws NumberFormatException { |
| 161 | int radix; |
| 162 | int i; |
| 163 | if( s.startsWith("0x") || s.startsWith("0X") ) { |
| 164 | radix = 16; |
| 165 | i = 2; |
| 166 | } else if( s.startsWith("0") && s.length() > 1 ) { |
| 167 | radix = 8; |
| 168 | i = 1; |
| 169 | } else { |
| 170 | radix = 10; |
| 171 | i = 0; |
| 172 | } |
| 173 | long result = 0; |
| 174 | int len = s.length(); |
| 175 | for( ; i<len; i++ ) { |
| 176 | if( result < 0L ) |
| 177 | throw new NumberFormatException("Number too big for long type: "+s); |
| 178 | result *= radix; |
| 179 | int digit = Character.digit(s.charAt(i),radix); |
| 180 | if( digit < 0 ) |
| 181 | throw new NumberFormatException("Invalid long type: "+s); |
| 182 | result += digit; |
| 183 | } |
| 184 | return result; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | Thanks to Sreenivasa Viswanadha for suggesting how to get rid of expensive |