From 50f136c874a7f4e18ee14e4737253da4049dfc5d Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 6 Apr 2011 05:31:22 +0000 Subject: [PATCH] Moving countMatches, ordinalIndexOf and lastOrdinalIndexOf over to a CharSequence-based API. LANG-687 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1089305 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang3/StringUtils.java | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index e68a03a52..1ecf25516 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -908,10 +908,10 @@ public class StringUtils { } /** - *

Finds the n-th index within a String, handling {@code null}. - * This method uses {@link String#indexOf(String)}.

+ *

Finds the n-th index within a CharSequence, handling {@code null}. + * This method uses {@link String#indexOf(String)} if possible.

* - *

A {@code null} String will return {@code -1}.

+ *

A {@code null} CharSequence will return {@code -1}.

* *
      * StringUtils.ordinalIndexOf(null, *, *)          = -1
@@ -927,38 +927,38 @@ public class StringUtils {
      * StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0
      * 
* - *

Note that 'head(String str, int n)' may be implemented as:

+ *

Note that 'head(CharSequence str, int n)' may be implemented as:

* *
      *   str.substring(0, lastOrdinalIndexOf(str, "\n", n))
      * 
* - * @param str the String to check, may be null - * @param searchStr the String to find, may be null + * @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 - * @return the n-th index of the search String, + * @return the n-th index of the search CharSequence, * {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input * @since 2.1 */ - public static int ordinalIndexOf(String str, String searchStr, int ordinal) { + public static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal) { return ordinalIndexOf(str, searchStr, ordinal, false); } /** *

Finds the n-th index within a String, handling {@code null}. - * This method uses {@link String#indexOf(String)}.

+ * This method uses {@link String#indexOf(String)} if possible.

* - *

A {@code null} String will return {@code -1}.

+ *

A {@code null} CharSequence will return {@code -1}.

* - * @param str the String to check, may be null - * @param searchStr the String to find, may be null + * @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 lastIndex true if lastOrdinalIndexOf() otherwise false if ordinalIndexOf() - * @return the n-th index of the search String, + * @return the n-th index of the search CharSequence, * {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input */ // Shared code between ordinalIndexOf(String,String,int) and lastOrdinalIndexOf(String,String,int) - private static int ordinalIndexOf(String str, String searchStr, int ordinal, boolean lastIndex) { + private static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal, boolean lastIndex) { if (str == null || searchStr == null || ordinal <= 0) { return INDEX_NOT_FOUND; } @@ -969,9 +969,9 @@ public class StringUtils { int index = lastIndex ? str.length() : INDEX_NOT_FOUND; do { if (lastIndex) { - index = str.lastIndexOf(searchStr, index - 1); + index = lastIndexOfSequence(str, searchStr, index - 1); } else { - index = str.indexOf(searchStr, index + 1); + index = indexOfSequence(str, searchStr, index + 1); } if (index < 0) { return index; @@ -1173,20 +1173,20 @@ public class StringUtils { * StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2) = 8 * * - *

Note that 'tail(String str, int n)' may be implemented as:

+ *

Note that 'tail(CharSequence str, int n)' may be implemented as:

* *
      *   str.substring(lastOrdinalIndexOf(str, "\n", n) + 1)
      * 
* - * @param str the String to check, may be null - * @param searchStr the String to find, may be null + * @param str the CharSequence to check, may be null + * @param searchStr the CharSequence to find, may be null * @param ordinal the n-th last {@code searchStr} to find - * @return the n-th last index of the search String, + * @return the n-th last index of the search CharSequence, * {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input * @since 2.5 */ - public static int lastOrdinalIndexOf(String str, String searchStr, int ordinal) { + public static int lastOrdinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal) { return ordinalIndexOf(str, searchStr, ordinal, true); } @@ -5087,7 +5087,7 @@ public class StringUtils { // Count matches //----------------------------------------------------------------------- /** - *

Counts how many times the substring appears in the larger String.

+ *

Counts how many times the substring appears in the larger string.

* *

A {@code null} or empty ("") String input returns {@code 0}.

* @@ -5101,17 +5101,17 @@ public class StringUtils { * StringUtils.countMatches("abba", "xxx") = 0 * * - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param sub the substring to count, may be null - * @return the number of occurrences, 0 if either String is {@code null} + * @return the number of occurrences, 0 if either CharSequence is {@code null} */ - public static int countMatches(String str, String sub) { + public static int countMatches(CharSequence str, CharSequence sub) { if (isEmpty(str) || isEmpty(sub)) { return 0; } int count = 0; int idx = 0; - while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) { + while ((idx = indexOfSequence(str, sub, idx)) != INDEX_NOT_FOUND) { count++; idx += sub.length(); }