Optimised StringUtils#replaceQuickly
This commit is contained in:
@@ -747,13 +747,7 @@ public final class StringUtils {
|
||||
public static String replaceQuickly(@NotNull final String input,
|
||||
@NotNull final String target,
|
||||
@NotNull final String replacement) {
|
||||
if (input.isEmpty()) {
|
||||
return input;
|
||||
}
|
||||
|
||||
int targetLength = target.length();
|
||||
int replacementLength = replacement.length();
|
||||
int inputLength = input.length();
|
||||
|
||||
// Count the number of original occurrences
|
||||
int count = 0;
|
||||
@@ -769,6 +763,9 @@ public final class StringUtils {
|
||||
return input;
|
||||
}
|
||||
|
||||
int replacementLength = replacement.length();
|
||||
int inputLength = input.length();
|
||||
|
||||
// Pre-calculate the final size of the StringBuilder
|
||||
int newSize = inputLength + (replacementLength - targetLength) * count;
|
||||
StringBuilder result = new StringBuilder(newSize);
|
||||
|
||||
Reference in New Issue
Block a user