classlib: fix delete and replace for AbstractStringBuilder (#783)

This commit is contained in:
Ivan Hetman 2023-10-05 14:27:15 +03:00 committed by GitHub
parent 83a53e13bc
commit fe1a169d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 3 deletions

View File

@ -668,12 +668,15 @@ class TAbstractStringBuilder implements TSerializable, TCharSequence {
} }
public TAbstractStringBuilder delete(int start, int end) { public TAbstractStringBuilder delete(int start, int end) {
if (start > end || start > length) { if (start < 0 || start > end || start > length) {
throw new TStringIndexOutOfBoundsException(); throw new TStringIndexOutOfBoundsException();
} }
if (start == end) { if (start == end) {
return this; return this;
} }
if (end > length) {
end = length;
}
int sz = length - end; int sz = length - end;
length -= end - start; length -= end - start;
for (int i = 0; i < sz; ++i) { for (int i = 0; i < sz; ++i) {
@ -683,6 +686,12 @@ class TAbstractStringBuilder implements TSerializable, TCharSequence {
} }
public TAbstractStringBuilder replace(int start, int end, TString str) { public TAbstractStringBuilder replace(int start, int end, TString str) {
if (start < 0 || start > end || start > length) {
throw new TStringIndexOutOfBoundsException();
}
if (end > length) {
end = length;
}
int oldSize = end - start; int oldSize = end - start;
if (str.length() > oldSize) { if (str.length() > oldSize) {
insertSpace(end, start + str.length()); insertSpace(end, start + str.length());

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner; import org.teavm.junit.TeaVMTestRunner;
@ -203,8 +204,8 @@ public class StringBuilderTest {
} }
@Test @Test
public void substringWithUpperBoundAtEndWorks() { public void substringWorks() {
assertEquals("23", "123".substring(1, 3)); assertEquals("23", new StringBuilder("123").substring(1, 3));
} }
@Test @Test
@ -216,4 +217,52 @@ public class StringBuilderTest {
assertEquals(0, sb.indexOf("12345")); assertEquals(0, sb.indexOf("12345"));
assertEquals(0, sb.indexOf("123")); assertEquals(0, sb.indexOf("123"));
} }
@Test
public void delete() {
StringBuilder sb = new StringBuilder("abcdef");
try {
sb.delete(-1, 3);
fail();
} catch (StringIndexOutOfBoundsException e) {
// ok
}
try {
sb.delete(7, 8);
fail();
} catch (StringIndexOutOfBoundsException e) {
// ok
}
sb.delete(6, 50);
assertEquals("abcdef", sb.toString());
sb.delete(5, 50);
assertEquals("abcde", sb.toString());
sb.delete(1, 4);
assertEquals("ae", sb.toString());
}
@Test
public void replace() {
StringBuilder sb = new StringBuilder("abcdef");
try {
sb.replace(-1, 3, "h");
fail();
} catch (StringIndexOutOfBoundsException e) {
// ok
}
try {
sb.replace(7, 8, "h");
fail();
} catch (StringIndexOutOfBoundsException e) {
// ok
}
sb.replace(6, 50, "g");
assertEquals("abcdefg", sb.toString());
sb.replace(6, 50, "h");
assertEquals("abcdefh", sb.toString());
sb.replace(1, 6, "g");
assertEquals("agh", sb.toString());
sb.replace(1, 1, "bc");
assertEquals("abcgh", sb.toString());
}
} }