From a40c955d39e659b2884ce00c15cd8d7785e39a93 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 5 Oct 2022 10:16:54 +0200 Subject: [PATCH] classlib: fix StringBuilder.indexOf Fix #615 --- .../classlib/java/lang/TAbstractStringBuilder.java | 2 +- .../teavm/classlib/java/lang/StringBuilderTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 1752ccd70..4fee5f51c 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 @@ -674,7 +674,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ public int indexOf(TString str, int fromIndex) { int sz = length - str.length(); - outer: for (int i = fromIndex; i < sz; ++i) { + outer: for (int i = fromIndex; i <= sz; ++i) { for (int j = 0; j < str.length(); ++j) { if (buffer[i + j] != str.charAt(j)) { continue outer; 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 dc95c9f9f..aab08bacf 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 @@ -407,4 +407,14 @@ public class StringBuilderTest { public void substringWithUpperBoundAtEndWorks() { assertEquals("23", "123".substring(1, 3)); } + + @Test + public void indexOf() { + StringBuilder sb = new StringBuilder(); + sb.append("12345"); + assertEquals(3, sb.indexOf("45")); + assertEquals(-1, sb.indexOf("56")); + assertEquals(0, sb.indexOf("12345")); + assertEquals(0, sb.indexOf("123")); + } }