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.util.Objects; 30 import jdk.internal.HotSpotIntrinsicCandidate; 31 32 /** 33 * The {@code Integer} class wraps a value of the primitive type 34 * {@code int} in an object. An object of type {@code Integer} 35 * contains a single field whose type is {@code int}. 36 * 37 * <p>In addition, this class provides several methods for converting 38 * an {@code int} to a {@code String} and a {@code String} to an 39 * {@code int}, as well as other constants and methods useful when 40 * dealing with an {@code int}. 41 * 42 * <p>Implementation note: The implementations of the "bit twiddling" 43 * methods (such as {@link #highestOneBit(int) highestOneBit} and 44 * {@link #numberOfTrailingZeros(int) 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 50 * @author Josh Bloch 51 * @author Joseph D. Darcy 121 * 122 * <blockquote> 123 * {@code Integer.toString(n, 16).toUpperCase()} 124 * </blockquote> 125 * 126 * @param i an integer to be converted to a string. 127 * @param radix the radix to use in the string representation. 128 * @return a string representation of the argument in the specified radix. 129 * @see java.lang.Character#MAX_RADIX 130 * @see java.lang.Character#MIN_RADIX 131 */ 132 public static String toString(int i, int radix) { 133 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 134 radix = 10; 135 136 /* Use the faster version */ 137 if (radix == 10) { 138 return toString(i); 139 } 140 141 char buf[] = new char[33]; 142 boolean negative = (i < 0); 143 int charPos = 32; 144 145 if (!negative) { 146 i = -i; 147 } 148 149 while (i <= -radix) { 150 buf[charPos--] = digits[-(i % radix)]; 151 i = i / radix; 152 } 153 buf[charPos] = digits[-i]; 154 155 if (negative) { 156 buf[--charPos] = '-'; 157 } 158 159 return new String(buf, charPos, (33 - charPos)); 160 } 161 162 /** 163 * Returns a string representation of the first argument as an 164 * unsigned integer value in the radix specified by the second 165 * argument. 166 * 167 * <p>If the radix is smaller than {@code Character.MIN_RADIX} 168 * or larger than {@code Character.MAX_RADIX}, then the radix 169 * {@code 10} is used instead. 170 * 171 * <p>Note that since the first argument is treated as an unsigned 172 * value, no leading sign character is printed. 173 * 174 * <p>If the magnitude is zero, it is represented by a single zero 175 * character {@code '0'} ({@code '\u005Cu0030'}); otherwise, 176 * the first character of the representation of the magnitude will 177 * not be the zero character. 178 * 179 * <p>The behavior of radixes and the characters used as digits 295 * '1'} ({@code '\u005Cu0031'}) are used as binary digits. 296 * 297 * @param i an integer to be converted to a string. 298 * @return the string representation of the unsigned integer value 299 * represented by the argument in binary (base 2). 300 * @see #parseUnsignedInt(String, int) 301 * @see #toUnsignedString(int, int) 302 * @since 1.0.2 303 */ 304 public static String toBinaryString(int i) { 305 return toUnsignedString0(i, 1); 306 } 307 308 /** 309 * Convert the integer to an unsigned number. 310 */ 311 private static String toUnsignedString0(int val, int shift) { 312 // assert shift > 0 && shift <=5 : "Illegal shift value"; 313 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); 314 int chars = Math.max(((mag + (shift - 1)) / shift), 1); 315 char[] buf = new char[chars]; 316 317 formatUnsignedInt(val, shift, buf, 0, chars); 318 319 // Use special constructor which takes over "buf". 320 return new String(buf, true); 321 } 322 323 /** 324 * Format an {@code int} (treated as unsigned) into a character buffer. If 325 * {@code len} exceeds the formatted ASCII representation of {@code val}, 326 * {@code buf} will be padded with leading zeroes. 327 * 328 * @param val the unsigned int to format 329 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 330 * @param buf the character buffer to write to 331 * @param offset the offset in the destination buffer to start at 332 * @param len the number of characters to write 333 */ 334 static void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { 335 // assert shift > 0 && shift <=5 : "Illegal shift value"; 336 // assert offset >= 0 && offset < buf.length : "illegal offset"; 337 // assert len > 0 && (offset + len) <= buf.length : "illegal length"; 338 int charPos = offset + len; 339 int radix = 1 << shift; 340 int mask = radix - 1; 341 do { 342 buf[--charPos] = Integer.digits[val & mask]; 343 val >>>= shift; 344 } while (charPos > offset); 345 } 346 347 static final char [] DigitTens = { 348 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 349 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 350 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 351 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 352 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 353 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', 354 '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', 355 '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', 356 '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', 357 '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', 358 } ; 359 360 static final char [] DigitOnes = { 361 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 362 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 363 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 364 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 365 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 366 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 384 // RE: Division by Invariant Integers using Multiplication 385 // T Gralund, P Montgomery 386 // ACM PLDI 1994 387 // 388 389 /** 390 * Returns a {@code String} object representing the 391 * specified integer. The argument is converted to signed decimal 392 * representation and returned as a string, exactly as if the 393 * argument and radix 10 were given as arguments to the {@link 394 * #toString(int, int)} method. 395 * 396 * @param i an integer to be converted. 397 * @return a string representation of the argument in base 10. 398 */ 399 @HotSpotIntrinsicCandidate 400 public static String toString(int i) { 401 if (i == Integer.MIN_VALUE) 402 return "-2147483648"; 403 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 404 char[] buf = new char[size]; 405 getChars(i, size, buf); 406 return new String(buf, true); 407 } 408 409 /** 410 * Returns a string representation of the argument as an unsigned 411 * decimal value. 412 * 413 * The argument is converted to unsigned decimal representation 414 * and returned as a string exactly as if the argument and radix 415 * 10 were given as arguments to the {@link #toUnsignedString(int, 416 * int)} method. 417 * 418 * @param i an integer to be converted to an unsigned string. 419 * @return an unsigned string representation of the argument. 420 * @see #toUnsignedString(int, int) 421 * @since 1.8 422 */ 423 public static String toUnsignedString(int i) { 424 return Long.toString(toUnsignedLong(i)); 425 } 426 427 /** 428 * Places characters representing the integer i into the 429 * character array buf. The characters are placed into 430 * the buffer backwards starting with the least significant 431 * digit at the specified index (exclusive), and working 432 * backwards from there. 433 * 434 * Will fail if i == Integer.MIN_VALUE 435 */ 436 static void getChars(int i, int index, char[] buf) { 437 int q, r; 438 int charPos = index; 439 char sign = 0; 440 441 if (i < 0) { 442 sign = '-'; 443 i = -i; 444 } 445 446 // Generate two digits per iteration 447 while (i >= 65536) { 448 q = i / 100; 449 // really: r = i - (q * 100); 450 r = i - ((q << 6) + (q << 5) + (q << 2)); 451 i = q; 452 buf [--charPos] = DigitOnes[r]; 453 buf [--charPos] = DigitTens[r]; 454 } 455 456 // Fall thru to fast mode for smaller numbers 457 // assert(i <= 65536, i); 458 for (;;) { 459 q = (i * 52429) >>> (16+3); 460 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 461 buf [--charPos] = digits [r]; 462 i = q; 463 if (i == 0) break; 464 } 465 if (sign != 0) { 466 buf [--charPos] = sign; 467 } 468 } 469 470 static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 471 99999999, 999999999, Integer.MAX_VALUE }; 472 473 // Requires positive x 474 static int stringSize(int x) { 475 for (int i=0; ; i++) 476 if (x <= sizeTable[i]) 477 return i+1; 478 } 479 480 /** 481 * Parses the string argument as a signed integer in the radix 482 * specified by the second argument. The characters in the string 483 * must all be digits of the specified radix (as determined by 484 * whether {@link java.lang.Character#digit(char, int)} returns a 485 * nonnegative value), except that the first character may be an 486 * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to | 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.util.Objects; 30 import jdk.internal.HotSpotIntrinsicCandidate; 31 32 import static java.lang.String.COMPACT_STRINGS; 33 import static java.lang.String.LATIN1; 34 import static java.lang.String.UTF16; 35 36 /** 37 * The {@code Integer} class wraps a value of the primitive type 38 * {@code int} in an object. An object of type {@code Integer} 39 * contains a single field whose type is {@code int}. 40 * 41 * <p>In addition, this class provides several methods for converting 42 * an {@code int} to a {@code String} and a {@code String} to an 43 * {@code int}, as well as other constants and methods useful when 44 * dealing with an {@code int}. 45 * 46 * <p>Implementation note: The implementations of the "bit twiddling" 47 * methods (such as {@link #highestOneBit(int) highestOneBit} and 48 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are 49 * based on material from Henry S. Warren, Jr.'s <i>Hacker's 50 * Delight</i>, (Addison Wesley, 2002). 51 * 52 * @author Lee Boynton 53 * @author Arthur van Hoff 54 * @author Josh Bloch 55 * @author Joseph D. Darcy 125 * 126 * <blockquote> 127 * {@code Integer.toString(n, 16).toUpperCase()} 128 * </blockquote> 129 * 130 * @param i an integer to be converted to a string. 131 * @param radix the radix to use in the string representation. 132 * @return a string representation of the argument in the specified radix. 133 * @see java.lang.Character#MAX_RADIX 134 * @see java.lang.Character#MIN_RADIX 135 */ 136 public static String toString(int i, int radix) { 137 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 138 radix = 10; 139 140 /* Use the faster version */ 141 if (radix == 10) { 142 return toString(i); 143 } 144 145 if (COMPACT_STRINGS) { 146 byte[] buf = new byte[33]; 147 boolean negative = (i < 0); 148 int charPos = 32; 149 150 if (!negative) { 151 i = -i; 152 } 153 154 while (i <= -radix) { 155 buf[charPos--] = (byte)digits[-(i % radix)]; 156 i = i / radix; 157 } 158 buf[charPos] = (byte)digits[-i]; 159 160 if (negative) { 161 buf[--charPos] = '-'; 162 } 163 164 return StringLatin1.newString(buf, charPos, (33 - charPos)); 165 } 166 return toStringUTF16(i, radix); 167 } 168 169 private static String toStringUTF16(int i, int radix) { 170 byte[] buf = new byte[33 * 2]; 171 boolean negative = (i < 0); 172 int charPos = 32; 173 if (!negative) { 174 i = -i; 175 } 176 while (i <= -radix) { 177 StringUTF16.putChar(buf, charPos--, digits[-(i % radix)]); 178 i = i / radix; 179 } 180 StringUTF16.putChar(buf, charPos, digits[-i]); 181 182 if (negative) { 183 StringUTF16.putChar(buf, --charPos, '-'); 184 } 185 return StringUTF16.newString(buf, charPos, (33 - charPos)); 186 } 187 188 /** 189 * Returns a string representation of the first argument as an 190 * unsigned integer value in the radix specified by the second 191 * argument. 192 * 193 * <p>If the radix is smaller than {@code Character.MIN_RADIX} 194 * or larger than {@code Character.MAX_RADIX}, then the radix 195 * {@code 10} is used instead. 196 * 197 * <p>Note that since the first argument is treated as an unsigned 198 * value, no leading sign character is printed. 199 * 200 * <p>If the magnitude is zero, it is represented by a single zero 201 * character {@code '0'} ({@code '\u005Cu0030'}); otherwise, 202 * the first character of the representation of the magnitude will 203 * not be the zero character. 204 * 205 * <p>The behavior of radixes and the characters used as digits 321 * '1'} ({@code '\u005Cu0031'}) are used as binary digits. 322 * 323 * @param i an integer to be converted to a string. 324 * @return the string representation of the unsigned integer value 325 * represented by the argument in binary (base 2). 326 * @see #parseUnsignedInt(String, int) 327 * @see #toUnsignedString(int, int) 328 * @since 1.0.2 329 */ 330 public static String toBinaryString(int i) { 331 return toUnsignedString0(i, 1); 332 } 333 334 /** 335 * Convert the integer to an unsigned number. 336 */ 337 private static String toUnsignedString0(int val, int shift) { 338 // assert shift > 0 && shift <=5 : "Illegal shift value"; 339 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); 340 int chars = Math.max(((mag + (shift - 1)) / shift), 1); 341 342 if (COMPACT_STRINGS) { 343 byte[] buf = new byte[chars]; 344 formatUnsignedInt(val, shift, buf, 0, chars); 345 return new String(buf, LATIN1); 346 } else { 347 byte[] buf = new byte[chars * 2]; 348 formatUnsignedIntUTF16(val, shift, buf, 0, chars); 349 return new String(buf, UTF16); 350 } 351 } 352 353 /** 354 * Format an {@code int} (treated as unsigned) into a character buffer. If 355 * {@code len} exceeds the formatted ASCII representation of {@code val}, 356 * {@code buf} will be padded with leading zeroes. 357 * 358 * @param val the unsigned int to format 359 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 360 * @param buf the character buffer to write to 361 * @param offset the offset in the destination buffer to start at 362 * @param len the number of characters to write 363 */ 364 static void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { 365 // assert shift > 0 && shift <=5 : "Illegal shift value"; 366 // assert offset >= 0 && offset < buf.length : "illegal offset"; 367 // assert len > 0 && (offset + len) <= buf.length : "illegal length"; 368 int charPos = offset + len; 369 int radix = 1 << shift; 370 int mask = radix - 1; 371 do { 372 buf[--charPos] = Integer.digits[val & mask]; 373 val >>>= shift; 374 } while (charPos > offset); 375 } 376 377 /** byte[]/LATIN1 version */ 378 static void formatUnsignedInt(int val, int shift, byte[] buf, int offset, int len) { 379 int charPos = offset + len; 380 int radix = 1 << shift; 381 int mask = radix - 1; 382 do { 383 buf[--charPos] = (byte)Integer.digits[val & mask]; 384 val >>>= shift; 385 } while (charPos > offset); 386 } 387 388 /** byte[]/UTF16 version */ 389 static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int offset, int len) { 390 int charPos = offset + len; 391 int radix = 1 << shift; 392 int mask = radix - 1; 393 do { 394 StringUTF16.putChar(buf, --charPos, Integer.digits[val & mask]); 395 val >>>= shift; 396 } while (charPos > offset); 397 } 398 399 static final char [] DigitTens = { 400 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 401 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 402 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 403 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 404 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 405 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', 406 '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', 407 '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', 408 '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', 409 '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', 410 } ; 411 412 static final char [] DigitOnes = { 413 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 414 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 415 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 416 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 417 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 418 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 436 // RE: Division by Invariant Integers using Multiplication 437 // T Gralund, P Montgomery 438 // ACM PLDI 1994 439 // 440 441 /** 442 * Returns a {@code String} object representing the 443 * specified integer. The argument is converted to signed decimal 444 * representation and returned as a string, exactly as if the 445 * argument and radix 10 were given as arguments to the {@link 446 * #toString(int, int)} method. 447 * 448 * @param i an integer to be converted. 449 * @return a string representation of the argument in base 10. 450 */ 451 @HotSpotIntrinsicCandidate 452 public static String toString(int i) { 453 if (i == Integer.MIN_VALUE) 454 return "-2147483648"; 455 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 456 if (COMPACT_STRINGS) { 457 byte[] buf = new byte[size]; 458 getChars(i, size, buf); 459 return new String(buf, LATIN1); 460 } else { 461 byte[] buf = new byte[size * 2]; 462 getCharsUTF16(i, size, buf); 463 return new String(buf, UTF16); 464 } 465 } 466 467 /** 468 * Returns a string representation of the argument as an unsigned 469 * decimal value. 470 * 471 * The argument is converted to unsigned decimal representation 472 * and returned as a string exactly as if the argument and radix 473 * 10 were given as arguments to the {@link #toUnsignedString(int, 474 * int)} method. 475 * 476 * @param i an integer to be converted to an unsigned string. 477 * @return an unsigned string representation of the argument. 478 * @see #toUnsignedString(int, int) 479 * @since 1.8 480 */ 481 public static String toUnsignedString(int i) { 482 return Long.toString(toUnsignedLong(i)); 483 } 484 485 /** 486 * Places characters representing the integer i into the 487 * character array buf. The characters are placed into 488 * the buffer backwards starting with the least significant 489 * digit at the specified index (exclusive), and working 490 * backwards from there. 491 * 492 * Will fail if i == Integer.MIN_VALUE 493 */ 494 static void getChars(int i, int index, byte[] buf) { 495 int q, r; 496 int charPos = index; 497 char sign = 0; 498 499 if (i < 0) { 500 sign = '-'; 501 i = -i; 502 } 503 504 // Generate two digits per iteration 505 while (i >= 65536) { 506 q = i / 100; 507 // really: r = i - (q * 100); 508 r = i - ((q << 6) + (q << 5) + (q << 2)); 509 i = q; 510 buf [--charPos] = (byte)DigitOnes[r]; 511 buf [--charPos] = (byte)DigitTens[r]; 512 } 513 514 // Fall thru to fast mode for smaller numbers 515 // assert(i <= 65536, i); 516 for (;;) { 517 q = (i * 52429) >>> (16+3); 518 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 519 buf [--charPos] = (byte)digits [r]; 520 i = q; 521 if (i == 0) break; 522 } 523 if (sign != 0) { 524 buf [--charPos] = (byte)sign; 525 } 526 } 527 528 static void getCharsUTF16(int i, int index, byte[] buf) { 529 int q, r; 530 int charPos = index; 531 char sign = 0; 532 533 if (i < 0) { 534 sign = '-'; 535 i = -i; 536 } 537 538 // Generate two digits per iteration 539 while (i >= 65536) { 540 q = i / 100; 541 // really: r = i - (q * 100); 542 r = i - ((q << 6) + (q << 5) + (q << 2)); 543 i = q; 544 StringUTF16.putChar(buf, --charPos, DigitOnes[r]); 545 StringUTF16.putChar(buf, --charPos, DigitTens[r]); 546 } 547 548 // Fall thru to fast mode for smaller numbers 549 // assert(i <= 65536, i); 550 for (;;) { 551 q = (i * 52429) >>> (16+3); 552 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 553 StringUTF16.putChar(buf, --charPos, Integer.digits[r]); 554 i = q; 555 if (i == 0) break; 556 } 557 if (sign != 0) { 558 StringUTF16.putChar(buf, --charPos, sign); 559 } 560 } 561 562 static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 563 99999999, 999999999, Integer.MAX_VALUE }; 564 565 // Requires positive x 566 static int stringSize(int x) { 567 for (int i=0; ; i++) 568 if (x <= sizeTable[i]) 569 return i+1; 570 } 571 572 /** 573 * Parses the string argument as a signed integer in the radix 574 * specified by the second argument. The characters in the string 575 * must all be digits of the specified radix (as determined by 576 * whether {@link java.lang.Character#digit(char, int)} returns a 577 * nonnegative value), except that the first character may be an 578 * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to |