Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/org/apache/commons/text/StrBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,7 @@ public int lastIndexOf(final char ch, int startIndex) {
* @return The last index of the string, or -1 if not found.
*/
public int lastIndexOf(final String str) {
return lastIndexOf(str, size - 1);
return lastIndexOf(str, size);
}

/**
Expand All @@ -2289,14 +2289,17 @@ public int lastIndexOf(final String str) {
* @return The last index of the string, or -1 if not found.
*/
public int lastIndexOf(final String str, int startIndex) {
startIndex = startIndex >= size ? size - 1 : startIndex;
startIndex = Math.min(startIndex, size);
if (str == null || startIndex < 0) {
return StringUtils.INDEX_NOT_FOUND;
}
final int strLen = str.length();
if (strLen == 0) {
return startIndex;
}
if (startIndex >= size) {
startIndex = size - 1;
}
if (strLen > size) {
return StringUtils.INDEX_NOT_FOUND;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/apache/commons/text/TextStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,7 @@ public int lastIndexOf(final char ch, int startIndex) {
* @return The last index of the string, or -1 if not found.
*/
public int lastIndexOf(final String str) {
return lastIndexOf(str, size - 1);
return lastIndexOf(str, size);
}

/**
Expand All @@ -2403,14 +2403,17 @@ public int lastIndexOf(final String str) {
* @return The last index of the string, or -1 if not found.
*/
public int lastIndexOf(final String str, int startIndex) {
startIndex = startIndex >= size ? size - 1 : startIndex;
startIndex = Math.min(startIndex, size);
if (str == null || startIndex < 0) {
return StringUtils.INDEX_NOT_FOUND;
}
final int strLen = str.length();
if (strLen == 0) {
return startIndex;
}
if (startIndex >= size) {
startIndex = size - 1;
}
if (strLen > size) {
return StringUtils.INDEX_NOT_FOUND;
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/apache/commons/text/StrBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*
* @deprecated This class will be removed in 2.0.
*/
@Deprecated
class StrBuilderTest {

private static final class MockReadable implements Readable {
Expand Down Expand Up @@ -1219,6 +1220,9 @@ void testLastIndexOf_String() {
assertEquals(-1, sb.lastIndexOf("z"));

assertEquals(-1, sb.lastIndexOf((String) null));

assertEquals(4, sb.lastIndexOf(""));
assertEquals("".lastIndexOf(""), new StringBuilder().lastIndexOf(""));
Comment thread
garydgregory marked this conversation as resolved.
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,9 @@ void testLastIndexOf_String() {
assertEquals(-1, sb.lastIndexOf("z"));

assertEquals(-1, sb.lastIndexOf((String) null));

assertEquals(4, sb.lastIndexOf(""));
assertEquals("".lastIndexOf(""), new StringBuilder().lastIndexOf(""));
Comment thread
garydgregory marked this conversation as resolved.
}

@Test
Expand Down
Loading