This commit is contained in:
wenshao 2025-02-19 06:39:31 +08:00
parent dc096e496c
commit f5be616daa
3 changed files with 25 additions and 31 deletions

View File

@ -218,7 +218,7 @@ class JSONWriterUTF8
if (minCapacity > bytes.length) {
bytes = grow(minCapacity);
}
putByte(bytes, off++, (byte) '{');
bytes[off++] = '{';
if (pretty != PRETTY_NON) {
off = indent(bytes, off);
@ -239,7 +239,7 @@ class JSONWriterUTF8
off = indent(bytes, off);
}
putByte(bytes, off, (byte) '}');
bytes[off] = '}';
this.off = off + 1;
startObject = false;
}
@ -492,40 +492,31 @@ class JSONWriterUTF8
this.off = StringUtils.writeLatin1(bytes, off, value, quote);
}
protected final void writeStringLatin1BrowserSecure(byte[] values) {
boolean escape = false;
protected final void writeStringLatin1BrowserSecure(byte[] value) {
final byte quote = (byte) this.quote;
for (int i = 0; i < values.length; i++) {
byte c = values[i];
if (c == quote
|| c == '\\'
|| c < ' '
|| c == '<'
|| c == '>'
|| c == '('
|| c == ')'
) {
escape = true;
int i = 0;
for (; i < value.length; i++) {
byte c = value[i];
if (c == quote || c == '\\' || c < ' ' || c == '<' || c == '>' || c == '(' || c == ')') {
break;
}
}
int off = this.off;
if (!escape) {
int minCapacity = off + values.length + 2;
if (i == value.length) {
int minCapacity = off + value.length + 2;
byte[] bytes = this.bytes;
if (minCapacity > bytes.length) {
bytes = grow(minCapacity);
}
putByte(bytes, off, quote);
System.arraycopy(values, 0, bytes, off + 1, values.length);
off += values.length + 1;
System.arraycopy(value, 0, bytes, off + 1, value.length);
off += value.length + 1;
putByte(bytes, off, quote);
this.off = off + 1;
return;
}
writeStringEscaped(values);
writeStringEscaped(value);
}
public final void writeStringUTF16(byte[] value) {

View File

@ -14,15 +14,15 @@ public class StringUtils {
protected static final long MASK_BROWSER_SECURE = BrowserSecure.mask;
public static int writeLatin1(byte[] bytes, int off, byte[] value, byte quote) {
int strlen = value.length;
putByte(bytes, off, quote);
bytes[off] = quote;
System.arraycopy(value, 0, bytes, off + 1, strlen);
putByte(bytes, off + strlen + 1, quote);
bytes[off + strlen + 1] = quote;
return off + strlen + 2;
}
public static int writeLatin1Escaped(byte[] bytes, int off, byte[] values, byte quote, long features) {
final boolean browserSecure = (features & MASK_BROWSER_SECURE) != 0;
putByte(bytes, off++, quote);
bytes[off++] = quote;
for (int i = 0; i < values.length; i++) {
byte ch = values[i];
switch (ch) {
@ -73,13 +73,13 @@ public class StringUtils {
writeU4HexU(bytes, off, ch);
off += 6;
} else {
putByte(bytes, off++, ch);
bytes[off++] = ch;
}
break;
default:
if (ch == quote) {
putByte(bytes, off, (byte) '\\');
putByte(bytes, off + 1, (byte) quote);
bytes[off + 1] = quote;
off += 2;
} else if (ch < 0) {
// latin
@ -93,7 +93,7 @@ public class StringUtils {
break;
}
}
putByte(bytes, off, quote);
bytes[off] = quote;
return off + 1;
}
@ -363,10 +363,6 @@ public class StringUtils {
&& ((v ^ 0xA3A3A3A3A3A3A3A3L) + 0x0101010101010101L & 0x8080808080808080L) == 0x8080808080808080L; // != '\\'
}
public static int stringNeedCapacity(byte coder, byte[] value, boolean escaped) {
return value.length * (coder == 0 && escaped ? 1 : 6) + 2;
}
public static final class LATIN1 {
private static final short U2;
private static final int U4;

View File

@ -49,6 +49,13 @@ public class BrowserSecureTest {
assertEquals("\"abcd\\u003C\\u003E\\u0028\\u0029\"", jsonWriter.toString());
}
@Test
public void testUTF8_none() {
JSONWriter jsonWriter = JSONWriter.ofUTF8(JSONWriter.Feature.BrowserSecure);
jsonWriter.writeString("0123456789");
assertEquals("\"0123456789\"", jsonWriter.toString());
}
@Test
public void testUTF16_2_0() {
JSONWriter jsonWriter = JSONWriter.ofUTF16(JSONWriter.Feature.BrowserSecure);