From 1051d59f8fb6ed56cbf60da43d95fe9ded010e27 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 13 Dec 2013 16:33:22 +0400 Subject: [PATCH] Adds append(long) and valueOf(long) implementation --- .../java/lang/StringBuilderTests.java | 29 +++++++++++++++ .../java/lang/TAbstractStringBuilder.java | 37 +++++++++++++++++++ .../org/teavm/classlib/java/lang/TString.java | 8 +++- .../classlib/java/lang/TStringBuilder.java | 6 +++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringBuilderTests.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringBuilderTests.java index 293eaeebe..0871b3da2 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringBuilderTests.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringBuilderTests.java @@ -8,6 +8,7 @@ import org.junit.Test; * @author Alexey Andreev */ public class StringBuilderTests { + @Test public void integerAppended() { StringBuilder sb = new StringBuilder(); sb.append(23); @@ -35,6 +36,34 @@ public class StringBuilderTests { assertEquals("2147483647", sb.toString()); } + @Test + public void longAppended() { + StringBuilder sb = new StringBuilder(); + sb.append(23L); + assertEquals("23", sb.toString()); + } + + @Test + public void negativeLongAppended() { + StringBuilder sb = new StringBuilder(); + sb.append(-23L); + assertEquals("-23", sb.toString()); + } + + @Test + public void largeLongAppended() { + StringBuilder sb = new StringBuilder(); + sb.append(12345678901234L); + assertEquals("12345678901234", sb.toString()); + } + + @Test + public void maxLongAppended() { + StringBuilder sb = new StringBuilder(); + sb.append(9223372036854775807L); + assertEquals("9223372036854775807", sb.toString()); + } + @Test public void appendsCodePoint() { StringBuilder sb = new StringBuilder(); 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 5e0d210c6..305fbceb8 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 @@ -67,6 +67,43 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ return this; } + protected TAbstractStringBuilder append(long value) { + boolean positive = true; + if (value < 0) { + positive = false; + value = -value; + } + if (value < 10) { + if (!positive) { + ensureCapacity(length + 2); + buffer[length++] = '-'; + } else { + ensureCapacity(length + 1); + } + buffer[length++] = (char)('0' + value); + } else { + int pos = 10; + int sz = 1; + while (pos < 1000000000000000000L && pos * 10 <= value) { + pos *= 10; + ++sz; + } + if (!positive) { + ++sz; + } + ensureCapacity(length + sz); + if (!positive) { + buffer[length++] = '-'; + } + while (pos > 0) { + buffer[length++] = (char)('0' + value / pos); + value %= pos; + pos /= 10; + } + } + return this; + } + protected TAbstractStringBuilder append(char c) { ensureCapacity(length + 1); buffer[length++] = c; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java index 0a66d0d8d..5138d6c1f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java @@ -415,8 +415,12 @@ public class TString extends TObject implements TSerializable, TComparable