Avoid temporary String creation in StringUtils.starts/endsWithIgnoreCase
Issue: SPR-16095
This commit is contained in:
@@ -164,7 +164,7 @@ public abstract class StringUtils {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given {@code CharSequence} contains any whitespace characters.
|
||||
@@ -314,7 +314,6 @@ public abstract class StringUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if the given {@code String} starts with the specified prefix,
|
||||
* ignoring upper/lower case.
|
||||
@@ -323,19 +322,8 @@ public abstract class StringUtils {
|
||||
* @see java.lang.String#startsWith
|
||||
*/
|
||||
public static boolean startsWithIgnoreCase(@Nullable String str, @Nullable 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()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -346,19 +334,8 @@ public abstract class StringUtils {
|
||||
* @see java.lang.String#endsWith
|
||||
*/
|
||||
public static boolean endsWithIgnoreCase(@Nullable String str, @Nullable 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()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -369,9 +346,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