Fixes and better unit test code coverage from Nathan Beyer [nbeyer@kc.rr.com].
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@226934 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
93b5f2a7ac
commit
ee5b770b08
|
@ -1383,7 +1383,7 @@ public class StrBuilder implements Cloneable {
|
|||
* @return the last index of the character, or -1 if not found
|
||||
*/
|
||||
public int lastIndexOf(char ch) {
|
||||
return lastIndexOf(ch, 0);
|
||||
return lastIndexOf(ch, size - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1398,9 +1398,8 @@ public class StrBuilder implements Cloneable {
|
|||
if (startIndex < 0) {
|
||||
return -1;
|
||||
}
|
||||
char[] thisBuf = buf;
|
||||
for (int i = startIndex; i >= 0; i--) {
|
||||
if (thisBuf[i] == ch) {
|
||||
if (buf[i] == ch) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
@ -1416,7 +1415,7 @@ public class StrBuilder implements Cloneable {
|
|||
* @return the last index of the string, or -1 if not found
|
||||
*/
|
||||
public int lastIndexOf(String str) {
|
||||
return lastIndexOf(str, size);
|
||||
return lastIndexOf(str, size - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1439,11 +1438,11 @@ public class StrBuilder implements Cloneable {
|
|||
if (strLen == 1) {
|
||||
return lastIndexOf(str.charAt(0), startIndex);
|
||||
}
|
||||
char[] thisBuf = buf;
|
||||
|
||||
outer:
|
||||
for (int i = startIndex - strLen; i >= 0; i--) {
|
||||
for (int i = startIndex - strLen + 1; i >= 0; i--) {
|
||||
for (int j = 0; j < strLen; j++) {
|
||||
if (str.charAt(j) != thisBuf[i + j]) {
|
||||
if (str.charAt(j) != buf[i + j]) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1345,4 +1345,152 @@ public class StrBuilderTest extends TestCase {
|
|||
actual = sb.reverse().toString();
|
||||
assertEquals("true", actual);
|
||||
}
|
||||
|
||||
public void testIndexOfChar() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (0, sb.indexOf('a'));
|
||||
//should work like String#indexOf
|
||||
assertEquals ("abab".indexOf('a'), sb.indexOf('a'));
|
||||
|
||||
assertEquals(1, sb.indexOf('b'));
|
||||
assertEquals ("abab".indexOf('b'), sb.indexOf('b'));
|
||||
|
||||
assertEquals (-1, sb.indexOf('z'));
|
||||
}
|
||||
|
||||
public void testLastIndexOfChar() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (2, sb.lastIndexOf('a'));
|
||||
//should work like String#lastIndexOf
|
||||
assertEquals ("abab".lastIndexOf('a'), sb.lastIndexOf('a'));
|
||||
|
||||
assertEquals(3, sb.lastIndexOf('b'));
|
||||
assertEquals ("abab".lastIndexOf('b'), sb.lastIndexOf('b'));
|
||||
|
||||
assertEquals (-1, sb.lastIndexOf('z'));
|
||||
}
|
||||
|
||||
public void testIndexOfCharInt() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (2, sb.indexOf('a', 1));
|
||||
//should work like String#indexOf
|
||||
assertEquals ("abab".indexOf('a', 1), sb.indexOf('a', 1));
|
||||
|
||||
assertEquals(3, sb.indexOf('b', 2));
|
||||
assertEquals ("abab".indexOf('b', 2), sb.indexOf('b', 2));
|
||||
|
||||
assertEquals (-1, sb.indexOf('z', 2));
|
||||
|
||||
sb = new StrBuilder("xyzabc");
|
||||
assertEquals (2, sb.indexOf('z', 0));
|
||||
assertEquals (-1, sb.indexOf('z', 3));
|
||||
}
|
||||
|
||||
public void testLastIndexOfCharInt() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (0, sb.lastIndexOf('a', 1));
|
||||
//should work like String#lastIndexOf
|
||||
assertEquals ("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1));
|
||||
|
||||
assertEquals(1, sb.lastIndexOf('b', 2));
|
||||
assertEquals ("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2));
|
||||
|
||||
assertEquals (-1, sb.lastIndexOf('z', 2));
|
||||
|
||||
sb = new StrBuilder("xyzabc");
|
||||
assertEquals (2, sb.lastIndexOf('z', sb.length()));
|
||||
assertEquals (-1, sb.lastIndexOf('z', 1));
|
||||
}
|
||||
|
||||
public void testIndexOfString() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (0, sb.indexOf("a"));
|
||||
//should work like String#indexOf
|
||||
assertEquals ("abab".indexOf("a"), sb.indexOf("a"));
|
||||
|
||||
assertEquals (0, sb.indexOf("ab"));
|
||||
//should work like String#indexOf
|
||||
assertEquals ("abab".indexOf("ab"), sb.indexOf("ab"));
|
||||
|
||||
assertEquals(1, sb.indexOf("b"));
|
||||
assertEquals ("abab".indexOf("b"), sb.indexOf("b"));
|
||||
|
||||
assertEquals(1, sb.indexOf("ba"));
|
||||
assertEquals ("abab".indexOf("ba"), sb.indexOf("ba"));
|
||||
|
||||
assertEquals (-1, sb.indexOf("z"));
|
||||
}
|
||||
|
||||
public void testLastIndexOfString() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (2, sb.lastIndexOf("a"));
|
||||
//should work like String#lastIndexOf
|
||||
assertEquals ("abab".lastIndexOf("a"), sb.lastIndexOf("a"));
|
||||
|
||||
assertEquals (2, sb.lastIndexOf("ab"));
|
||||
//should work like String#lastIndexOf
|
||||
assertEquals ("abab".lastIndexOf("ab"), sb.lastIndexOf("ab"));
|
||||
|
||||
assertEquals(3, sb.lastIndexOf("b"));
|
||||
assertEquals ("abab".lastIndexOf("b"), sb.lastIndexOf("b"));
|
||||
|
||||
assertEquals(1, sb.lastIndexOf("ba"));
|
||||
assertEquals ("abab".lastIndexOf("ba"), sb.lastIndexOf("ba"));
|
||||
|
||||
assertEquals (-1, sb.lastIndexOf("z"));
|
||||
}
|
||||
|
||||
public void testIndexOfStringInt() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
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));
|
||||
//should work like String#indexOf
|
||||
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(1, sb.indexOf("ba", 1));
|
||||
assertEquals ("abab".indexOf("ba", 2), sb.indexOf("ba", 2));
|
||||
|
||||
assertEquals (-1, sb.indexOf("z", 2));
|
||||
|
||||
sb = new StrBuilder("xyzabc");
|
||||
assertEquals (2, sb.indexOf("za", 0));
|
||||
assertEquals (-1, sb.indexOf("za", 3));
|
||||
}
|
||||
|
||||
public void testLastIndexOfStringInt() {
|
||||
StrBuilder sb = new StrBuilder("abab");
|
||||
|
||||
assertEquals (0, sb.lastIndexOf("a", 1));
|
||||
//should work like String#lastIndexOf
|
||||
assertEquals ("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1));
|
||||
|
||||
assertEquals (0, sb.lastIndexOf("ab", 1));
|
||||
//should work like String#lastIndexOf
|
||||
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(1, sb.lastIndexOf("ba", 2));
|
||||
assertEquals ("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2));
|
||||
|
||||
assertEquals (-1, sb.lastIndexOf("z", 2));
|
||||
|
||||
sb = new StrBuilder("xyzabc");
|
||||
assertEquals (2, sb.lastIndexOf("za", sb.length()));
|
||||
assertEquals (-1, sb.lastIndexOf("za", 1));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue