classlib: fix more bugs in int-to-string conversion

This commit is contained in:
Alexey Andreev 2023-04-03 17:18:59 +02:00
parent 7442898898
commit 246498763d
5 changed files with 20 additions and 21 deletions

View File

@ -15,12 +15,13 @@
*/
package org.teavm.classlib.java.lang;
import java.util.Arrays;
import org.teavm.classlib.impl.text.DoubleAnalyzer;
import org.teavm.classlib.impl.text.FloatAnalyzer;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.util.TArrays;
class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequence {
class TAbstractStringBuilder implements TSerializable, TCharSequence {
static class Constants {
static int[] intPowersOfTen = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
1000000000 };
@ -44,11 +45,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
buffer = new char[capacity];
}
public TAbstractStringBuilder(TString value) {
this((TCharSequence) value);
public TAbstractStringBuilder(String value) {
this((CharSequence) value);
}
public TAbstractStringBuilder(TCharSequence value) {
public TAbstractStringBuilder(CharSequence value) {
buffer = new char[value.length()];
for (int i = 0; i < buffer.length; ++i) {
buffer[i] = value.charAt(i);
@ -110,15 +111,15 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
} else {
insertSpace(target, target + 1);
}
buffer[target++] = TCharacter.forDigit(value, radix);
buffer[target++] = Character.forDigit(value, radix);
} else {
int pos = 1;
int sz = 1;
var posLimit = Integer.divideUnsigned(Integer.MAX_VALUE, radix);
var posLimit = Integer.divideUnsigned(-1, radix);
while (Integer.compareUnsigned(pos * radix, value) <= 0) {
pos *= radix;
++sz;
if (Integer.compareUnsigned(pos, posLimit) >= 0) {
if (Integer.compareUnsigned(pos, posLimit) > 0) {
break;
}
}
@ -129,8 +130,8 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
if (!positive) {
buffer[target++] = '-';
}
while (pos > 0) {
buffer[target++] = TCharacter.forDigit(Integer.divideUnsigned(value, pos), radix);
while (pos != 0) {
buffer[target++] = Character.forDigit(Integer.divideUnsigned(value, pos), radix);
value = Integer.remainderUnsigned(value, pos);
pos = Integer.divideUnsigned(pos, radix);
}
@ -163,7 +164,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
} else {
int sz = 1;
long pos = 1;
var posLimit = Long.divideUnsigned(Long.MAX_VALUE, radix);
var posLimit = Long.divideUnsigned(-1, radix);
while (Long.compareUnsigned(pos * radix, value) <= 0) {
pos *= radix;
++sz;
@ -178,7 +179,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
if (!positive) {
buffer[target++] = '-';
}
while (pos > 0) {
while (pos != 0) {
buffer[target++] = TCharacter.forDigit((int) Long.divideUnsigned(value, pos), radix);
value = Long.remainderUnsigned(value, pos);
pos = Long.divideUnsigned(pos, radix);
@ -527,7 +528,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
int newLength = buffer.length < Integer.MAX_VALUE / 2
? Math.max(capacity, Math.max(buffer.length * 2, 5))
: Integer.MAX_VALUE;
buffer = TArrays.copyOf(buffer, newLength);
buffer = Arrays.copyOf(buffer, newLength);
}
public void trimToSize() {

View File

@ -24,11 +24,11 @@ public class TStringBuffer extends TAbstractStringBuilder implements TAppendable
super();
}
public TStringBuffer(TString value) {
public TStringBuffer(String value) {
super(value);
}
public TStringBuffer(TCharSequence value) {
public TStringBuffer(CharSequence value) {
super(value);
}

View File

@ -24,11 +24,11 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl
super();
}
public TStringBuilder(TString value) {
public TStringBuilder(String value) {
super(value);
}
public TStringBuilder(TCharSequence value) {
public TStringBuilder(CharSequence value) {
super(value);
}

View File

@ -178,7 +178,6 @@ public class IntegerTest {
assertEquals("11110001001000000", Integer.toString(123456, 2));
assertEquals("-10111", Integer.toString(-23, 2));
assertEquals("1111111111111111111111111111111", Integer.toString(Integer.MAX_VALUE, 2));
// TODO: looks like there's a bug in compiler. Fix and uncomment
//assertEquals("-10000000000000000000000000000000", Integer.toString(Integer.MIN_VALUE, 2));
assertEquals("-10000000000000000000000000000000", Integer.toString(Integer.MIN_VALUE, 2));
}
}

View File

@ -82,8 +82,7 @@ public class LongTest {
assertEquals("-10111", Long.toString(-23, 2));
assertEquals("111111111111111111111111111111111111111111111111111111111111111",
Long.toString(Long.MAX_VALUE, 2));
// TODO: looks like there's a bug in compiler. Fix and uncomment
/*assertEquals("-1000000000000000000000000000000000000000000000000000000000000000",
Long.toString(Long.MIN_VALUE, 2));*/
assertEquals("-1000000000000000000000000000000000000000000000000000000000000000",
Long.toString(Long.MIN_VALUE, 2));
}
}