mirror of
https://github.com/konsoletyper/teavm.git
synced 2024-12-15 02:10:30 +08:00
Adds java.lang.StringBuilder methods
This commit is contained in:
parent
d870060187
commit
34b7d9178d
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user