From 2f6b0b2c69b626d6a669aa8add3223417b3b274e Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Mon, 8 Aug 2005 23:15:04 +0000 Subject: [PATCH] Fix indexOf and lastIndexOf with null input git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230921 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/text/StrBuilder.java | 8 +-- .../commons/lang/text/StrBuilderTest.java | 70 +++++++++++-------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/java/org/apache/commons/lang/text/StrBuilder.java b/src/java/org/apache/commons/lang/text/StrBuilder.java index fba251cf469..13e8a6b2dc8 100644 --- a/src/java/org/apache/commons/lang/text/StrBuilder.java +++ b/src/java/org/apache/commons/lang/text/StrBuilder.java @@ -1456,10 +1456,10 @@ public int indexOf(String str) { */ public int indexOf(String str, int startIndex) { startIndex = (startIndex < 0 ? 0 : startIndex); - if (startIndex >= size) { + if (str == null || startIndex >= size) { return -1; } - int strLen = (str == null ? 0 : str.length()); + int strLen = str.length(); if (strLen > 0 && strLen <= size) { if (strLen == 1) { return indexOf(str.charAt(0), startIndex); @@ -1536,10 +1536,10 @@ public int lastIndexOf(String str) { */ public int lastIndexOf(String str, int startIndex) { startIndex = (startIndex >= size ? size - 1 : startIndex); - if (startIndex < 0) { + if (str == null || startIndex < 0) { return -1; } - int strLen = (str == null ? 0 : str.length()); + int strLen = str.length(); if (strLen > 0 && strLen <= size) { if (strLen == 1) { return lastIndexOf(str.charAt(0), startIndex); diff --git a/src/test/org/apache/commons/lang/text/StrBuilderTest.java b/src/test/org/apache/commons/lang/text/StrBuilderTest.java index 37995a3226a..0474ed6cca9 100644 --- a/src/test/org/apache/commons/lang/text/StrBuilderTest.java +++ b/src/test/org/apache/commons/lang/text/StrBuilderTest.java @@ -1663,89 +1663,97 @@ public void testLastIndexOfCharInt() { public void testIndexOfString() { StrBuilder sb = new StrBuilder("abab"); - assertEquals (0, sb.indexOf("a")); + assertEquals(0, sb.indexOf("a")); //should work like String#indexOf - assertEquals ("abab".indexOf("a"), sb.indexOf("a")); + assertEquals("abab".indexOf("a"), sb.indexOf("a")); - assertEquals (0, sb.indexOf("ab")); + assertEquals(0, sb.indexOf("ab")); //should work like String#indexOf - assertEquals ("abab".indexOf("ab"), sb.indexOf("ab")); + assertEquals("abab".indexOf("ab"), sb.indexOf("ab")); assertEquals(1, sb.indexOf("b")); - assertEquals ("abab".indexOf("b"), sb.indexOf("b")); + assertEquals("abab".indexOf("b"), sb.indexOf("b")); assertEquals(1, sb.indexOf("ba")); - assertEquals ("abab".indexOf("ba"), sb.indexOf("ba")); + assertEquals("abab".indexOf("ba"), sb.indexOf("ba")); - assertEquals (-1, sb.indexOf("z")); + assertEquals(-1, sb.indexOf("z")); + + assertEquals(-1, sb.indexOf((String) null)); } public void testLastIndexOfString() { StrBuilder sb = new StrBuilder("abab"); - assertEquals (2, sb.lastIndexOf("a")); + assertEquals(2, sb.lastIndexOf("a")); //should work like String#lastIndexOf - assertEquals ("abab".lastIndexOf("a"), sb.lastIndexOf("a")); + assertEquals("abab".lastIndexOf("a"), sb.lastIndexOf("a")); - assertEquals (2, sb.lastIndexOf("ab")); + assertEquals(2, sb.lastIndexOf("ab")); //should work like String#lastIndexOf - assertEquals ("abab".lastIndexOf("ab"), sb.lastIndexOf("ab")); + assertEquals("abab".lastIndexOf("ab"), sb.lastIndexOf("ab")); assertEquals(3, sb.lastIndexOf("b")); - assertEquals ("abab".lastIndexOf("b"), sb.lastIndexOf("b")); + assertEquals("abab".lastIndexOf("b"), sb.lastIndexOf("b")); assertEquals(1, sb.lastIndexOf("ba")); - assertEquals ("abab".lastIndexOf("ba"), sb.lastIndexOf("ba")); + assertEquals("abab".lastIndexOf("ba"), sb.lastIndexOf("ba")); + + assertEquals(-1, sb.lastIndexOf("z")); - assertEquals (-1, sb.lastIndexOf("z")); + assertEquals(-1, sb.lastIndexOf((String) null)); } public void testIndexOfStringInt() { StrBuilder sb = new StrBuilder("abab"); - assertEquals (2, sb.indexOf("a", 1)); + assertEquals(2, sb.indexOf("a", 1)); //should work like String#indexOf assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1)); - assertEquals (2, sb.indexOf("ab", 1)); + assertEquals(2, sb.indexOf("ab", 1)); //should work like String#indexOf - assertEquals ("abab".indexOf("ab", 1), sb.indexOf("ab", 1)); + assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1)); assertEquals(3, sb.indexOf("b", 2)); - assertEquals ("abab".indexOf("b", 2), sb.indexOf("b", 2)); + assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2)); assertEquals(1, sb.indexOf("ba", 1)); - assertEquals ("abab".indexOf("ba", 2), sb.indexOf("ba", 2)); + assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2)); - assertEquals (-1, sb.indexOf("z", 2)); + assertEquals(-1, sb.indexOf("z", 2)); sb = new StrBuilder("xyzabc"); - assertEquals (2, sb.indexOf("za", 0)); - assertEquals (-1, sb.indexOf("za", 3)); + assertEquals(2, sb.indexOf("za", 0)); + assertEquals(-1, sb.indexOf("za", 3)); + + assertEquals(-1, sb.indexOf((String) null, 2)); } public void testLastIndexOfStringInt() { StrBuilder sb = new StrBuilder("abab"); - assertEquals (0, sb.lastIndexOf("a", 1)); + assertEquals(0, sb.lastIndexOf("a", 1)); //should work like String#lastIndexOf - assertEquals ("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1)); + assertEquals("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1)); - assertEquals (0, sb.lastIndexOf("ab", 1)); + assertEquals(0, sb.lastIndexOf("ab", 1)); //should work like String#lastIndexOf - assertEquals ("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1)); + assertEquals("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1)); assertEquals(1, sb.lastIndexOf("b", 2)); - assertEquals ("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2)); + assertEquals("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2)); assertEquals(1, sb.lastIndexOf("ba", 2)); - assertEquals ("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2)); + assertEquals("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2)); - assertEquals (-1, sb.lastIndexOf("z", 2)); + assertEquals(-1, sb.lastIndexOf("z", 2)); sb = new StrBuilder("xyzabc"); - assertEquals (2, sb.lastIndexOf("za", sb.length())); - assertEquals (-1, sb.lastIndexOf("za", 1)); + assertEquals(2, sb.lastIndexOf("za", sb.length())); + assertEquals(-1, sb.lastIndexOf("za", 1)); + + assertEquals(-1, sb.lastIndexOf((String) null, 2)); } public void testContainsChar() {