mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
classlib: fix delete and replace for AbstractStringBuilder (#783)
This commit is contained in:
parent
83a53e13bc
commit
fe1a169d9b
|
@ -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());
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user