Skip to content

Commit

Permalink
Speed improvement in test classes. May contribute to upstream
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Dec 2, 2024
1 parent 1dd4ba1 commit f9e50b6
Showing 1 changed file with 4 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3738,55 +3738,12 @@ public static final char[] replace(
char[] toBeReplaced,
char[] replacementChars) {

int max = array.length;
int replacedLength = toBeReplaced.length;
int replacementLength = replacementChars.length;

int[] starts = new int[5];
int occurrenceCount = 0;

if (!equals(toBeReplaced, replacementChars)) {

next : for (int i = 0; i < max;) {
int index = indexOf(toBeReplaced, array, true, i);
if (index == -1) {
i++;
continue next;
}
if (occurrenceCount == starts.length) {
System.arraycopy(
starts,
0,
starts = new int[occurrenceCount * 2],
0,
occurrenceCount);
}
starts[occurrenceCount++] = index;
i = index + replacedLength;
}
}
if (occurrenceCount == 0)
String s1 = new String(array);
String myRet = s1.replace(new String(toBeReplaced), new String(replacementChars));
if( myRet.equals(s1)) {
return array;
char[] result =
new char[max
+ occurrenceCount * (replacementLength - replacedLength)];
int inStart = 0, outStart = 0;
for (int i = 0; i < occurrenceCount; i++) {
int offset = starts[i] - inStart;
System.arraycopy(array, inStart, result, outStart, offset);
inStart += offset;
outStart += offset;
System.arraycopy(
replacementChars,
0,
result,
outStart,
replacementLength);
inStart += replacedLength;
outStart += replacementLength;
}
System.arraycopy(array, inStart, result, outStart, max - inStart);
return result;
return myRet.toCharArray();
}

/**
Expand Down

0 comments on commit f9e50b6

Please sign in to comment.