Skip to content

Commit 903f5c4

Browse files
Avoid allocations.
1 parent fc30518 commit 903f5c4

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/libserialize/json.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,41 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError
331331

332332
for (i, byte) in bytes.iter().enumerate() {
333333
let escaped = match *byte {
334-
b'"' => "\\\"".into_cow(),
335-
b'\\' => "\\\\".into_cow(),
336-
b'\x08' => "\\b".into_cow(),
337-
b'\x0c' => "\\f".into_cow(),
338-
b'\n' => "\\n".into_cow(),
339-
b'\r' => "\\r".into_cow(),
340-
b'\t' => "\\t".into_cow(),
341-
b'\x00'...b'\x1f' | b'\x7f' => format!("\\u00{:0>2x}", *byte).into_cow(),
334+
b'"' => "\\\"",
335+
b'\\' => "\\\\",
336+
b'\x00' => "\\u0000",
337+
b'\x01' => "\\u0001",
338+
b'\x02' => "\\u0002",
339+
b'\x03' => "\\u0003",
340+
b'\x04' => "\\u0004",
341+
b'\x05' => "\\u0005",
342+
b'\x06' => "\\u0006",
343+
b'\x07' => "\\u0007",
344+
b'\x08' => "\\b",
345+
b'\t' => "\\t",
346+
b'\n' => "\\n",
347+
b'\x0b' => "\\u000b",
348+
b'\x0c' => "\\f",
349+
b'\r' => "\\r",
350+
b'\x0e' => "\\u000e",
351+
b'\x0f' => "\\u000f",
352+
b'\x10' => "\\u0010",
353+
b'\x11' => "\\u0011",
354+
b'\x12' => "\\u0012",
355+
b'\x13' => "\\u0013",
356+
b'\x14' => "\\u0014",
357+
b'\x15' => "\\u0015",
358+
b'\x16' => "\\u0016",
359+
b'\x17' => "\\u0017",
360+
b'\x18' => "\\u0018",
361+
b'\x19' => "\\u0019",
362+
b'\x1a' => "\\u001a",
363+
b'\x1b' => "\\u001b",
364+
b'\x1c' => "\\u001c",
365+
b'\x1d' => "\\u001d",
366+
b'\x1e' => "\\u001e",
367+
b'\x1f' => "\\u001f",
368+
b'\x7f' => "\\u007f",
342369
_ => { continue; }
343370
};
344371

0 commit comments

Comments
 (0)