Skip to content

Commit

Permalink
optimize JSONWriterUTF16JDK8UF
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 19, 2025
1 parent f55355f commit fd8c667
Showing 1 changed file with 4 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,9 @@ public final class JSONWriterUTF16JDK8UF

@Override
public void writeString(String str) {
if (str == null) {
writeStringNull();
return;
}

boolean browserSecure = (context.features & BrowserSecure.mask) != 0;
boolean escapeNoneAscii = (context.features & EscapeNoneAscii.mask) != 0;
char[] value = (char[]) UNSAFE.getObject(str, JDKUtils.FIELD_STRING_VALUE_OFFSET);
final int strlen = value.length;

boolean escape = false;
for (int i = 0; i < value.length; i++) {
char ch = value[i];
if (ch == quote || ch == '\\' || ch < ' '
|| (browserSecure && (ch == '<' || ch == '>' || ch == '(' || ch == ')'))
|| (escapeNoneAscii && ch > 0x007F)
) {
escape = true;
break;
}
}

if (!escape) {
int off = this.off;
// inline ensureCapacity(off + strlen + 2);
int minCapacity = off + strlen + 2;
if (minCapacity >= chars.length) {
ensureCapacity(minCapacity);
}

final char[] chars = this.chars;
chars[off++] = quote;
System.arraycopy(value, 0, chars, off, value.length);
off += strlen;
chars[off] = quote;
this.off = off + 1;
return;
}

writeStringEscape(str);
writeString(
str == null
? null
: (char[]) UNSAFE.getObject(str, JDKUtils.FIELD_STRING_VALUE_OFFSET));
}
}

0 comments on commit fd8c667

Please sign in to comment.