Optimize StringUtils.replace for larger replacement pattern

Issue: SPR-15430
This commit is contained in:
Juergen Hoeller 2017-04-11 11:40:50 +02:00
parent ac27dbea4b
commit ce4eff321c
1 changed files with 7 additions and 1 deletions

View File

@ -414,7 +414,12 @@ public abstract class StringUtils {
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 patLen = oldPattern.length();
while (index >= 0) {
@ -423,6 +428,7 @@ public abstract class StringUtils {
pos = index + patLen;
index = inString.indexOf(oldPattern, pos);
}
// append any characters to the right of a match
sb.append(inString.substring(pos));
return sb.toString();