mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 16:01:02 +00:00
rewrite escape_bytes to not write each byte individually
This commit is contained in:
@@ -735,20 +735,36 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
|
|||||||
|
|
||||||
pub type EncodeResult = io::IoResult<()>;
|
pub type EncodeResult = io::IoResult<()>;
|
||||||
|
|
||||||
pub fn escape_bytes<W: Writer>(wr: &mut W, s: &[u8]) -> IoResult<()> {
|
pub fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> IoResult<()> {
|
||||||
try!(wr.write_str("\""));
|
try!(wr.write_str("\""));
|
||||||
for byte in s.iter() {
|
|
||||||
match *byte {
|
let mut start = 0;
|
||||||
b'"' => try!(wr.write_str("\\\"")),
|
|
||||||
b'\\' => try!(wr.write_str("\\\\")),
|
for (i, byte) in bytes.iter().enumerate() {
|
||||||
b'\x08' => try!(wr.write_str("\\b")),
|
let escaped = match *byte {
|
||||||
b'\x0c' => try!(wr.write_str("\\f")),
|
b'"' => "\\\"",
|
||||||
b'\n' => try!(wr.write_str("\\n")),
|
b'\\' => "\\\\",
|
||||||
b'\r' => try!(wr.write_str("\\r")),
|
b'\x08' => "\\b",
|
||||||
b'\t' => try!(wr.write_str("\\t")),
|
b'\x0c' => "\\f",
|
||||||
_ => try!(wr.write_u8(*byte)),
|
b'\n' => "\\n",
|
||||||
|
b'\r' => "\\r",
|
||||||
|
b'\t' => "\\t",
|
||||||
|
_ => { continue; }
|
||||||
|
};
|
||||||
|
|
||||||
|
if start < i {
|
||||||
|
try!(wr.write(bytes.slice(start, i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try!(wr.write_str(escaped));
|
||||||
|
|
||||||
|
start = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if start != bytes.len() {
|
||||||
|
try!(wr.write(bytes.slice_from(start)));
|
||||||
|
}
|
||||||
|
|
||||||
wr.write_str("\"")
|
wr.write_str("\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user