Skip to content

Commit 10aa451

Browse files
committed
fixed AbstractRawValue.toString
1 parent 03879cf commit 10aa451

3 files changed

Lines changed: 346 additions & 22 deletions

File tree

src/main/java/org/msgpack/packer/JSONPacker.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,29 +307,31 @@ private static void escape(Output out, String s) throws IOException {
307307
for(int i=0; i < chars.length; i++) {
308308
int ch = chars[i];
309309
if (ch <= 0x7f) {
310-
int e = ESCAPE_TABLE[ch];
311-
if (e == 0) {
312-
tmp[2] = (byte) ch;
313-
out.write(tmp, 2, 1);
314-
} else if (e > 0) {
315-
tmp[2] = BACKSLASH;
316-
tmp[3] = (byte) e;
317-
out.write(tmp, 2, 2);
318-
} else {
319-
// control char
320-
tmp[2] = ZERO;
321-
tmp[3] = ZERO;
322-
tmp[4] = HEX_TABLE[ch >> 4];
323-
tmp[5] = HEX_TABLE[ch & 0x0f];
324-
out.write(tmp, 0, 6);
310+
int e = ESCAPE_TABLE[ch];
311+
if (e == 0) {
312+
// ascii char
313+
tmp[2] = (byte) ch;
314+
out.write(tmp, 2, 1);
315+
} else if (e > 0) {
316+
// popular control char
317+
tmp[2] = BACKSLASH;
318+
tmp[3] = (byte) e;
319+
out.write(tmp, 2, 2);
320+
} else {
321+
// control char uXXXXXX
322+
tmp[2] = ZERO;
323+
tmp[3] = ZERO;
324+
tmp[4] = HEX_TABLE[ch >> 4];
325+
tmp[5] = HEX_TABLE[ch & 0x0f];
326+
out.write(tmp, 0, 6);
325327
}
326328
} else if (ch <= 0x7ff) {
327329
// 2-bytes char
328330
tmp[2] = (byte) (0xc0 | (ch >> 6));
329331
tmp[3] = (byte) (0x80 | (ch & 0x3f));
330332
out.write(tmp, 2, 2);
331333
} else if (ch >= 0xd800 && ch <= 0xdfff) {
332-
// outside of BMP
334+
// surrogates
333335
tmp[2] = HEX_TABLE[(ch >> 12) & 0x0f];
334336
tmp[3] = HEX_TABLE[(ch >> 8) & 0x0f];
335337
tmp[4] = HEX_TABLE[(ch >> 4) & 0x0f];

src/main/java/org/msgpack/type/AbstractRawValue.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public StringBuilder toString(StringBuilder sb) {
7878
sb.append("\\b");
7979
break;
8080
default:
81+
// control chars
8182
escapeChar(sb, ch);
8283
break;
8384
}
84-
} else {
85+
} else if(ch <= 0x7f) {
8586
switch(ch) {
8687
case '\\':
8788
sb.append("\\\\");
@@ -93,18 +94,25 @@ public StringBuilder toString(StringBuilder sb) {
9394
sb.append(ch);
9495
break;
9596
}
97+
} else if(ch >= 0xd800 && ch <= 0xdfff) {
98+
// surrogates
99+
escapeChar(sb, ch);
100+
} else {
101+
sb.append(ch);
96102
}
97103
}
98104
sb.append("\"");
99105
return sb;
100106
}
101107

102-
private void escapeChar(StringBuilder sb, char ch) {
108+
private final static char[] HEX_TABLE = "0123456789ABCDEF".toCharArray();
109+
110+
private void escapeChar(StringBuilder sb, int ch) {
103111
sb.append("\\u");
104-
sb.append(((int)ch >> 12) & 0xF);
105-
sb.append(((int)ch >> 8) & 0xF);
106-
sb.append(((int)ch >> 4) & 0xF);
107-
sb.append((int)ch & 0xF);
112+
sb.append(HEX_TABLE[(ch >> 12) & 0x0f]);
113+
sb.append(HEX_TABLE[(ch >> 8) & 0x0f]);
114+
sb.append(HEX_TABLE[(ch >> 4) & 0x0f]);
115+
sb.append(HEX_TABLE[ch & 0x0f]);
108116
}
109117
}
110118

0 commit comments

Comments
 (0)