Use INDEX_NOT_FOUND instead of magic number.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@920546 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2010-03-08 22:22:31 +00:00
parent 7f23ec3d90
commit 655050f6fd
1 changed files with 49 additions and 49 deletions

View File

@ -497,7 +497,7 @@ public class StringUtils {
} else if (stripChars.length() == 0) {
return str;
} else {
while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {
while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != INDEX_NOT_FOUND)) {
start++;
}
}
@ -541,7 +541,7 @@ public class StringUtils {
} else if (stripChars.length() == 0) {
return str;
} else {
while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND)) {
end--;
}
}
@ -731,7 +731,7 @@ public class StringUtils {
* <p>Finds the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(int)}.</p>
*
* <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p>
* <p>A <code>null</code> or empty ("") String will return <code>INDEX_NOT_FOUND (-1)</code>.</p>
*
* <pre>
* StringUtils.indexOf(null, *) = -1
@ -748,7 +748,7 @@ public class StringUtils {
*/
public static int indexOf(String str, char searchChar) {
if (isEmpty(str)) {
return -1;
return INDEX_NOT_FOUND;
}
return str.indexOf(searchChar);
}
@ -758,7 +758,7 @@ public class StringUtils {
* handling <code>null</code>.
* This method uses {@link String#indexOf(int, int)}.</p>
*
* <p>A <code>null</code> or empty ("") String will return <code>-1</code>.
* <p>A <code>null</code> or empty ("") String will return <code>(INDEX_NOT_FOUND) -1</code>.
* A negative start position is treated as zero.
* A start position greater than the string length returns <code>-1</code>.</p>
*
@ -780,7 +780,7 @@ public class StringUtils {
*/
public static int indexOf(String str, char searchChar, int startPos) {
if (isEmpty(str)) {
return -1;
return INDEX_NOT_FOUND;
}
return str.indexOf(searchChar, startPos);
}
@ -810,7 +810,7 @@ public class StringUtils {
*/
public static int indexOf(String str, String searchStr) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
return str.indexOf(searchStr);
}
@ -849,7 +849,7 @@ public class StringUtils {
*/
public static int indexOf(String str, String searchStr, int startPos) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
return str.indexOf(searchStr, startPos);
}
@ -989,14 +989,14 @@ public class StringUtils {
*/
public static int indexOfIgnoreCase(String str, String searchStr, int startPos) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
if (startPos < 0) {
startPos = 0;
}
int endLimit = (str.length() - searchStr.length()) + 1;
if (startPos > endLimit) {
return -1;
return INDEX_NOT_FOUND;
}
if (searchStr.length() == 0) {
return startPos;
@ -1006,7 +1006,7 @@ public class StringUtils {
return i;
}
}
return -1;
return INDEX_NOT_FOUND;
}
// LastIndexOf
@ -1032,7 +1032,7 @@ public class StringUtils {
*/
public static int lastIndexOf(String str, char searchChar) {
if (isEmpty(str)) {
return -1;
return INDEX_NOT_FOUND;
}
return str.lastIndexOf(searchChar);
}
@ -1066,7 +1066,7 @@ public class StringUtils {
*/
public static int lastIndexOf(String str, char searchChar, int startPos) {
if (isEmpty(str)) {
return -1;
return INDEX_NOT_FOUND;
}
return str.lastIndexOf(searchChar, startPos);
}
@ -1095,7 +1095,7 @@ public class StringUtils {
*/
public static int lastIndexOf(String str, String searchStr) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
return str.lastIndexOf(searchStr);
}
@ -1167,7 +1167,7 @@ public class StringUtils {
*/
public static int lastIndexOf(String str, String searchStr, int startPos) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
return str.lastIndexOf(searchStr, startPos);
}
@ -1196,7 +1196,7 @@ public class StringUtils {
*/
public static int lastIndexOfIgnoreCase(String str, String searchStr) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
return lastIndexOfIgnoreCase(str, searchStr, str.length());
}
@ -1231,13 +1231,13 @@ public class StringUtils {
*/
public static int lastIndexOfIgnoreCase(String str, String searchStr, int startPos) {
if (str == null || searchStr == null) {
return -1;
return INDEX_NOT_FOUND;
}
if (startPos > (str.length() - searchStr.length())) {
startPos = str.length() - searchStr.length();
}
if (startPos < 0) {
return -1;
return INDEX_NOT_FOUND;
}
if (searchStr.length() == 0) {
return startPos;
@ -1248,7 +1248,7 @@ public class StringUtils {
return i;
}
}
return -1;
return INDEX_NOT_FOUND;
}
// Contains
@ -1370,7 +1370,7 @@ public class StringUtils {
*/
public static int indexOfAny(CharSequence cs, char[] searchChars) {
if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) {
return -1;
return INDEX_NOT_FOUND;
}
for (int i = 0; i < cs.length(); i++) {
char ch = cs.charAt(i);
@ -1380,7 +1380,7 @@ public class StringUtils {
}
}
}
return -1;
return INDEX_NOT_FOUND;
}
/**
@ -1407,7 +1407,7 @@ public class StringUtils {
*/
public static int indexOfAny(CharSequence cs, String searchChars) {
if (isEmpty(cs) || isEmpty(searchChars)) {
return -1;
return INDEX_NOT_FOUND;
}
return indexOfAny(cs, searchChars.toCharArray());
}
@ -1512,7 +1512,7 @@ public class StringUtils {
*/
public static int indexOfAnyBut(CharSequence cs, char[] searchChars) {
if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) {
return -1;
return INDEX_NOT_FOUND;
}
outer : for (int i = 0; i < cs.length(); i++) {
char ch = cs.charAt(i);
@ -1523,7 +1523,7 @@ public class StringUtils {
}
return i;
}
return -1;
return INDEX_NOT_FOUND;
}
/**
@ -1550,14 +1550,14 @@ public class StringUtils {
*/
public static int indexOfAnyBut(String str, String searchChars) {
if (isEmpty(str) || isEmpty(searchChars)) {
return -1;
return INDEX_NOT_FOUND;
}
for (int i = 0; i < str.length(); i++) {
if (searchChars.indexOf(str.charAt(i)) < 0) {
return i;
}
}
return -1;
return INDEX_NOT_FOUND;
}
// ContainsOnly
@ -1594,7 +1594,7 @@ public class StringUtils {
if (valid.length == 0) {
return false;
}
return indexOfAnyBut(cs, valid) == -1;
return indexOfAnyBut(cs, valid) == INDEX_NOT_FOUND;
}
/**
@ -1726,7 +1726,7 @@ public class StringUtils {
*/
public static int indexOfAny(String str, String[] searchStrs) {
if (str == null || searchStrs == null) {
return -1;
return INDEX_NOT_FOUND;
}
int sz = searchStrs.length;
@ -1740,7 +1740,7 @@ public class StringUtils {
continue;
}
tmp = str.indexOf(search);
if (tmp == -1) {
if (tmp == INDEX_NOT_FOUND) {
continue;
}
@ -1749,7 +1749,7 @@ public class StringUtils {
}
}
return (ret == Integer.MAX_VALUE) ? -1 : ret;
return (ret == Integer.MAX_VALUE) ? INDEX_NOT_FOUND : ret;
}
/**
@ -1778,11 +1778,11 @@ public class StringUtils {
* @return the last index of any of the Strings, -1 if no match
*/
public static int lastIndexOfAny(String str, String[] searchStrs) {
if ((str == null) || (searchStrs == null)) {
return -1;
if (str == null || searchStrs == null) {
return INDEX_NOT_FOUND;
}
int sz = searchStrs.length;
int ret = -1;
int ret = INDEX_NOT_FOUND;
int tmp = 0;
for (int i = 0; i < sz; i++) {
String search = searchStrs[i];
@ -2056,7 +2056,7 @@ public class StringUtils {
return EMPTY;
}
int pos = str.indexOf(separator);
if (pos == -1) {
if (pos == INDEX_NOT_FOUND) {
return str;
}
return str.substring(0, pos);
@ -2098,7 +2098,7 @@ public class StringUtils {
return EMPTY;
}
int pos = str.indexOf(separator);
if (pos == -1) {
if (pos == INDEX_NOT_FOUND) {
return EMPTY;
}
return str.substring(pos + separator.length());
@ -2136,7 +2136,7 @@ public class StringUtils {
return str;
}
int pos = str.lastIndexOf(separator);
if (pos == -1) {
if (pos == INDEX_NOT_FOUND) {
return str;
}
return str.substring(0, pos);
@ -2179,7 +2179,7 @@ public class StringUtils {
return EMPTY;
}
int pos = str.lastIndexOf(separator);
if (pos == -1 || pos == (str.length() - separator.length())) {
if (pos == INDEX_NOT_FOUND || pos == (str.length() - separator.length())) {
return EMPTY;
}
return str.substring(pos + separator.length());
@ -2244,9 +2244,9 @@ public class StringUtils {
return null;
}
int start = str.indexOf(open);
if (start != -1) {
if (start != INDEX_NOT_FOUND) {
int end = str.indexOf(close, start + open.length());
if (end != -1) {
if (end != INDEX_NOT_FOUND) {
return str.substring(start + open.length(), end);
}
}
@ -3564,7 +3564,7 @@ public class StringUtils {
* @since 2.1
*/
public static String remove(String str, char remove) {
if (isEmpty(str) || str.indexOf(remove) == -1) {
if (isEmpty(str) || str.indexOf(remove) == INDEX_NOT_FOUND) {
return str;
}
char[] chars = str.toCharArray();
@ -3667,7 +3667,7 @@ public class StringUtils {
}
int start = 0;
int end = text.indexOf(searchString, start);
if (end == -1) {
if (end == INDEX_NOT_FOUND) {
return text;
}
int replLength = searchString.length();
@ -3675,7 +3675,7 @@ public class StringUtils {
increase = (increase < 0 ? 0 : increase);
increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
StringBuilder buf = new StringBuilder(text.length() + increase);
while (end != -1) {
while (end != INDEX_NOT_FOUND) {
buf.append(text.substring(start, end)).append(replacement);
start = end + replLength;
if (--max == 0) {
@ -4975,7 +4975,7 @@ public class StringUtils {
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
count++;
idx += sub.length();
}
@ -5621,7 +5621,7 @@ public class StringUtils {
return str1;
}
int at = indexOfDifference(str1, str2);
if (at == -1) {
if (at == INDEX_NOT_FOUND) {
return EMPTY;
}
return str2.substring(at);
@ -5652,7 +5652,7 @@ public class StringUtils {
*/
public static int indexOfDifference(CharSequence cs1, CharSequence cs2) {
if (cs1 == cs2) {
return -1;
return INDEX_NOT_FOUND;
}
if (cs1 == null || cs2 == null) {
return 0;
@ -5666,7 +5666,7 @@ public class StringUtils {
if (i < cs2.length() || i < cs1.length()) {
return i;
}
return -1;
return INDEX_NOT_FOUND;
}
/**
@ -5702,7 +5702,7 @@ public class StringUtils {
*/
public static int indexOfDifference(CharSequence[] css) {
if (css == null || css.length <= 1) {
return -1;
return INDEX_NOT_FOUND;
}
boolean anyStringNull = false;
boolean allStringsNull = true;
@ -5726,7 +5726,7 @@ public class StringUtils {
// handle lists containing all nulls or all empty strings
if (allStringsNull || (longestStrLen == 0 && !anyStringNull)) {
return -1;
return INDEX_NOT_FOUND;
}
// handle lists containing some nulls or some empty strings
@ -5796,7 +5796,7 @@ public class StringUtils {
return EMPTY;
}
int smallestIndexOfDiff = indexOfDifference(strs);
if (smallestIndexOfDiff == -1) {
if (smallestIndexOfDiff == INDEX_NOT_FOUND) {
// all strings were identical
if (strs[0] == null) {
return EMPTY;