Optimize StringUtils.replace for larger replacement pattern
Issue: SPR-15430
This commit is contained in:
@@ -414,7 +414,12 @@ public abstract class StringUtils {
|
|||||||
return inString;
|
return inString;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(inString.length());
|
int capacity = inString.length();
|
||||||
|
if (newPattern.length() > oldPattern.length()) {
|
||||||
|
capacity += 16;
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder(capacity);
|
||||||
|
|
||||||
int pos = 0; // our position in the old string
|
int pos = 0; // our position in the old string
|
||||||
int patLen = oldPattern.length();
|
int patLen = oldPattern.length();
|
||||||
while (index >= 0) {
|
while (index >= 0) {
|
||||||
@@ -423,6 +428,7 @@ public abstract class StringUtils {
|
|||||||
pos = index + patLen;
|
pos = index + patLen;
|
||||||
index = inString.indexOf(oldPattern, pos);
|
index = inString.indexOf(oldPattern, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// append any characters to the right of a match
|
// append any characters to the right of a match
|
||||||
sb.append(inString.substring(pos));
|
sb.append(inString.substring(pos));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user