From 9929082d4908afea013f0a8ec8e74f0e067b65ee Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Mon, 10 Feb 2014 17:27:53 +0400 Subject: [PATCH] Adds Integer.toString emulation --- .../java/lang/TAbstractStringBuilder.java | 20 ++++++++++++------- .../teavm/classlib/java/lang/TCharacter.java | 7 +++++++ .../teavm/classlib/java/lang/TInteger.java | 7 +++++++ .../teavm/classlib/java/lang/IntegerTest.java | 7 +++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index 8e3bb6d65..c5cacc1e3 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -66,13 +66,17 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ return this; } - protected TAbstractStringBuilder append(int value) { + TAbstractStringBuilder append(int value) { + return append(value, 10); + } + + TAbstractStringBuilder append(int value, int radix) { boolean positive = true; if (value < 0) { positive = false; value = -value; } - if (value < 10) { + if (value < radix) { if (!positive) { ensureCapacity(length + 2); buffer[length++] = '-'; @@ -81,10 +85,12 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } buffer[length++] = (char)('0' + value); } else { - int pos = 10; + int pos = 1; int sz = 1; - while (pos < 1000000000 && pos * 10 <= value) { - pos *= 10; + int valueCopy = value; + while (valueCopy > radix) { + pos *= radix; + valueCopy /= radix; ++sz; } if (!positive) { @@ -95,9 +101,9 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ buffer[length++] = '-'; } while (pos > 0) { - buffer[length++] = (char)('0' + value / pos); + buffer[length++] = TCharacter.forDigit(value / pos, radix); value %= pos; - pos /= 10; + pos /= radix; } } return this; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java index 708cc1b0e..1e96261e6 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java @@ -64,6 +64,13 @@ public class TCharacter { return -1; } + public static char forDigit(int digit, int radix) { + if (radix < MIN_RADIX || radix > MAX_RADIX || digit >= radix) { + return '\0'; + } + return digit < 10 ? (char)('0' + digit) : (char)('a' + digit - 10); + } + private static int[] getDigitMapping() { if (digitMapping == null) { digitMapping = UnicodeHelper.decodeIntByte(obtainDigitMapping()); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java index 3063fe596..0c8dde314 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java @@ -34,6 +34,13 @@ public class TInteger extends TNumber implements TComparable { this(parseInt(s)); } + public static TString toString(int i, int radix) { + if (radix < MIN_VALUE || radix > MAX_VALUE) { + radix = 10; + } + return TString.wrap(new TAbstractStringBuilder(20).append(i, radix).toString()); + } + @Override public int compareTo(TInteger other) { return compare(value, other.value); diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java index 4b825c0fb..032d502f4 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java @@ -45,4 +45,11 @@ public class IntegerTest { public void rejectsIntegerWithDigitsOutOfRadix() { Integer.parseInt("99", 8); } + + @Test + public void writesInteger() { + assertEquals("473", Integer.toString(473, 10)); + assertEquals("-ff", Integer.toString(-255, 16)); + assertEquals("kona", Integer.toString(411787, 27)); + } }