< prev index next >

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

Print this page




 381      * Format a long (treated as unsigned) into a String.
 382      * @param val the value to format
 383      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 384      */
 385     static String toUnsignedString0(long val, int shift) {
 386         // assert shift > 0 && shift <=5 : "Illegal shift value";
 387         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 388         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 389         if (COMPACT_STRINGS) {
 390             byte[] buf = new byte[chars];
 391             formatUnsignedLong0(val, shift, buf, 0, chars);
 392             return new String(buf, LATIN1);
 393         } else {
 394             byte[] buf = new byte[chars * 2];
 395             formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
 396             return new String(buf, UTF16);
 397         }
 398     }
 399 
 400     /**
 401      * Format a long (treated as unsigned) into a byte buffer (LATIN1 version). If
 402      * {@code len} exceeds the formatted ASCII representation of {@code val},
 403      * {@code buf} will be padded with leading zeroes.
 404      *
 405      * @param val the unsigned long to format
 406      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 407      * @param buf the byte buffer to write to
 408      * @param offset the offset in the destination buffer to start at
 409      * @param len the number of characters to write
 410      */
 411     private static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {


 412         int charPos = offset + len;
 413         int radix = 1 << shift;
 414         int mask = radix - 1;
 415         do {
 416             buf[--charPos] = (byte)Integer.digits[((int) val) & mask];
 417             val >>>= shift;
 418         } while (charPos > offset);
 419     }
 420 
 421     /**
 422      * Format a long (treated as unsigned) into a byte buffer (UTF16 version). If
 423      * {@code len} exceeds the formatted ASCII representation of {@code val},
 424      * {@code buf} will be padded with leading zeroes.
 425      *
 426      * @param val the unsigned long to format
 427      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 428      * @param buf the byte buffer to write to
 429      * @param offset the offset in the destination buffer to start at
 430      * @param len the number of characters to write
 431      */
 432     private static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, int offset, int len) {
 433         int charPos = offset + len;
 434         int radix = 1 << shift;
 435         int mask = radix - 1;
 436         do {
 437             StringUTF16.putChar(buf, --charPos, Integer.digits[((int) val) & mask]);
 438             val >>>= shift;
 439         } while (charPos > offset);
 440     }
 441 
 442     static String fastUUID(long lsb, long msb) {
 443         if (COMPACT_STRINGS) {
 444             byte[] buf = new byte[36];
 445             formatUnsignedLong0(lsb,        4, buf, 24, 12);
 446             formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4);
 447             formatUnsignedLong0(msb,        4, buf, 14, 4);
 448             formatUnsignedLong0(msb >>> 16, 4, buf, 9,  4);
 449             formatUnsignedLong0(msb >>> 32, 4, buf, 0,  8);
 450 
 451             buf[23] = '-';


 730      *                  representation to be parsed
 731      * @param      beginIndex   the beginning index, inclusive.
 732      * @param      endIndex     the ending index, exclusive.
 733      * @param      radix   the radix to be used while parsing {@code s}.
 734      * @return     the signed {@code long} represented by the subsequence in
 735      *             the specified radix.
 736      * @throws     NullPointerException  if {@code s} is null.
 737      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 738      *             negative, or if {@code beginIndex} is greater than
 739      *             {@code endIndex} or if {@code endIndex} is greater than
 740      *             {@code s.length()}.
 741      * @throws     NumberFormatException  if the {@code CharSequence} does not
 742      *             contain a parsable {@code long} in the specified
 743      *             {@code radix}, or if {@code radix} is either smaller than
 744      *             {@link java.lang.Character#MIN_RADIX} or larger than
 745      *             {@link java.lang.Character#MAX_RADIX}.
 746      * @since  9
 747      */
 748     public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
 749                 throws NumberFormatException {
 750         Objects.requireNonNull(s);
 751 
 752         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 753             throw new IndexOutOfBoundsException();
 754         }
 755         if (radix < Character.MIN_RADIX) {
 756             throw new NumberFormatException("radix " + radix +
 757                     " less than Character.MIN_RADIX");
 758         }
 759         if (radix > Character.MAX_RADIX) {
 760             throw new NumberFormatException("radix " + radix +
 761                     " greater than Character.MAX_RADIX");
 762         }
 763 
 764         boolean negative = false;
 765         int i = beginIndex;
 766         long limit = -Long.MAX_VALUE;
 767 
 768         if (i < endIndex) {
 769             char firstChar = s.charAt(i);
 770             if (firstChar < '0') { // Possible leading "+" or "-"


 976      *                 {@code long} representation to be parsed
 977      * @param      beginIndex   the beginning index, inclusive.
 978      * @param      endIndex     the ending index, exclusive.
 979      * @param      radix   the radix to be used while parsing {@code s}.
 980      * @return     the unsigned {@code long} represented by the subsequence in
 981      *             the specified radix.
 982      * @throws     NullPointerException  if {@code s} is null.
 983      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 984      *             negative, or if {@code beginIndex} is greater than
 985      *             {@code endIndex} or if {@code endIndex} is greater than
 986      *             {@code s.length()}.
 987      * @throws     NumberFormatException  if the {@code CharSequence} does not
 988      *             contain a parsable unsigned {@code long} in the specified
 989      *             {@code radix}, or if {@code radix} is either smaller than
 990      *             {@link java.lang.Character#MIN_RADIX} or larger than
 991      *             {@link java.lang.Character#MAX_RADIX}.
 992      * @since  9
 993      */
 994     public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)
 995                 throws NumberFormatException {
 996         Objects.requireNonNull(s);
 997 
 998         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 999             throw new IndexOutOfBoundsException();
1000         }
1001         int start = beginIndex, len = endIndex - beginIndex;
1002 
1003         if (len > 0) {
1004             char firstChar = s.charAt(start);
1005             if (firstChar == '-') {
1006                 throw new NumberFormatException(String.format("Illegal leading minus sign " +
1007                         "on unsigned string %s.", s.subSequence(start, start + len)));
1008             } else {
1009                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
1010                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
1011                     return parseLong(s, start, start + len, radix);
1012                 }
1013 
1014                 // No need for range checks on end due to testing above.
1015                 long first = parseLong(s, start, start + len - 1, radix);
1016                 int second = Character.digit(s.charAt(start + len - 1), radix);




 381      * Format a long (treated as unsigned) into a String.
 382      * @param val the value to format
 383      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 384      */
 385     static String toUnsignedString0(long val, int shift) {
 386         // assert shift > 0 && shift <=5 : "Illegal shift value";
 387         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 388         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 389         if (COMPACT_STRINGS) {
 390             byte[] buf = new byte[chars];
 391             formatUnsignedLong0(val, shift, buf, 0, chars);
 392             return new String(buf, LATIN1);
 393         } else {
 394             byte[] buf = new byte[chars * 2];
 395             formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
 396             return new String(buf, UTF16);
 397         }
 398     }
 399 
 400     /**
 401      * Format a long (treated as unsigned) into a character buffer. If
 402      * {@code len} exceeds the formatted ASCII representation of {@code val},
 403      * {@code buf} will be padded with leading zeroes.
 404      *
 405      * @param val the unsigned long to format
 406      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 407      * @param buf the character buffer to write to
 408      * @param offset the offset in the destination buffer to start at
 409      * @param len the number of characters to write
 410      */
 411 
 412     /** byte[]/LATIN1 version    */
 413     static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {
 414         int charPos = offset + len;
 415         int radix = 1 << shift;
 416         int mask = radix - 1;
 417         do {
 418             buf[--charPos] = (byte)Integer.digits[((int) val) & mask];
 419             val >>>= shift;
 420         } while (charPos > offset);
 421     }
 422 
 423     /** byte[]/UTF16 version    */










 424     private static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, int offset, int len) {
 425         int charPos = offset + len;
 426         int radix = 1 << shift;
 427         int mask = radix - 1;
 428         do {
 429             StringUTF16.putChar(buf, --charPos, Integer.digits[((int) val) & mask]);
 430             val >>>= shift;
 431         } while (charPos > offset);
 432     }
 433 
 434     static String fastUUID(long lsb, long msb) {
 435         if (COMPACT_STRINGS) {
 436             byte[] buf = new byte[36];
 437             formatUnsignedLong0(lsb,        4, buf, 24, 12);
 438             formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4);
 439             formatUnsignedLong0(msb,        4, buf, 14, 4);
 440             formatUnsignedLong0(msb >>> 16, 4, buf, 9,  4);
 441             formatUnsignedLong0(msb >>> 32, 4, buf, 0,  8);
 442 
 443             buf[23] = '-';


 722      *                  representation to be parsed
 723      * @param      beginIndex   the beginning index, inclusive.
 724      * @param      endIndex     the ending index, exclusive.
 725      * @param      radix   the radix to be used while parsing {@code s}.
 726      * @return     the signed {@code long} represented by the subsequence in
 727      *             the specified radix.
 728      * @throws     NullPointerException  if {@code s} is null.
 729      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 730      *             negative, or if {@code beginIndex} is greater than
 731      *             {@code endIndex} or if {@code endIndex} is greater than
 732      *             {@code s.length()}.
 733      * @throws     NumberFormatException  if the {@code CharSequence} does not
 734      *             contain a parsable {@code long} in the specified
 735      *             {@code radix}, or if {@code radix} is either smaller than
 736      *             {@link java.lang.Character#MIN_RADIX} or larger than
 737      *             {@link java.lang.Character#MAX_RADIX}.
 738      * @since  9
 739      */
 740     public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
 741                 throws NumberFormatException {
 742         s = Objects.requireNonNull(s);
 743 
 744         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 745             throw new IndexOutOfBoundsException();
 746         }
 747         if (radix < Character.MIN_RADIX) {
 748             throw new NumberFormatException("radix " + radix +
 749                     " less than Character.MIN_RADIX");
 750         }
 751         if (radix > Character.MAX_RADIX) {
 752             throw new NumberFormatException("radix " + radix +
 753                     " greater than Character.MAX_RADIX");
 754         }
 755 
 756         boolean negative = false;
 757         int i = beginIndex;
 758         long limit = -Long.MAX_VALUE;
 759 
 760         if (i < endIndex) {
 761             char firstChar = s.charAt(i);
 762             if (firstChar < '0') { // Possible leading "+" or "-"


 968      *                 {@code long} representation to be parsed
 969      * @param      beginIndex   the beginning index, inclusive.
 970      * @param      endIndex     the ending index, exclusive.
 971      * @param      radix   the radix to be used while parsing {@code s}.
 972      * @return     the unsigned {@code long} represented by the subsequence in
 973      *             the specified radix.
 974      * @throws     NullPointerException  if {@code s} is null.
 975      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 976      *             negative, or if {@code beginIndex} is greater than
 977      *             {@code endIndex} or if {@code endIndex} is greater than
 978      *             {@code s.length()}.
 979      * @throws     NumberFormatException  if the {@code CharSequence} does not
 980      *             contain a parsable unsigned {@code long} in the specified
 981      *             {@code radix}, or if {@code radix} is either smaller than
 982      *             {@link java.lang.Character#MIN_RADIX} or larger than
 983      *             {@link java.lang.Character#MAX_RADIX}.
 984      * @since  9
 985      */
 986     public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)
 987                 throws NumberFormatException {
 988         s = Objects.requireNonNull(s);
 989 
 990         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 991             throw new IndexOutOfBoundsException();
 992         }
 993         int start = beginIndex, len = endIndex - beginIndex;
 994 
 995         if (len > 0) {
 996             char firstChar = s.charAt(start);
 997             if (firstChar == '-') {
 998                 throw new NumberFormatException(String.format("Illegal leading minus sign " +
 999                         "on unsigned string %s.", s.subSequence(start, start + len)));
1000             } else {
1001                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
1002                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
1003                     return parseLong(s, start, start + len, radix);
1004                 }
1005 
1006                 // No need for range checks on end due to testing above.
1007                 long first = parseLong(s, start, start + len - 1, radix);
1008                 int second = Character.digit(s.charAt(start + len - 1), radix);


< prev index next >