From 246498763d72de058e295caf7dffaa6cfd7ee5f7 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 3 Apr 2023 17:18:59 +0200 Subject: [PATCH] classlib: fix more bugs in int-to-string conversion --- .../java/lang/TAbstractStringBuilder.java | 25 ++++++++++--------- .../classlib/java/lang/TStringBuffer.java | 4 +-- .../classlib/java/lang/TStringBuilder.java | 4 +-- .../teavm/classlib/java/lang/IntegerTest.java | 3 +-- .../teavm/classlib/java/lang/LongTest.java | 5 ++-- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index b85010fb2..a7cf112bc 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -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() { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java index 5dfe1173b..6d86a941b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java @@ -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); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java index 689fe6388..765337902 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java @@ -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); } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java index 7d108f249..09a19b4f4 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java @@ -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)); } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/LongTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/LongTest.java index 88a87abc5..4b0f35fbe 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/LongTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/LongTest.java @@ -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)); } }