Skip to content

Commit 224f99e

Browse files
committed
---
yaml --- r: 153104 b: refs/heads/try2 c: 717de50 h: refs/heads/master v: v3
1 parent 950580c commit 224f99e

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 83f9f07ec4a339a31650abf7023a584cf7c5ce54
8+
refs/heads/try2: 717de500eefc2f501f12587fc9626944ab593ff8
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libserialize/json.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,37 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
256256
pub type EncodeResult = io::IoResult<()>;
257257
pub type DecodeResult<T> = Result<T, DecoderError>;
258258

259-
fn escape_bytes(writer: &mut io::Writer, s: &[u8]) -> Result<(), io::IoError> {
260-
try!(writer.write_str("\""));
261-
for byte in s.iter() {
262-
match *byte {
263-
b'"' => try!(writer.write_str("\\\"")),
264-
b'\\' => try!(writer.write_str("\\\\")),
265-
b'\x08' => try!(writer.write_str("\\b")),
266-
b'\x0c' => try!(writer.write_str("\\f")),
267-
b'\n' => try!(writer.write_str("\\n")),
268-
b'\r' => try!(writer.write_str("\\r")),
269-
b'\t' => try!(writer.write_str("\\t")),
270-
_ => try!(writer.write_u8(*byte)),
271-
}
272-
}
273-
writer.write_str("\"")
259+
pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError> {
260+
try!(wr.write_str("\""));
261+
262+
let mut start = 0;
263+
264+
for (i, byte) in bytes.iter().enumerate() {
265+
let escaped = match *byte {
266+
b'"' => "\\\"",
267+
b'\\' => "\\\\",
268+
b'\x08' => "\\b",
269+
b'\x0c' => "\\f",
270+
b'\n' => "\\n",
271+
b'\r' => "\\r",
272+
b'\t' => "\\t",
273+
_ => { continue; }
274+
};
275+
276+
if start < i {
277+
try!(wr.write(bytes.slice(start, i)));
278+
}
279+
280+
try!(wr.write_str(escaped));
281+
282+
start = i + 1;
283+
}
284+
285+
if start != bytes.len() {
286+
try!(wr.write(bytes.slice_from(start)));
287+
}
288+
289+
wr.write_str("\"")
274290
}
275291

276292
fn escape_str(writer: &mut io::Writer, v: &str) -> Result<(), io::IoError> {

0 commit comments

Comments
 (0)