Index: src/main/java/java/lang/Integer.java =================================================================== --- src/main/java/java/lang/Integer.java (revision 732677) +++ src/main/java/java/lang/Integer.java (working copy) @@ -64,7 +64,12 @@ * @since 1.5 */ public static final int SIZE = 32; + + private static final int[] numbers = new int[]{ 1000000000, 100000000, 10000000, + 1000000, 100000, 10000, 1000, 100, 10, 1 }; + + /** * The java.lang.Class that represents this class. */ @@ -506,19 +511,279 @@ return Integer.toString(value); } + public static String toString(int value) { + return toString_Orig(value); +// return toString_Kevin(value); +// return toString_Aleksey(value); +// return toString_Jim(value); +// return toString_Jim_Aleksey(value); + } + + public static String toString_Orig(int value) { + return toString(value, 10); + } + + public static String toString_Kevin(int value) { + int len = 1; + int quot; + int i = value; + + while ((i /= 10) != 0) { + len++; + } + + if (value >= 0) { + quot = -value; + } else { + // leave room for '-' + len++; + quot = value; + } + + char[] values = new char[len]; + int index = len - 1; + do { + int res = quot / 10; + int rem = quot - (res * 10); + values[index] = (char) ('0' - rem); + index--; + quot = res; + } while (quot != 0); + + if (value < 0) { + values[index] = '-'; + } + + return new String(0, len, values); + } + + /* + * Radix-10 specialized version + */ + public static String toString_Aleksey(int value) { + int len = 1; + int quot; + int i = value; + + /* + while ((i /= 10) != 0) { + len++; + } + */ + len = 11; + + if (value >= 0) { + quot = -value; + } else { + // leave room for '-' + len++; + quot = value; + } + + char[] values = new char[len]; + int index = len - 1; + do { + int res = quot / 10; + int rem = quot - (res * 10); + values[index] = (char) ('0' - rem); + index--; + quot = res; + } while (quot != 0); + + if (value < 0) { + values[index] = '-'; + } + + return new String(0, len, values); + } + /** * Answers a string containing characters in the range 0..9 which describe * the decimal representation of the argument. * - * @param i + * @param value * an int to get the representation of * @return String the representation of the argument */ - public static String toString(int i) { - return toString(i, 10); + public static String toString_Jim(int value) { + if (value == 0) { + return "0"; //$NON-NLS-1$ + } + + if (value < 1000 && value > -1000) { + char[] buffer = new char[4]; + int positive_value = value < 0 ? -value : value; + int first_digit = 0; + if (value < 0) { + buffer[0] = '-'; + first_digit++; + } + int last_digit = first_digit; + do { + int digit_value = (int) (positive_value % 10); + digit_value += '0'; + buffer[last_digit++] = (char) digit_value; + } while ((positive_value /= 10) != 0); + + int count = last_digit--; + do { + char tmp = buffer[last_digit]; + buffer[last_digit--] = buffer[first_digit]; + buffer[first_digit++] = tmp; + } while (first_digit < last_digit); + return new String(0, count, buffer); + } + if (value == MIN_VALUE) { + return "-2147483648";//$NON-NLS-1$ + } + + char[] buffer = new char[11]; + int positive_value = value < 0 ? -value : value; + byte first_digit = 0; + if (value < 0) { + buffer[0] = '-'; + first_digit++; + } + byte last_digit = first_digit; + byte count; + int number; + boolean start = false; + for (int i = 0; i < 9; i++) { + count = 0; + if (positive_value < (number = numbers[i])) { + if (start) { + buffer[last_digit++] = '0'; + } + continue; + } + + if (i > 0) { + number = (numbers[i] << 3); + if (positive_value >= number) { + positive_value -= number; + count += 8; + } + number = (numbers[i] << 2); + if (positive_value >= number) { + positive_value -= number; + count += 4; + } + } + number = (numbers[i] << 1); + if (positive_value >= number) { + positive_value -= number; + count += 2; + } + if (positive_value >= numbers[i]) { + positive_value -= numbers[i]; + count++; + } + if (count > 0 && !start) { + start = true; + } + if (start) { + buffer[last_digit++] = (char) (count + '0'); + } + } + + buffer[last_digit++] = (char) (positive_value + '0'); + count = last_digit--; + return new String(0, count, buffer); + } - /** + public static String toString_Jim_Aleksey(int value) { + if (value == 0) { + return "0"; //$NON-NLS-1$ + } + + if (value < 1000 && value > -1000) { + char[] buffer = new char[4]; + int positive_value = value < 0 ? -value : value; + int first_digit = 0; + if (value < 0) { + buffer[0] = '-'; + first_digit++; + } + int last_digit = first_digit; + int quot = positive_value; + do { + int res = quot / 10; + int digit_value = quot - (res * 10); + digit_value += '0'; + buffer[last_digit++] = (char) digit_value; + quot = res; + } while (quot != 0); + + int count = last_digit--; + do { + char tmp = buffer[last_digit]; + buffer[last_digit--] = buffer[first_digit]; + buffer[first_digit++] = tmp; + } while (first_digit < last_digit); + return new String(0, count, buffer); + } + if (value == MIN_VALUE) { + return "-2147483648";//$NON-NLS-1$ + } + + char[] buffer = new char[11]; + int positive_value = value < 0 ? -value : value; + byte first_digit = 0; + if (value < 0) { + buffer[0] = '-'; + first_digit++; + } + byte last_digit = first_digit; + byte count; + int number; + boolean start = false; + for (int i = 0; i < 9; i++) { + count = 0; + if (positive_value < (number = numbers[i])) { + if (start) { + buffer[last_digit++] = '0'; + } + continue; + } + + if (i > 0) { + number = (numbers[i] << 3); + if (positive_value >= number) { + positive_value -= number; + count += 8; + } + number = (numbers[i] << 2); + if (positive_value >= number) { + positive_value -= number; + count += 4; + } + } + number = (numbers[i] << 1); + if (positive_value >= number) { + positive_value -= number; + count += 2; + } + if (positive_value >= numbers[i]) { + positive_value -= numbers[i]; + count++; + } + if (count > 0 && !start) { + start = true; + } + if (start) { + buffer[last_digit++] = (char) (count + '0'); + } + } + + buffer[last_digit++] = (char) (positive_value + '0'); + count = last_digit--; + return new String(0, count, buffer); + + } + + + /* * Answers a string containing characters in the range 0..9, a..z (depending * on the radix) which describe the representation of the argument in that * radix.