Avoid temporary String creation in StringUtils.starts/endsWithIgnoreCase
Issue: SPR-16095
This commit is contained in:
@@ -135,17 +135,7 @@ public abstract class StringUtils {
|
||||
* @see Character#isWhitespace
|
||||
*/
|
||||
public static boolean hasText(CharSequence str) {
|
||||
if (!hasLength(str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int strLen = str.length();
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (!Character.isWhitespace(str.charAt(i))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return (hasLength(str) && containsText(str));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,7 +149,17 @@ public abstract class StringUtils {
|
||||
* @see #hasText(CharSequence)
|
||||
*/
|
||||
public static boolean hasText(String str) {
|
||||
return (str != null && !str.isEmpty() && hasText((CharSequence) str));
|
||||
return (hasLength(str) && containsText(str));
|
||||
}
|
||||
|
||||
private static boolean containsText(CharSequence str) {
|
||||
int strLen = str.length();
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (!Character.isWhitespace(str.charAt(i))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -310,7 +310,6 @@ public abstract class StringUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if the given {@code String} starts with the specified prefix,
|
||||
* ignoring upper/lower case.
|
||||
@@ -319,19 +318,8 @@ public abstract class StringUtils {
|
||||
* @see java.lang.String#startsWith
|
||||
*/
|
||||
public static boolean startsWithIgnoreCase(String str, String prefix) {
|
||||
if (str == null || prefix == null) {
|
||||
return false;
|
||||
}
|
||||
if (str.startsWith(prefix)) {
|
||||
return true;
|
||||
}
|
||||
if (str.length() < prefix.length()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String lcStr = str.substring(0, prefix.length()).toLowerCase();
|
||||
String lcPrefix = prefix.toLowerCase();
|
||||
return lcStr.equals(lcPrefix);
|
||||
return (str != null && prefix != null && str.length() >= prefix.length() &&
|
||||
str.regionMatches(true, 0, prefix, 0, prefix.length()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -342,19 +330,8 @@ public abstract class StringUtils {
|
||||
* @see java.lang.String#endsWith
|
||||
*/
|
||||
public static boolean endsWithIgnoreCase(String str, String suffix) {
|
||||
if (str == null || suffix == null) {
|
||||
return false;
|
||||
}
|
||||
if (str.endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
if (str.length() < suffix.length()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String lcStr = str.substring(str.length() - suffix.length()).toLowerCase();
|
||||
String lcSuffix = suffix.toLowerCase();
|
||||
return lcStr.equals(lcSuffix);
|
||||
return (str != null && suffix != null && str.length() >= suffix.length() &&
|
||||
str.regionMatches(true, str.length() - suffix.length(), suffix, 0, suffix.length()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -365,9 +342,11 @@ public abstract class StringUtils {
|
||||
* @param substring the substring to match at the given index
|
||||
*/
|
||||
public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
|
||||
for (int j = 0; j < substring.length(); j++) {
|
||||
int i = index + j;
|
||||
if (i >= str.length() || str.charAt(i) != substring.charAt(j)) {
|
||||
if (index + substring.length() > str.length()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < substring.length(); i++) {
|
||||
if (str.charAt(index + i) != substring.charAt(i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user