From fe1a169d9bce454be832555dfecf7e4f09b66597 Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Thu, 5 Oct 2023 14:27:15 +0300 Subject: [PATCH] classlib: fix delete and replace for AbstractStringBuilder (#783) --- .../java/lang/TAbstractStringBuilder.java | 11 +++- .../classlib/java/lang/StringBuilderTest.java | 53 ++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index 4b261f748..b2bda3d00 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -668,12 +668,15 @@ class TAbstractStringBuilder implements TSerializable, TCharSequence { } public TAbstractStringBuilder delete(int start, int end) { - if (start > end || start > length) { + if (start < 0 || start > end || start > length) { throw new TStringIndexOutOfBoundsException(); } if (start == end) { return this; } + if (end > length) { + end = length; + } int sz = length - end; length -= end - start; 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) { + if (start < 0 || start > end || start > length) { + throw new TStringIndexOutOfBoundsException(); + } + if (end > length) { + end = length; + } int oldSize = end - start; if (str.length() > oldSize) { insertSpace(end, start + str.length()); diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java index dfabd189c..12e7a2d58 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java @@ -16,6 +16,7 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.TeaVMTestRunner; @@ -203,8 +204,8 @@ public class StringBuilderTest { } @Test - public void substringWithUpperBoundAtEndWorks() { - assertEquals("23", "123".substring(1, 3)); + public void substringWorks() { + assertEquals("23", new StringBuilder("123").substring(1, 3)); } @Test @@ -216,4 +217,52 @@ public class StringBuilderTest { assertEquals(0, sb.indexOf("12345")); 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()); + } }