LANG-1193 ordinalIndexOf("abc", "ab", 1) gives incorrect answer of -1

(correct answer should be 0)
Revert LANG-1077
This commit is contained in:
Sebb 2015-12-11 13:41:22 +00:00
parent 15e1ea2f4e
commit d75fe46b8f
3 changed files with 28 additions and 6 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1193" type="fix" dev="sebb" due-to="Qin Li">ordinalIndexOf("abc", "ab", 1) gives incorrect answer of -1 (correct answer should be 0); revert fix for LANG-1077</action>
<action issue="LANG-1182" type="update" dev="britter" due-to="Larry West, Pascal Schumacher">Clarify JavaDoc of StringUtils.containsAny()</action>
<action issue="LANG-1169" type="add" dev="lguibert" due-to="Rafal Glowinski, Robert Parr, Arman Sharif">Add StringUtils methods to compare a string to multiple strings</action>
<action issue="LANG-1185" type="add" dev="lguibert">Add remove by regular expression methods in StringUtils</action>
@ -104,7 +105,6 @@
<action issue="LANG-1071" type="update" dev="britter" due-to="Arno Noordover">Fix wrong examples in JavaDoc of StringUtils.replaceEachRepeatedly(...), StringUtils.replaceEach(...)</action>
<action issue="LANG-883" type="add" dev="britter" due-to="Daniel Stewart">Add StringUtils.containsAny(CharSequence, CharSequence...) method</action>
<action issue="LANG-1073" type="fix" dev="kinow" due-to="haiyang li">Read wrong component type of array in add in ArrayUtils</action>
<action issue="LANG-1077" type="fix" dev="kinow" due-to="haiyang li">StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 3 in StringUtils</action>
<action issue="LANG-1072" type="fix" dev="sebb" due-to="haiyang li">Duplicated "0x" check in createBigInteger in NumberUtils</action>
<action issue="LANG-1064" type="fix" dev="djones" due-to="B.J. Herbison">StringUtils.abbreviate description doesn't agree with the examples</action>
<action issue="LANG-1052" type="add" dev="britter" due-to="Jan Matèrne">Multiline recursive to string style</action>

View File

@ -1222,6 +1222,13 @@ public static int indexOf(final CharSequence seq, final CharSequence searchSeq,
* StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0
* </pre>
*
* <p>Matches may overlap:</p>
* <pre>
* StringUtils.ordinalIndexOf("ababab","aba", 1) = 0
* StringUtils.ordinalIndexOf("ababab","aba", 2) = 2
* StringUtils.ordinalIndexOf("ababab","aba", 3) = -1
* </pre>
*
* <p>Note that 'head(CharSequence str, int n)' may be implemented as: </p>
*
* <pre>
@ -1248,7 +1255,7 @@ public static int ordinalIndexOf(final CharSequence str, final CharSequence sear
*
* @param str the CharSequence to check, may be null
* @param searchStr the CharSequence to find, may be null
* @param ordinal the n-th {@code searchStr} to find
* @param ordinal the n-th {@code searchStr} to find, overlapping matches are allowed.
* @param lastIndex true if lastOrdinalIndexOf() otherwise false if ordinalIndexOf()
* @return the n-th index of the search CharSequence,
* {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input
@ -1262,12 +1269,14 @@ private static int ordinalIndexOf(final CharSequence str, final CharSequence sea
return lastIndex ? str.length() : 0;
}
int found = 0;
// set the initial index beyond the end of the string
// this is to allow for the initial index decrement/increment
int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
do {
if (lastIndex) {
index = CharSequenceUtils.lastIndexOf(str, searchStr, index - searchStr.length());
index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1); // step backwards thru string
} else {
index = CharSequenceUtils.indexOf(str, searchStr, index + searchStr.length());
index = CharSequenceUtils.indexOf(str, searchStr, index + 1); // step forwards through string
}
if (index < 0) {
return index;

View File

@ -1148,8 +1148,21 @@ public void testOrdinalIndexOf() {
assertEquals(8, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 9));
assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 10));
assertEquals(3, StringUtils.ordinalIndexOf("aaaaaa", "aa", 2));
assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaa", "aa", 3));
assertEquals(0, StringUtils.ordinalIndexOf("aaaaaa", "aa", 1));
assertEquals(1, StringUtils.ordinalIndexOf("aaaaaa", "aa", 2));
assertEquals(2, StringUtils.ordinalIndexOf("aaaaaa", "aa", 3));
assertEquals(3, StringUtils.ordinalIndexOf("aaaaaa", "aa", 4));
assertEquals(4, StringUtils.ordinalIndexOf("aaaaaa", "aa", 5));
assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaa", "aa", 6));
assertEquals(0, StringUtils.ordinalIndexOf("ababab", "aba", 1));
assertEquals(2, StringUtils.ordinalIndexOf("ababab", "aba", 2));
assertEquals(-1, StringUtils.ordinalIndexOf("ababab", "aba", 3));
}
@Test
public void testLANG1193() {
assertEquals(0, StringUtils.ordinalIndexOf("abc", "ab", 1));
}
}