src/share/classes/java/lang/Long.java

Print this page
rev 9892 : imported patch format.diff
rev 9893 : 8041972: Add improved parse methods for Long/Integer


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Native;
  29 import java.math.*;

  30 
  31 
  32 /**
  33  * The {@code Long} class wraps a value of the primitive type {@code
  34  * long} in an object. An object of type {@code Long} contains a
  35  * single field whose type is {@code long}.
  36  *
  37  * <p> In addition, this class provides several methods for converting
  38  * a {@code long} to a {@code String} and a {@code String} to a {@code
  39  * long}, as well as other constants and methods useful when dealing
  40  * with a {@code long}.
  41  *
  42  * <p>Implementation note: The implementations of the "bit twiddling"
  43  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  44  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  45  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  46  * Delight</i>, (Addison Wesley, 2002).
  47  *
  48  * @author  Lee Boynton
  49  * @author  Arthur van Hoff


 547      *             the specified radix.
 548      * @throws     NumberFormatException  if the string does not contain a
 549      *             parsable {@code long}.
 550      */
 551     public static long parseLong(String s, int radix)
 552               throws NumberFormatException
 553     {
 554         if (s == null) {
 555             throw new NumberFormatException("null");
 556         }
 557 
 558         if (radix < Character.MIN_RADIX) {
 559             throw new NumberFormatException("radix " + radix +
 560                                             " less than Character.MIN_RADIX");
 561         }
 562         if (radix > Character.MAX_RADIX) {
 563             throw new NumberFormatException("radix " + radix +
 564                                             " greater than Character.MAX_RADIX");
 565         }
 566 
 567         long result = 0;
 568         boolean negative = false;
 569         int i = 0, len = s.length();
 570         long limit = -Long.MAX_VALUE;
 571         long multmin;
 572         int digit;
 573 
 574         if (len > 0) {
 575             char firstChar = s.charAt(0);
 576             if (firstChar < '0') { // Possible leading "+" or "-"
 577                 if (firstChar == '-') {
 578                     negative = true;
 579                     limit = Long.MIN_VALUE;
 580                 } else if (firstChar != '+')
 581                     throw NumberFormatException.forInputString(s);

 582 
 583                 if (len == 1) // Cannot have lone "+" or "-"
 584                     throw NumberFormatException.forInputString(s);

 585                 i++;
 586             }
 587             multmin = limit / radix;

 588             while (i < len) {
 589                 // Accumulating negatively avoids surprises near MAX_VALUE
 590                 digit = Character.digit(s.charAt(i++),radix);
 591                 if (digit < 0) {
 592                     throw NumberFormatException.forInputString(s);
 593                 }
 594                 if (result < multmin) {
 595                     throw NumberFormatException.forInputString(s);
 596                 }
 597                 result *= radix;
 598                 if (result < limit + digit) {
 599                     throw NumberFormatException.forInputString(s);
 600                 }
 601                 result -= digit;
 602             }

 603         } else {
 604             throw NumberFormatException.forInputString(s);
 605         }
















































































































 606         return negative ? result : -result;



 607     }
 608 
 609     /**
 610      * Parses the string argument as a signed decimal {@code long}.
 611      * The characters in the string must all be decimal digits, except
 612      * that the first character may be an ASCII minus sign {@code '-'}
 613      * ({@code \u005Cu002D'}) to indicate a negative value or an
 614      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
 615      * indicate a positive value. The resulting {@code long} value is
 616      * returned, exactly as if the argument and the radix {@code 10}
 617      * were given as arguments to the {@link
 618      * #parseLong(java.lang.String, int)} method.
 619      *
 620      * <p>Note that neither the character {@code L}
 621      * ({@code '\u005Cu004C'}) nor {@code l}
 622      * ({@code '\u005Cu006C'}) is permitted to appear at the end
 623      * of the string as a type indicator, as would be permitted in
 624      * Java programming language source code.
 625      *
 626      * @param      s   a {@code String} containing the {@code long}


 680     public static long parseUnsignedLong(String s, int radix)
 681                 throws NumberFormatException {
 682         if (s == null)  {
 683             throw new NumberFormatException("null");
 684         }
 685 
 686         int len = s.length();
 687         if (len > 0) {
 688             char firstChar = s.charAt(0);
 689             if (firstChar == '-') {
 690                 throw new
 691                     NumberFormatException(String.format("Illegal leading minus sign " +
 692                                                        "on unsigned string %s.", s));
 693             } else {
 694                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
 695                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
 696                     return parseLong(s, radix);
 697                 }
 698 
 699                 // No need for range checks on len due to testing above.
 700                 long first = parseLong(s.substring(0, len - 1), radix);
 701                 int second = Character.digit(s.charAt(len - 1), radix);
 702                 if (second < 0) {
 703                     throw new NumberFormatException("Bad digit at end of " + s);
 704                 }
 705                 long result = first * radix + second;
 706                 if (compareUnsigned(result, first) < 0) {
 707                     /*
 708                      * The maximum unsigned value, (2^64)-1, takes at
 709                      * most one more digit to represent than the
 710                      * maximum signed value, (2^63)-1.  Therefore,
 711                      * parsing (len - 1) digits will be appropriately
 712                      * in-range of the signed parsing.  In other
 713                      * words, if parsing (len -1) digits overflows
 714                      * signed parsing, parsing len digits will
 715                      * certainly overflow unsigned parsing.
 716                      *
 717                      * The compareUnsigned check above catches
 718                      * situations where an unsigned overflow occurs
 719                      * incorporating the contribution of the final
 720                      * digit.
 721                      */
 722                     throw new NumberFormatException(String.format("String value %s exceeds " +
 723                                                                   "range of unsigned long.", s));
 724                 }
 725                 return result;
 726             }
 727         } else {
 728             throw NumberFormatException.forInputString(s);





















































































































































 729         }
 730     }
 731 
 732     /**
 733      * Parses the string argument as an unsigned decimal {@code long}. The
 734      * characters in the string must all be decimal digits, except
 735      * that the first character may be an an ASCII plus sign {@code
 736      * '+'} ({@code '\u005Cu002B'}). The resulting integer value
 737      * is returned, exactly as if the argument and the radix 10 were
 738      * given as arguments to the {@link
 739      * #parseUnsignedLong(java.lang.String, int)} method.
 740      *
 741      * @param s   a {@code String} containing the unsigned {@code long}
 742      *            representation to be parsed
 743      * @return    the unsigned {@code long} value represented by the decimal string argument
 744      * @throws    NumberFormatException  if the string does not contain a
 745      *            parsable unsigned integer.
 746      * @since 1.8
 747      */
 748     public static long parseUnsignedLong(String s) throws NumberFormatException {




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Native;
  29 import java.math.*;
  30 import java.util.Objects;
  31 
  32 
  33 /**
  34  * The {@code Long} class wraps a value of the primitive type {@code
  35  * long} in an object. An object of type {@code Long} contains a
  36  * single field whose type is {@code long}.
  37  *
  38  * <p> In addition, this class provides several methods for converting
  39  * a {@code long} to a {@code String} and a {@code String} to a {@code
  40  * long}, as well as other constants and methods useful when dealing
  41  * with a {@code long}.
  42  *
  43  * <p>Implementation note: The implementations of the "bit twiddling"
  44  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  45  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  46  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  47  * Delight</i>, (Addison Wesley, 2002).
  48  *
  49  * @author  Lee Boynton
  50  * @author  Arthur van Hoff


 548      *             the specified radix.
 549      * @throws     NumberFormatException  if the string does not contain a
 550      *             parsable {@code long}.
 551      */
 552     public static long parseLong(String s, int radix)
 553               throws NumberFormatException
 554     {
 555         if (s == null) {
 556             throw new NumberFormatException("null");
 557         }
 558 
 559         if (radix < Character.MIN_RADIX) {
 560             throw new NumberFormatException("radix " + radix +
 561                                             " less than Character.MIN_RADIX");
 562         }
 563         if (radix > Character.MAX_RADIX) {
 564             throw new NumberFormatException("radix " + radix +
 565                                             " greater than Character.MAX_RADIX");
 566         }
 567 

 568         boolean negative = false;
 569         int i = 0, len = s.length();
 570         long limit = -Long.MAX_VALUE;


 571 
 572         if (len > 0) {
 573             char firstChar = s.charAt(0);
 574             if (firstChar < '0') { // Possible leading "+" or "-"
 575                 if (firstChar == '-') {
 576                     negative = true;
 577                     limit = Long.MIN_VALUE;
 578                 } else if (firstChar != '+') {
 579                     throw NumberFormatException.forInputString(s);
 580                 }
 581 
 582                 if (len == 1) { // Cannot have lone "+" or "-"
 583                     throw NumberFormatException.forInputString(s);
 584                 }
 585                 i++;
 586             }
 587             long multmin = limit / radix;
 588             long result = 0;
 589             while (i < len) {
 590                 // Accumulating negatively avoids surprises near MAX_VALUE
 591                 int digit = Character.digit(s.charAt(i++),radix);
 592                 if (digit < 0 || result < multmin) {



 593                     throw NumberFormatException.forInputString(s);
 594                 }
 595                 result *= radix;
 596                 if (result < limit + digit) {
 597                     throw NumberFormatException.forInputString(s);
 598                 }
 599                 result -= digit;
 600             }
 601             return negative ? result : -result;
 602         } else {
 603             throw NumberFormatException.forInputString(s);
 604         }
 605     }
 606 
 607     /**
 608      * Parses the {@link CharSequence} argument as a signed {@code long} in
 609      * the specified {@code radix}, beginning at the specified {@code beginIndex}
 610      * and extending to the end of the sequence.
 611      *
 612      * <p>The method does not take steps to guard against the
 613      * {@code CharSequence} being mutated while parsing.
 614      *
 615      * @param      s   the {@code CharSequence} containing the {@code long}
 616      *                  representation to be parsed
 617      * @param      radix   the radix to be used while parsing {@code s}.
 618      * @param      beginIndex   the beginning index, inclusive.
 619      * @return     the signed {@code long} represented by the subsequence in
 620      *             the specified radix.
 621      * @throws     NullPointerException  if {@code s} is null.
 622      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 623      *             negative, or if {@code beginIndex} is greater than
 624      *             {@code s.length()}.
 625      * @throws     NumberFormatException  if the {@code CharSequence} does not
 626      *             contain a parsable {@code long} in the specified
 627      *             {@code radix}, or if {@code radix} is either smaller than
 628      *             {@link java.lang.Character#MIN_RADIX} or larger than
 629      *             {@link java.lang.Character#MAX_RADIX}.
 630      * @since  1.9
 631      */
 632     static long parseLong(CharSequence s, int radix, int beginIndex)
 633             throws NumberFormatException {
 634         // forces a null check of s
 635         return parseLong(s, radix, beginIndex, s.length());
 636     }
 637 
 638     /**
 639      * Parses the {@link CharSequence} argument as a signed {@code long} in
 640      * the specified {@code radix}, beginning at the specified
 641      * {@code beginIndex} and extending to {@code endIndex - 1}.
 642      *
 643      * <p>The method does not take steps to guard against the
 644      * {@code CharSequence} being mutated while parsing.
 645      *
 646      * @param      s   the {@code CharSequence} containing the {@code long}
 647      *                  representation to be parsed
 648      * @param      radix   the radix to be used while parsing {@code s}.
 649      * @param      beginIndex   the beginning index, inclusive.
 650      * @param      endIndex     the ending index, exclusive.
 651      * @return     the signed {@code long} represented by the subsequence in
 652      *             the specified radix.
 653      * @throws     NullPointerException  if {@code s} is null.
 654      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 655      *             negative, or if {@code beginIndex} is greater than
 656      *             {@code endIndex} or if {@code endIndex} is greater than
 657      *             {@code s.length()}.
 658      * @throws     NumberFormatException  if the {@code CharSequence} does not
 659      *             contain a parsable {@code int} in the specified
 660      *             {@code radix}, or if {@code radix} is either smaller than
 661      *             {@link java.lang.Character#MIN_RADIX} or larger than
 662      *             {@link java.lang.Character#MAX_RADIX}.
 663      * @since  1.9
 664      */
 665     static long parseLong(CharSequence s, int radix, int beginIndex, int endIndex)
 666                 throws NumberFormatException {
 667         s = Objects.requireNonNull(s);
 668 
 669         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 670             throw new IndexOutOfBoundsException();
 671         }
 672         if (radix < Character.MIN_RADIX) {
 673             throw new NumberFormatException("radix " + radix +
 674                     " less than Character.MIN_RADIX");
 675         }
 676         if (radix > Character.MAX_RADIX) {
 677             throw new NumberFormatException("radix " + radix +
 678                     " greater than Character.MAX_RADIX");
 679         }
 680 
 681         boolean negative = false;
 682         int i = beginIndex;
 683         long limit = -Long.MAX_VALUE;
 684 
 685         if (i < endIndex) {
 686             char firstChar = s.charAt(i);
 687             if (firstChar < '0') { // Possible leading "+" or "-"
 688                 if (firstChar == '-') {
 689                     negative = true;
 690                     limit = Long.MIN_VALUE;
 691                 } else if (firstChar != '+') {
 692                     throw NumberFormatException.forCharSequence(s, beginIndex,
 693                             endIndex, i);
 694                 }
 695                 i++;
 696             }
 697             if (i >= endIndex) { // Cannot have lone "+", "-" or ""
 698                 throw NumberFormatException.forCharSequence(s, beginIndex,
 699                         endIndex, i);
 700             }
 701             long multmin = limit / radix;
 702             long result = 0;
 703             while (i < endIndex) {
 704                 // Accumulating negatively avoids surprises near MAX_VALUE
 705                 int digit = Character.digit(s.charAt(i++), radix);
 706                 if (digit < 0 || result < multmin) {
 707                     throw NumberFormatException.forCharSequence(s, beginIndex,
 708                             endIndex, i);
 709                 }
 710                 result *= radix;
 711                 if (result < limit + digit) {
 712                     throw NumberFormatException.forCharSequence(s, beginIndex,
 713                             endIndex, i);
 714                 }
 715                 result -= digit;
 716             }
 717             return negative ? result : -result;
 718         } else {
 719             throw new NumberFormatException("");
 720         }
 721     }
 722 
 723     /**
 724      * Parses the string argument as a signed decimal {@code long}.
 725      * The characters in the string must all be decimal digits, except
 726      * that the first character may be an ASCII minus sign {@code '-'}
 727      * ({@code \u005Cu002D'}) to indicate a negative value or an
 728      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
 729      * indicate a positive value. The resulting {@code long} value is
 730      * returned, exactly as if the argument and the radix {@code 10}
 731      * were given as arguments to the {@link
 732      * #parseLong(java.lang.String, int)} method.
 733      *
 734      * <p>Note that neither the character {@code L}
 735      * ({@code '\u005Cu004C'}) nor {@code l}
 736      * ({@code '\u005Cu006C'}) is permitted to appear at the end
 737      * of the string as a type indicator, as would be permitted in
 738      * Java programming language source code.
 739      *
 740      * @param      s   a {@code String} containing the {@code long}


 794     public static long parseUnsignedLong(String s, int radix)
 795                 throws NumberFormatException {
 796         if (s == null)  {
 797             throw new NumberFormatException("null");
 798         }
 799 
 800         int len = s.length();
 801         if (len > 0) {
 802             char firstChar = s.charAt(0);
 803             if (firstChar == '-') {
 804                 throw new
 805                     NumberFormatException(String.format("Illegal leading minus sign " +
 806                                                        "on unsigned string %s.", s));
 807             } else {
 808                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
 809                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
 810                     return parseLong(s, radix);
 811                 }
 812 
 813                 // No need for range checks on len due to testing above.
 814                 long first = parseLong(s, radix, 0, len - 1);
 815                 int second = Character.digit(s.charAt(len - 1), radix);
 816                 if (second < 0) {
 817                     throw new NumberFormatException("Bad digit at end of " + s);
 818                 }
 819                 long result = first * radix + second;
 820                 if (compareUnsigned(result, first) < 0) {
 821                     /*
 822                      * The maximum unsigned value, (2^64)-1, takes at
 823                      * most one more digit to represent than the
 824                      * maximum signed value, (2^63)-1.  Therefore,
 825                      * parsing (len - 1) digits will be appropriately
 826                      * in-range of the signed parsing.  In other
 827                      * words, if parsing (len -1) digits overflows
 828                      * signed parsing, parsing len digits will
 829                      * certainly overflow unsigned parsing.
 830                      *
 831                      * The compareUnsigned check above catches
 832                      * situations where an unsigned overflow occurs
 833                      * incorporating the contribution of the final
 834                      * digit.
 835                      */
 836                     throw new NumberFormatException(String.format("String value %s exceeds " +
 837                                                                   "range of unsigned long.", s));
 838                 }
 839                 return result;
 840             }
 841         } else {
 842             throw NumberFormatException.forInputString(s);
 843         }
 844     }
 845 
 846     /**
 847      * Parses the {@link CharSequence} argument as an unsigned {@code long} in
 848      * the specified {@code radix}, beginning at the specified
 849      * {@code beginIndex} and extending to the end of the sequence.
 850      *
 851      * <p>The method does not take steps to guard against the
 852      * {@code CharSequence} being mutated while parsing.
 853      *
 854      * @param      s   the {@code CharSequence} containing the unsigned
 855      *                 {@code long} representation to be parsed
 856      * @param      radix   the radix to be used while parsing {@code s}.
 857      * @param      beginIndex   the beginning index, inclusive.
 858      * @return     the unsigned {@code long} represented by the subsequence in
 859      *             the specified radix.
 860      * @throws     NullPointerException  if {@code s} is null.
 861      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 862      *             negative, or if {@code beginIndex} is greater than
 863      *             {@code s.length()}.
 864      * @throws     NumberFormatException  if the {@code CharSequence} does not
 865      *             contain a parsable unsigned {@code long} in the specified
 866      *             {@code radix}, or if {@code radix} is either smaller than
 867      *             {@link java.lang.Character#MIN_RADIX} or larger than
 868      *             {@link java.lang.Character#MAX_RADIX}.
 869      * @since  1.9
 870      */
 871     static long parseUnsignedLong(CharSequence s, int radix, int beginIndex)
 872                 throws NumberFormatException {
 873         // forces a null check of s
 874         return parseUnsignedLong(s, radix, beginIndex, s.length());
 875     }
 876 
 877     /**
 878      * Parses the {@link CharSequence} argument as an unsigned {@code long} in
 879      * the specified {@code radix}, beginning at the specified
 880      * {@code beginIndex} and extending to {@code endIndex - 1}.
 881      *
 882      * <p>The method does not take steps to guard against the
 883      * {@code CharSequence} being mutated while parsing.
 884      *
 885      * @param      s   the {@code CharSequence} containing the unsigned
 886      *                 {@code long} representation to be parsed
 887      * @param      radix   the radix to be used while parsing {@code s}.
 888      * @param      beginIndex   the beginning index, inclusive.
 889      * @param      endIndex     the ending index, exclusive.
 890      * @return     the unsigned {@code long} represented by the subsequence in
 891      *             the specified radix.
 892      * @throws     NullPointerException  if {@code s} is null.
 893      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 894      *             negative, or if {@code beginIndex} is greater than
 895      *             {@code endIndex} or if {@code endIndex} is greater than
 896      *             {@code s.length()}.
 897      * @throws     NumberFormatException  if the {@code CharSequence} does not
 898      *             contain a parsable unsigned {@code long} in the specified
 899      *             {@code radix}, or if {@code radix} is either smaller than
 900      *             {@link java.lang.Character#MIN_RADIX} or larger than
 901      *             {@link java.lang.Character#MAX_RADIX}.
 902      * @since  1.9
 903      */
 904     static long parseUnsignedLong(CharSequence s, int radix, int beginIndex, int endIndex)
 905                 throws NumberFormatException {
 906         s = Objects.requireNonNull(s);
 907 
 908         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 909             throw new IndexOutOfBoundsException();
 910         }
 911         int start = beginIndex, len = endIndex - beginIndex;
 912 
 913         if (len > 0) {
 914             char firstChar = s.charAt(start);
 915             if (firstChar == '-') {
 916                 throw new NumberFormatException(String.format("Illegal leading minus sign " +
 917                         "on unsigned string %s.", s.subSequence(start, start + len)));
 918             } else {
 919                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
 920                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
 921                     return parseLong(s, radix, start, start + len);
 922                 }
 923 
 924                 // No need for range checks on end due to testing above.
 925                 long first = parseLong(s, radix, start, start + len - 1);
 926                 int second = Character.digit(s.charAt(start + len - 1), radix);
 927                 if (second < 0) {
 928                     throw new NumberFormatException("Bad digit at end of " +
 929                             s.subSequence(start, start + len));
 930                 }
 931                 long result = first * radix + second;
 932 
 933                 /*
 934                  * Test leftmost bits of multiprecision extension of first*radix
 935                  * for overflow. The number of bits needed is defined by
 936                  * GUARD_BIT = ceil(log2(Character.MAX_RADIX)) + 1 = 7. Then
 937                  * int guard = radix*(int)(first >>> (64 - GUARD_BIT)) and
 938                  * overflow is tested by splitting guard in the ranges
 939                  * guard < 92, 92 <= guard < 128, and 128 <= guard, where
 940                  * 92 = 128 - Character.MAX_RADIX. Note that guard cannot take
 941                  * on a value which does not include a prime factor in the legal
 942                  * radix range.
 943                  */
 944                 int guard = radix * (int) (first >>> 57);
 945                 if (guard >= 128 ||
 946                         (result >= 0 && guard >= 128 - Character.MAX_RADIX)) {
 947                     /*
 948                      * For purposes of exposition, the programmatic statements
 949                      * below should be taken to be multi-precision, i.e., not
 950                      * subject to overflow.
 951                      *
 952                      * A) Condition guard >= 128:
 953                      * If guard >= 128 then first*radix >= 2^7 * 2^57 = 2^64
 954                      * hence always overflow.
 955                      *
 956                      * B) Condition guard < 92:
 957                      * Define left7 = first >>> 57.
 958                      * Given first = (left7 * 2^57) + (first & (2^57 - 1)) then
 959                      * result <= (radix*left7)*2^57 + radix*(2^57 - 1) + second.
 960                      * Thus if radix*left7 < 92, radix <= 36, and second < 36,
 961                      * then result < 92*2^57 + 36*(2^57 - 1) + 36 = 2^64 hence
 962                      * never overflow.
 963                      *
 964                      * C) Condition 92 <= guard < 128:
 965                      * first*radix + second >= radix*left7*2^57 + second
 966                      * so that first*radix + second >= 92*2^57 + 0 > 2^63
 967                      *
 968                      * D) Condition guard < 128:
 969                      * radix*first <= (radix*left7) * 2^57 + radix*(2^57 - 1)
 970                      * so
 971                      * radix*first + second <= (radix*left7) * 2^57 + radix*(2^57 - 1) + 36
 972                      * thus
 973                      * radix*first + second < 128 * 2^57 + 36*2^57 - radix + 36
 974                      * whence
 975                      * radix*first + second < 2^64 + 2^6*2^57 = 2^64 + 2^63
 976                      *
 977                      * E) Conditions C, D, and result >= 0:
 978                      * C and D combined imply the mathematical result
 979                      * 2^63 < first*radix + second < 2^64 + 2^63. The lower
 980                      * bound is therefore negative as a signed long, but the
 981                      * upper bound is too small to overflow again after the
 982                      * signed long overflows to positive above 2^64 - 1. Hence
 983                      * result >= 0 implies overflow given C and D.
 984                      */
 985                     throw new NumberFormatException(String.format("String value %s exceeds " +
 986                             "range of unsigned long.", s.subSequence(start, start + len)));
 987                 }
 988                 return result;
 989             }
 990         } else {
 991             throw NumberFormatException.forInputString("");
 992         }
 993     }
 994 
 995     /**
 996      * Parses the string argument as an unsigned decimal {@code long}. The
 997      * characters in the string must all be decimal digits, except
 998      * that the first character may be an an ASCII plus sign {@code
 999      * '+'} ({@code '\u005Cu002B'}). The resulting integer value
1000      * is returned, exactly as if the argument and the radix 10 were
1001      * given as arguments to the {@link
1002      * #parseUnsignedLong(java.lang.String, int)} method.
1003      *
1004      * @param s   a {@code String} containing the unsigned {@code long}
1005      *            representation to be parsed
1006      * @return    the unsigned {@code long} value represented by the decimal string argument
1007      * @throws    NumberFormatException  if the string does not contain a
1008      *            parsable unsigned integer.
1009      * @since 1.8
1010      */
1011     public static long parseUnsignedLong(String s) throws NumberFormatException {