8364320: String encodeUTF8 latin1 with negatives

Reviewed-by: liach, rriggs
This commit is contained in:
Brett Okken 2025-08-19 20:31:17 +00:00 committed by Roger Riggs
parent 0858743dee
commit 3bbaa772b0

View File

@ -1299,13 +1299,18 @@ public final class String
return encodeUTF8_UTF16(val, doReplace);
}
if (!StringCoding.hasNegatives(val, 0, val.length)) {
int positives = StringCoding.countPositives(val, 0, val.length);
if (positives == val.length) {
return val.clone();
}
int dp = 0;
byte[] dst = StringUTF16.newBytesFor(val.length);
for (byte c : val) {
if (positives > 0) {
System.arraycopy(val, 0, dst, 0, positives);
}
int dp = positives;
for (int i = dp; i < val.length; i++) {
byte c = val[i];
if (c < 0) {
dst[dp++] = (byte) (0xc0 | ((c & 0xff) >> 6));
dst[dp++] = (byte) (0x80 | (c & 0x3f));