< prev index next >

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

Print this page
rev 14560 : Merge


 362      * @param   i   a {@code long} to be converted to a string.
 363      * @return  the string representation of the unsigned {@code long}
 364      *          value represented by the argument in binary (base&nbsp;2).
 365      * @see #parseUnsignedLong(String, int)
 366      * @see #toUnsignedString(long, int)
 367      * @since   1.0.2
 368      */
 369     public static String toBinaryString(long i) {
 370         return toUnsignedString0(i, 1);
 371     }
 372 
 373     /**
 374      * Format a long (treated as unsigned) into a String.
 375      * @param val the value to format
 376      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 377      */
 378     static String toUnsignedString0(long val, int shift) {
 379         // assert shift > 0 && shift <=5 : "Illegal shift value";
 380         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 381         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 382 
 383         if (COMPACT_STRINGS) {
 384             byte[] buf = new byte[chars];
 385             formatUnsignedLong0(val, shift, buf, 0, chars);
 386             return new String(buf, LATIN1);
 387         } else {
 388             byte[] buf = new byte[chars * 2];
 389             formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
 390             return new String(buf, UTF16);
 391         }
 392     }
 393 
 394     /**
 395      * Format a long (treated as unsigned) into a character buffer. If
 396      * {@code len} exceeds the formatted ASCII representation of {@code val},
 397      * {@code buf} will be padded with leading zeroes.
 398      *
 399      * @param val the unsigned long to format
 400      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 401      * @param buf the character buffer to write to
 402      * @param offset the offset in the destination buffer to start at


 472      * @param   i  an integer to be converted to an unsigned string.
 473      * @return  an unsigned string representation of the argument.
 474      * @see     #toUnsignedString(long, int)
 475      * @since 1.8
 476      */
 477     public static String toUnsignedString(long i) {
 478         return toUnsignedString(i, 10);
 479     }
 480 
 481     /**
 482      * Places characters representing the long i into the
 483      * character array buf. The characters are placed into
 484      * the buffer backwards starting with the least significant
 485      * digit at the specified index (exclusive), and working
 486      * backwards from there.
 487      *
 488      * @implNote This method converts positive inputs into negative
 489      * values, to cover the Long.MIN_VALUE case. Converting otherwise
 490      * (negative to positive) will expose -Long.MIN_VALUE that overflows
 491      * long.





 492      */
 493     static void getChars(long i, int index, byte[] buf) {
 494         long q;
 495         int r;
 496         int charPos = index;
 497 
 498         boolean negative = (i < 0);
 499         if (!negative) {
 500             i = -i;
 501         }
 502 
 503         // Get 2 digits/iteration using longs until quotient fits into an int
 504         while (i <= Integer.MIN_VALUE) {
 505             q = i / 100;
 506             r = (int)((q * 100) - i);
 507             i = q;
 508             buf[--charPos] = Integer.DigitOnes[r];
 509             buf[--charPos] = Integer.DigitTens[r];
 510         }
 511 
 512         // Get 2 digits/iteration using ints
 513         int q2;


 516             q2 = i2 / 100;
 517             r  = (q2 * 100) - i2;
 518             i2 = q2;
 519             buf[--charPos] = Integer.DigitOnes[r];
 520             buf[--charPos] = Integer.DigitTens[r];
 521         }
 522 
 523         // We know there are at most two digits left at this point.
 524         q2 = i2 / 10;
 525         r  = (q2 * 10) - i2;
 526         buf[--charPos] = (byte)('0' + r);
 527 
 528         // Whatever left is the remaining digit.
 529         if (q2 < 0) {
 530             buf[--charPos] = (byte)('0' - q2);
 531         }
 532 
 533         if (negative) {
 534             buf[--charPos] = (byte)'-';
 535         }

 536     }
 537 
 538     static void getCharsUTF16(long i, int index, byte[] buf) {









 539         long q;
 540         int r;
 541         int charPos = index;
 542 
 543         boolean negative = (i < 0);
 544         if (!negative) {
 545             i = -i;
 546         }
 547 
 548         // Get 2 digits/iteration using longs until quotient fits into an int
 549         while (i <= Integer.MIN_VALUE) {
 550             q = i / 100;
 551             r = (int)((q * 100) - i);
 552             i = q;
 553             StringUTF16.putChar(buf, --charPos, Integer.DigitOnes[r]);
 554             StringUTF16.putChar(buf, --charPos, Integer.DigitTens[r]);
 555         }
 556 
 557         // Get 2 digits/iteration using ints
 558         int q2;


 561             q2 = i2 / 100;
 562             r  = (q2 * 100) - i2;
 563             i2 = q2;
 564             StringUTF16.putChar(buf, --charPos, Integer.DigitOnes[r]);
 565             StringUTF16.putChar(buf, --charPos, Integer.DigitTens[r]);
 566         }
 567 
 568         // We know there are at most two digits left at this point.
 569         q2 = i2 / 10;
 570         r  = (q2 * 10) - i2;
 571         StringUTF16.putChar(buf, --charPos, '0' + r);
 572 
 573         // Whatever left is the remaining digit.
 574         if (q2 < 0) {
 575             StringUTF16.putChar(buf, --charPos, '0' - q2);
 576         }
 577 
 578         if (negative) {
 579             StringUTF16.putChar(buf, --charPos, '-');
 580         }

 581     }
 582 
 583     /**
 584      * Returns the string representation size for a given long value.
 585      *
 586      * @param x long value
 587      * @return string size
 588      *
 589      * @implNote There are other ways to compute this: e.g. binary search,
 590      * but values are biased heavily towards zero, and therefore linear search
 591      * wins. The iteration results are also routinely inlined in the generated
 592      * code after loop unrolling.
 593      */
 594     static int stringSize(long x) {
 595         int d = 1;
 596         if (x >= 0) {
 597             d = 0;
 598             x = -x;
 599         }
 600         long p = -10;




 362      * @param   i   a {@code long} to be converted to a string.
 363      * @return  the string representation of the unsigned {@code long}
 364      *          value represented by the argument in binary (base&nbsp;2).
 365      * @see #parseUnsignedLong(String, int)
 366      * @see #toUnsignedString(long, int)
 367      * @since   1.0.2
 368      */
 369     public static String toBinaryString(long i) {
 370         return toUnsignedString0(i, 1);
 371     }
 372 
 373     /**
 374      * Format a long (treated as unsigned) into a String.
 375      * @param val the value to format
 376      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 377      */
 378     static String toUnsignedString0(long val, int shift) {
 379         // assert shift > 0 && shift <=5 : "Illegal shift value";
 380         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 381         int chars = Math.max(((mag + (shift - 1)) / shift), 1);

 382         if (COMPACT_STRINGS) {
 383             byte[] buf = new byte[chars];
 384             formatUnsignedLong0(val, shift, buf, 0, chars);
 385             return new String(buf, LATIN1);
 386         } else {
 387             byte[] buf = new byte[chars * 2];
 388             formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
 389             return new String(buf, UTF16);
 390         }
 391     }
 392 
 393     /**
 394      * Format a long (treated as unsigned) into a character buffer. If
 395      * {@code len} exceeds the formatted ASCII representation of {@code val},
 396      * {@code buf} will be padded with leading zeroes.
 397      *
 398      * @param val the unsigned long to format
 399      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 400      * @param buf the character buffer to write to
 401      * @param offset the offset in the destination buffer to start at


 471      * @param   i  an integer to be converted to an unsigned string.
 472      * @return  an unsigned string representation of the argument.
 473      * @see     #toUnsignedString(long, int)
 474      * @since 1.8
 475      */
 476     public static String toUnsignedString(long i) {
 477         return toUnsignedString(i, 10);
 478     }
 479 
 480     /**
 481      * Places characters representing the long i into the
 482      * character array buf. The characters are placed into
 483      * the buffer backwards starting with the least significant
 484      * digit at the specified index (exclusive), and working
 485      * backwards from there.
 486      *
 487      * @implNote This method converts positive inputs into negative
 488      * values, to cover the Long.MIN_VALUE case. Converting otherwise
 489      * (negative to positive) will expose -Long.MIN_VALUE that overflows
 490      * long.
 491      *
 492      * @param i     value to convert
 493      * @param index next index, after the least significant digit
 494      * @param buf   target buffer, Latin1-encoded
 495      * @return index of the most significant digit or minus sign, if present
 496      */
 497     static int getChars(long i, int index, byte[] buf) {
 498         long q;
 499         int r;
 500         int charPos = index;
 501 
 502         boolean negative = (i < 0);
 503         if (!negative) {
 504             i = -i;
 505         }
 506 
 507         // Get 2 digits/iteration using longs until quotient fits into an int
 508         while (i <= Integer.MIN_VALUE) {
 509             q = i / 100;
 510             r = (int)((q * 100) - i);
 511             i = q;
 512             buf[--charPos] = Integer.DigitOnes[r];
 513             buf[--charPos] = Integer.DigitTens[r];
 514         }
 515 
 516         // Get 2 digits/iteration using ints
 517         int q2;


 520             q2 = i2 / 100;
 521             r  = (q2 * 100) - i2;
 522             i2 = q2;
 523             buf[--charPos] = Integer.DigitOnes[r];
 524             buf[--charPos] = Integer.DigitTens[r];
 525         }
 526 
 527         // We know there are at most two digits left at this point.
 528         q2 = i2 / 10;
 529         r  = (q2 * 10) - i2;
 530         buf[--charPos] = (byte)('0' + r);
 531 
 532         // Whatever left is the remaining digit.
 533         if (q2 < 0) {
 534             buf[--charPos] = (byte)('0' - q2);
 535         }
 536 
 537         if (negative) {
 538             buf[--charPos] = (byte)'-';
 539         }
 540         return charPos;
 541     }
 542 
 543     /**
 544      * This is a variant of {@link #getChars(long, int, byte[])}, but for
 545      * UTF-16 coder.
 546      *
 547      * @param i     value to convert
 548      * @param index next index, after the least significant digit
 549      * @param buf   target buffer, UTF16-coded.
 550      * @return index of the most significant digit or minus sign, if present
 551      */
 552     static int getCharsUTF16(long i, int index, byte[] buf) {
 553         long q;
 554         int r;
 555         int charPos = index;
 556 
 557         boolean negative = (i < 0);
 558         if (!negative) {
 559             i = -i;
 560         }
 561 
 562         // Get 2 digits/iteration using longs until quotient fits into an int
 563         while (i <= Integer.MIN_VALUE) {
 564             q = i / 100;
 565             r = (int)((q * 100) - i);
 566             i = q;
 567             StringUTF16.putChar(buf, --charPos, Integer.DigitOnes[r]);
 568             StringUTF16.putChar(buf, --charPos, Integer.DigitTens[r]);
 569         }
 570 
 571         // Get 2 digits/iteration using ints
 572         int q2;


 575             q2 = i2 / 100;
 576             r  = (q2 * 100) - i2;
 577             i2 = q2;
 578             StringUTF16.putChar(buf, --charPos, Integer.DigitOnes[r]);
 579             StringUTF16.putChar(buf, --charPos, Integer.DigitTens[r]);
 580         }
 581 
 582         // We know there are at most two digits left at this point.
 583         q2 = i2 / 10;
 584         r  = (q2 * 10) - i2;
 585         StringUTF16.putChar(buf, --charPos, '0' + r);
 586 
 587         // Whatever left is the remaining digit.
 588         if (q2 < 0) {
 589             StringUTF16.putChar(buf, --charPos, '0' - q2);
 590         }
 591 
 592         if (negative) {
 593             StringUTF16.putChar(buf, --charPos, '-');
 594         }
 595         return charPos;
 596     }
 597 
 598     /**
 599      * Returns the string representation size for a given long value.
 600      *
 601      * @param x long value
 602      * @return string size
 603      *
 604      * @implNote There are other ways to compute this: e.g. binary search,
 605      * but values are biased heavily towards zero, and therefore linear search
 606      * wins. The iteration results are also routinely inlined in the generated
 607      * code after loop unrolling.
 608      */
 609     static int stringSize(long x) {
 610         int d = 1;
 611         if (x >= 0) {
 612             d = 0;
 613             x = -x;
 614         }
 615         long p = -10;


< prev index next >