Returns the value of the specified string using the given radix. @param __v The String to decode. @param __r The radix to use. @throws NumberFormatException If the string is not valid or the radix is outside of the valid bounds. @since 2018/10/12
(String __v, int __r)
| 360 | * @since 2018/10/12 |
| 361 | */ |
| 362 | public static int parseInt(String __v, int __r) |
| 363 | throws NumberFormatException |
| 364 | { |
| 365 | // {@squirreljme.error ZZ0q The radix is out of bounds. (The radix)} |
| 366 | if (__r < Character.MIN_RADIX || __r > Character.MAX_RADIX) |
| 367 | throw new NumberFormatException("ZZ0q " + __r); |
| 368 | |
| 369 | // {@squirreljme.error ZZ0r String is null or has zero length.} |
| 370 | int n = __v.length(); |
| 371 | if (__v == null || n <= 0) |
| 372 | throw new NumberFormatException("ZZ0r"); |
| 373 | |
| 374 | // Detect sign |
| 375 | boolean neg = false, |
| 376 | signed = false; |
| 377 | char c = __v.charAt(0); |
| 378 | if ((neg = (c == '-')) || c == '+') |
| 379 | signed = true; |
| 380 | |
| 381 | // If the number is negative, instead of negating the value at the end |
| 382 | // just subtract digits instead. |
| 383 | int digsign = (neg ? -1 : 1); |
| 384 | |
| 385 | // Read all digits |
| 386 | int rv = 0; |
| 387 | for (int i = (signed ? 1 : 0); i < n; i++) |
| 388 | { |
| 389 | // Read character |
| 390 | c = __v.charAt(i); |
| 391 | |
| 392 | // Convert to digit |
| 393 | int dig = Character.digit(c, __r); |
| 394 | |
| 395 | // {@squirreljme.error ZZ0s Character out of range of radix. |
| 396 | // (The input string; The out of range character)} |
| 397 | if (dig < 0) |
| 398 | throw new NumberFormatException("ZZ0s " + __v + " " + c); |
| 399 | |
| 400 | // {@squirreljme.error ZZ0t Input integer out of range of 32-bit |
| 401 | // integer. (The input string)} |
| 402 | int prod = rv * __r; |
| 403 | if (rv != 0 && (neg ? (prod > rv) : (prod < rv))) |
| 404 | throw new NumberFormatException("ZZ0t " + __v); |
| 405 | |
| 406 | // Add up |
| 407 | rv = prod + (dig * digsign); |
| 408 | } |
| 409 | |
| 410 | return rv; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Returns the value of the specified string. |