Skip to content

Commit 9fa1d73

Browse files
LANG-1631 - Check if the char to be searched is defined (#682)
1 parent 45bec21 commit 9fa1d73

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/main/java/org/apache/commons/lang3/CharSequenceUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static int indexOf(final CharSequence cs, final int searchChar, int start) {
109109
return i;
110110
}
111111
}
112+
return NOT_FOUND;
112113
}
113114
//supplementary characters (LANG1300)
114115
if (searchChar <= Character.MAX_CODE_POINT) {
@@ -195,6 +196,7 @@ static int lastIndexOf(final CharSequence cs, final int searchChar, int start) {
195196
return i;
196197
}
197198
}
199+
return NOT_FOUND;
198200
}
199201
//supplementary characters (LANG1300)
200202
//NOTE - we must do a forward traversal for this to avoid duplicating code points
@@ -244,7 +246,7 @@ static int lastIndexOf(final CharSequence cs, final CharSequence searchChar, int
244246
}
245247

246248
if (start < 0 || len2 < 0 || len2 > len1) {
247-
return -1;
249+
return NOT_FOUND;
248250
}
249251

250252
if (len2 == 0) {
@@ -272,15 +274,15 @@ static int lastIndexOf(final CharSequence cs, final CharSequence searchChar, int
272274
while (cs.charAt(i) != char0) {
273275
i--;
274276
if (i < 0) {
275-
return -1;
277+
return NOT_FOUND;
276278
}
277279
}
278280
if (checkLaterThan1(cs, searchChar, len2, i)) {
279281
return i;
280282
}
281283
i--;
282284
if (i < 0) {
283-
return -1;
285+
return NOT_FOUND;
284286
}
285287
}
286288
}

src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
2323

24+
import java.nio.CharBuffer;
2425
import java.util.Locale;
2526

2627
import org.hamcrest.core.IsNot;
@@ -275,6 +276,7 @@ public void testIndexOf_char() {
275276
assertEquals(2, StringUtils.indexOf("aabaabaa", 'b'));
276277

277278
assertEquals(2, StringUtils.indexOf(new StringBuilder("aabaabaa"), 'b'));
279+
assertEquals(StringUtils.INDEX_NOT_FOUND, StringUtils.indexOf(new StringBuilder("aabaabaa"), -1738));
278280
}
279281

280282
@Test
@@ -564,6 +566,7 @@ public void testLastIndexOf_charInt() {
564566
assertEquals(1, StringUtils.lastIndexOf(builder, CODE_POINT, 1 ));
565567
assertEquals(-1, StringUtils.lastIndexOf(builder.toString(), CODE_POINT, 0));
566568
assertEquals(1, StringUtils.lastIndexOf(builder.toString(), CODE_POINT, 1));
569+
assertEquals(StringUtils.INDEX_NOT_FOUND, StringUtils.lastIndexOf(CharBuffer.wrap("[%{.c.0rro"), -1738, 982));
567570
}
568571

569572
@Test

0 commit comments

Comments
 (0)