From 34b7d9178d1cf2805c255e822d15ece5f8e1891f Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Tue, 11 Mar 2014 17:14:31 +0400 Subject: [PATCH] Adds java.lang.StringBuilder methods --- .../java/lang/TAbstractStringBuilder.java | 45 ++++++++++++++ .../classlib/java/lang/TStringBuilder.java | 22 +++++++ .../classlib/java/lang/StringBuilderTest.java | 60 +++++++++++++++++++ 3 files changed, 127 insertions(+) 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 bbf10a167..b6e9bbbea 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 @@ -57,6 +57,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } public TAbstractStringBuilder(TString value) { + this((TCharSequence)value); + } + + public TAbstractStringBuilder(TCharSequence value) { buffer = new char[value.length()]; for (int i = 0; i < buffer.length; ++i) { buffer[i] = value.charAt(i); @@ -585,6 +589,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ return this; } + protected TAbstractStringBuilder append(char[] chars) { + return append(chars, 0, chars.length); + } + @Override public TCharSequence subSequence(int start, int end) { // TODO: implement @@ -615,4 +623,41 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ --length; return this; } + + public TAbstractStringBuilder delete(int start, int end) { + if (start > end || start >= length) { + throw new TStringIndexOutOfBoundsException(); + } + if (start == end) { + return this; + } + int sz = length - end; + length -= end - start; + for (int i = 0; i < sz; ++i) { + buffer[start++] = buffer[end++]; + } + return this; + } + + public TAbstractStringBuilder replace(int start, int end, TString str) { + int oldSize = end - start; + if (str.length() > oldSize) { + insertSpace(end, start + str.length()); + } else if (str.length() < oldSize) { + delete(start + str.length(), end); + } + for (int i = 0; i < str.length(); ++i) { + buffer[start++] = str.charAt(i); + } + return this; + } + + private void insertSpace(int start, int end) { + int sz = length - end; + ensureCapacity(buffer.length + sz); + for (int i = sz - 1; i >= 0; --i) { + buffer[end + i] = buffer[start + i]; + } + length += end - start; + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java index c7f03042b..a034472e7 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java @@ -32,6 +32,10 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl super(value); } + public TStringBuilder(TCharSequence value) { + super(value); + } + @Override public TStringBuilder append(TString string) { super.append(string); @@ -74,6 +78,12 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl return this; } + @Override + public TStringBuilder append(char[] chars) { + super.append(chars); + return this; + } + @Override public TStringBuilder appendCodePoint(int codePoint) { super.appendCodePoint(codePoint); @@ -104,6 +114,18 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl return this; } + @Override + public TStringBuilder delete(int start, int end) { + super.delete(start, end); + return this; + } + + @Override + public TStringBuilder replace(int start, int end, TString str) { + super.replace(start, end, str); + return this; + } + @Override public TStringBuilder deleteCharAt(int index) { deleteCharAt(index); diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java index 5526223c2..572e2bb20 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java @@ -282,4 +282,64 @@ public class StringBuilderTest { assertEquals(56178, sb.charAt(0)); assertEquals(56972, sb.charAt(1)); } + + @Test + public void deletesRange() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i <= 9; ++i) { + sb.append((char)('0' + i)); + } + sb.delete(4, 6); + assertEquals(8, sb.length()); + assertEquals('0', sb.charAt(0)); + assertEquals('3', sb.charAt(3)); + assertEquals('6', sb.charAt(4)); + assertEquals('9', sb.charAt(7)); + } + + @Test + public void replacesRangeWithSequenceOfSameLength() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i <= 9; ++i) { + sb.append((char)('0' + i)); + } + sb.replace(4, 6, "ab"); + assertEquals(10, sb.length()); + assertEquals('0', sb.charAt(0)); + assertEquals('3', sb.charAt(3)); + assertEquals('a', sb.charAt(4)); + assertEquals('6', sb.charAt(6)); + assertEquals('9', sb.charAt(9)); + } + + @Test + public void replacesRangeWithShorterSequence() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i <= 9; ++i) { + sb.append((char)('0' + i)); + } + sb.replace(4, 6, "a"); + assertEquals(9, sb.length()); + assertEquals('0', sb.charAt(0)); + assertEquals('3', sb.charAt(3)); + assertEquals('a', sb.charAt(4)); + assertEquals('6', sb.charAt(5)); + assertEquals('9', sb.charAt(8)); + } + + @Test + public void replacesRangeWithLongerSequence() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i <= 9; ++i) { + sb.append((char)('0' + i)); + } + sb.replace(4, 6, "abc"); + assertEquals(11, sb.length()); + assertEquals('0', sb.charAt(0)); + assertEquals('3', sb.charAt(3)); + assertEquals('a', sb.charAt(4)); + assertEquals('c', sb.charAt(6)); + assertEquals('6', sb.charAt(7)); + assertEquals('9', sb.charAt(10)); + } }