mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Adds Integer.toString emulation
This commit is contained in:
parent
e094fe4192
commit
9929082d49
|
@ -66,13 +66,17 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected TAbstractStringBuilder append(int value) {
|
TAbstractStringBuilder append(int value) {
|
||||||
|
return append(value, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
TAbstractStringBuilder append(int value, int radix) {
|
||||||
boolean positive = true;
|
boolean positive = true;
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
positive = false;
|
positive = false;
|
||||||
value = -value;
|
value = -value;
|
||||||
}
|
}
|
||||||
if (value < 10) {
|
if (value < radix) {
|
||||||
if (!positive) {
|
if (!positive) {
|
||||||
ensureCapacity(length + 2);
|
ensureCapacity(length + 2);
|
||||||
buffer[length++] = '-';
|
buffer[length++] = '-';
|
||||||
|
@ -81,10 +85,12 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
||||||
}
|
}
|
||||||
buffer[length++] = (char)('0' + value);
|
buffer[length++] = (char)('0' + value);
|
||||||
} else {
|
} else {
|
||||||
int pos = 10;
|
int pos = 1;
|
||||||
int sz = 1;
|
int sz = 1;
|
||||||
while (pos < 1000000000 && pos * 10 <= value) {
|
int valueCopy = value;
|
||||||
pos *= 10;
|
while (valueCopy > radix) {
|
||||||
|
pos *= radix;
|
||||||
|
valueCopy /= radix;
|
||||||
++sz;
|
++sz;
|
||||||
}
|
}
|
||||||
if (!positive) {
|
if (!positive) {
|
||||||
|
@ -95,9 +101,9 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
||||||
buffer[length++] = '-';
|
buffer[length++] = '-';
|
||||||
}
|
}
|
||||||
while (pos > 0) {
|
while (pos > 0) {
|
||||||
buffer[length++] = (char)('0' + value / pos);
|
buffer[length++] = TCharacter.forDigit(value / pos, radix);
|
||||||
value %= pos;
|
value %= pos;
|
||||||
pos /= 10;
|
pos /= radix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -64,6 +64,13 @@ public class TCharacter {
|
||||||
return -1;
|
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() {
|
private static int[] getDigitMapping() {
|
||||||
if (digitMapping == null) {
|
if (digitMapping == null) {
|
||||||
digitMapping = UnicodeHelper.decodeIntByte(obtainDigitMapping());
|
digitMapping = UnicodeHelper.decodeIntByte(obtainDigitMapping());
|
||||||
|
|
|
@ -34,6 +34,13 @@ public class TInteger extends TNumber implements TComparable<TInteger> {
|
||||||
this(parseInt(s));
|
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
|
@Override
|
||||||
public int compareTo(TInteger other) {
|
public int compareTo(TInteger other) {
|
||||||
return compare(value, other.value);
|
return compare(value, other.value);
|
||||||
|
|
|
@ -45,4 +45,11 @@ public class IntegerTest {
|
||||||
public void rejectsIntegerWithDigitsOutOfRadix() {
|
public void rejectsIntegerWithDigitsOutOfRadix() {
|
||||||
Integer.parseInt("99", 8);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user