Added ordinalIndexOf() and associated unit tests.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137663 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2003-09-04 18:18:18 +00:00
parent b586a8123d
commit 2546311b27
2 changed files with 205 additions and 95 deletions

View File

@ -145,7 +145,7 @@ import java.util.List;
* @author Gary Gregory * @author Gary Gregory
* @author Phil Steitz * @author Phil Steitz
* @since 1.0 * @since 1.0
* @version $Id: StringUtils.java,v 1.107 2003/08/23 10:39:20 scolebourne Exp $ * @version $Id: StringUtils.java,v 1.108 2003/09/04 18:18:18 ggregory Exp $
*/ */
public class StringUtils { public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne) // Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -170,6 +170,12 @@ public class StringUtils {
*/ */
public static final String EMPTY = ""; public static final String EMPTY = "";
/**
* Represents a failed index search.
* @since 2.?.?
*/
public static final int INDEX_NOT_FOUND = -1;
/** /**
* <p>The maximum size to which the padding constant(s) can expand.</p> * <p>The maximum size to which the padding constant(s) can expand.</p>
*/ */
@ -810,6 +816,52 @@ public class StringUtils {
return str.indexOf(searchStr); return str.indexOf(searchStr);
} }
/**
* <p>Finds the n-th index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.ordinalIndexOf(null, *, *) = -1
* StringUtils.ordinalIndexOf(*, null, *) = -1
* StringUtils.ordinalIndexOf("", "", *) = 0
* StringUtils.ordinalIndexOf("aabaabaa", "a", 1) = 0
* StringUtils.ordinalIndexOf("aabaabaa", "a", 2) = 1
* StringUtils.ordinalIndexOf("aabaabaa", "b", 1) = 2
* StringUtils.ordinalIndexOf("aabaabaa", "b", 2) = 5
* StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1
* StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4
* StringUtils.ordinalIndexOf("aabaabaa", "", 1) = 0
* StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0
* </pre>
*
* @param str the String to check, may be null
* @param searchStr the String to find, may be null
* @param ordinal the n-th <code>searchStr</code> to find
* @return the n-th index of the search String,
* <code>-1</code> (<code>INDEX_NOT_FOUND</code>) if no match or <code>null</code> string input
* @since 2.?.?
*/
public static int ordinalIndexOf(String str, String searchStr, int ordinal) {
if (str == null || searchStr == null || ordinal <= 0) {
return INDEX_NOT_FOUND;
}
if (searchStr.length() == 0) {
return 0;
}
int found = 0;
int index = INDEX_NOT_FOUND;
do {
index = str.indexOf(searchStr, index + 1);
if (index < 0) {
return index;
}
found++;
} while (found < ordinal);
return index;
}
/** /**
* <p>Finds the first index within a String, handling <code>null</code>. * <p>Finds the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String, int)}.</p> * This method uses {@link String#indexOf(String, int)}.</p>
@ -2256,9 +2308,11 @@ public class StringUtils {
// ArraySize == 0: Len = 0 // ArraySize == 0: Len = 0
// ArraySize > 0: Len = NofStrings *(len(firstString) + len(separator)) // ArraySize > 0: Len = NofStrings *(len(firstString) + len(separator))
// (Assuming that all Strings are roughly equally long) // (Assuming that all Strings are roughly equally long)
int bufSize int bufSize =
= ((arraySize == 0) ? 0 ((arraySize == 0)
: arraySize * ((array[0] == null ? 16 : array[0].toString().length()) ? 0
: arraySize
* ((array[0] == null ? 16 : array[0].toString().length())
+ ((separator != null) ? separator.length() : 0))); + ((separator != null) ? separator.length() : 0)));
StringBuffer buf = new StringBuffer(bufSize); StringBuffer buf = new StringBuffer(bufSize);
@ -2956,7 +3010,6 @@ public class StringUtils {
return str.substring(0, lastIdx); return str.substring(0, lastIdx);
} }
// Conversion // Conversion
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -3697,8 +3750,7 @@ public class StringUtils {
} }
int sz = str.length(); int sz = str.length();
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
if ((Character.isLetter(str.charAt(i)) == false) && if ((Character.isLetter(str.charAt(i)) == false) && (str.charAt(i) != ' ')) {
(str.charAt(i) != ' ')) {
return false; return false;
} }
} }
@ -3765,8 +3817,7 @@ public class StringUtils {
} }
int sz = str.length(); int sz = str.length();
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
if ((Character.isLetterOrDigit(str.charAt(i)) == false) && if ((Character.isLetterOrDigit(str.charAt(i)) == false) && (str.charAt(i) != ' ')) {
(str.charAt(i) != ' ')) {
return false; return false;
} }
} }
@ -3836,8 +3887,7 @@ public class StringUtils {
} }
int sz = str.length(); int sz = str.length();
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
if ((Character.isDigit(str.charAt(i)) == false) && if ((Character.isDigit(str.charAt(i)) == false) && (str.charAt(i) != ' ')) {
(str.charAt(i) != ' ')) {
return false; return false;
} }
} }
@ -4192,7 +4242,6 @@ public class StringUtils {
return -1; return -1;
} }
// Misc // Misc
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -4301,4 +4350,3 @@ public class StringUtils {
} }
} }

View File

@ -64,7 +64,7 @@ import junit.textui.TestRunner;
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a> * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @author Phil Steitz * @author Phil Steitz
* @version $Id: StringUtilsEqualsIndexOfTest.java,v 1.8 2003/08/18 02:22:25 bayard Exp $ * @version $Id: StringUtilsEqualsIndexOfTest.java,v 1.9 2003/09/04 18:18:18 ggregory Exp $
*/ */
public class StringUtilsEqualsIndexOfTest extends TestCase { public class StringUtilsEqualsIndexOfTest extends TestCase {
private static final String FOO = "foo"; private static final String FOO = "foo";
@ -146,6 +146,68 @@ public class StringUtilsEqualsIndexOfTest extends TestCase {
assertEquals(0, StringUtils.indexOf("aabaabaa", "")); assertEquals(0, StringUtils.indexOf("aabaabaa", ""));
} }
public void testOrdinalIndexOf() {
assertEquals(-1, StringUtils.ordinalIndexOf(null, null, Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("", null, Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("", "", Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "a", Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "b", Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "ab", Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "", Integer.MIN_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf(null, null, -1));
assertEquals(-1, StringUtils.ordinalIndexOf("", null, -1));
assertEquals(-1, StringUtils.ordinalIndexOf("", "", -1));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "a", -1));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "b", -1));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "ab", -1));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "", -1));
assertEquals(-1, StringUtils.ordinalIndexOf(null, null, 0));
assertEquals(-1, StringUtils.ordinalIndexOf("", null, 0));
assertEquals(-1, StringUtils.ordinalIndexOf("", "", 0));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "a", 0));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "b", 0));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "ab", 0));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "", 0));
assertEquals(-1, StringUtils.ordinalIndexOf(null, null, 1));
assertEquals(-1, StringUtils.ordinalIndexOf("", null, 1));
assertEquals(0, StringUtils.ordinalIndexOf("", "", 1));
assertEquals(0, StringUtils.ordinalIndexOf("aabaabaa", "a", 1));
assertEquals(2, StringUtils.ordinalIndexOf("aabaabaa", "b", 1));
assertEquals(1, StringUtils.ordinalIndexOf("aabaabaa", "ab", 1));
assertEquals(0, StringUtils.ordinalIndexOf("aabaabaa", "", 1));
assertEquals(-1, StringUtils.ordinalIndexOf(null, null, 2));
assertEquals(-1, StringUtils.ordinalIndexOf("", null, 2));
assertEquals(0, StringUtils.ordinalIndexOf("", "", 2));
assertEquals(1, StringUtils.ordinalIndexOf("aabaabaa", "a", 2));
assertEquals(5, StringUtils.ordinalIndexOf("aabaabaa", "b", 2));
assertEquals(4, StringUtils.ordinalIndexOf("aabaabaa", "ab", 2));
assertEquals(0, StringUtils.ordinalIndexOf("aabaabaa", "", 2));
assertEquals(-1, StringUtils.ordinalIndexOf(null, null, Integer.MAX_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("", null, Integer.MAX_VALUE));
assertEquals(0, StringUtils.ordinalIndexOf("", "", Integer.MAX_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "a", Integer.MAX_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "b", Integer.MAX_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aabaabaa", "ab", Integer.MAX_VALUE));
assertEquals(0, StringUtils.ordinalIndexOf("aabaabaa", "", Integer.MAX_VALUE));
assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 0));
assertEquals(0, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 1));
assertEquals(1, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 2));
assertEquals(2, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 3));
assertEquals(3, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 4));
assertEquals(4, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 5));
assertEquals(5, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 6));
assertEquals(6, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 7));
assertEquals(7, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 8));
assertEquals(8, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 9));
assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 10));
}
public void testIndexOf_StringInt() { public void testIndexOf_StringInt() {
assertEquals(-1, StringUtils.indexOf(null, null, 0)); assertEquals(-1, StringUtils.indexOf(null, null, 0));
assertEquals(-1, StringUtils.indexOf(null, null, -1)); assertEquals(-1, StringUtils.indexOf(null, null, -1));