8302323: Add repeat methods to StringBuilder/StringBuffer

Reviewed-by: tvaleev, redestad
This commit is contained in:
Jim Laskey 2023-04-03 15:29:21 +00:00
parent dd7ca757a7
commit 9b9b5a7a5c
6 changed files with 571 additions and 5 deletions

View file

@ -4563,14 +4563,36 @@ public final class String
final int limit = len * count;
final byte[] multiple = new byte[limit];
System.arraycopy(value, 0, multiple, 0, len);
int copied = len;
for (; copied < limit - copied; copied <<= 1) {
System.arraycopy(multiple, 0, multiple, copied, copied);
}
System.arraycopy(multiple, 0, multiple, copied, limit - copied);
repeatCopyRest(multiple, 0, limit, len);
return new String(multiple, coder);
}
/**
* Used to perform copying after the initial insertion. Copying is optimized
* by using power of two duplication. First pass duplicates original copy,
* second pass then duplicates the original and the copy yielding four copies,
* third pass duplicates four copies yielding eight copies, and so on.
* Finally, the remainder is filled in with prior copies.
*
* @implNote The technique used here is significantly faster than hand-rolled
* loops or special casing small numbers due to the intensive optimization
* done by intrinsic {@code System.arraycopy}.
*
* @param buffer destination buffer
* @param offset offset in the destination buffer
* @param limit total replicated including what is already in the buffer
* @param copied number of bytes that have already in the buffer
*/
static void repeatCopyRest(byte[] buffer, int offset, int limit, int copied) {
// Initial copy is in the buffer.
for (; copied < limit - copied; copied <<= 1) {
// Power of two duplicate.
System.arraycopy(buffer, offset, buffer, offset + copied, copied);
}
// Duplicate remainder.
System.arraycopy(buffer, offset, buffer, offset + copied, limit - copied);
}
////////////////////////////////////////////////////////////////
/**