Skip to content

Commit 7185903

Browse files
Escape Unicode C1 escape characters in strings
1 parent e10f6fa commit 7185903

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

json/src/ser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ pub fn escape_bytes<W>(wr: &mut W, bytes: &[u8]) -> Result<()>
494494
try!(wr.write_all(b"\""));
495495

496496
let mut start = 0;
497+
let mut last_byte_was_c2 = false;
497498

498499
for (i, byte) in bytes.iter().enumerate() {
499500
let escaped = match *byte {
@@ -515,6 +516,21 @@ pub fn escape_bytes<W>(wr: &mut W, bytes: &[u8]) -> Result<()>
515516

516517
continue;
517518
},
519+
b'\x80' ... b'\x9F' if last_byte_was_c2 => {
520+
if start < (i - 1) {
521+
try!(wr.write_all(&bytes[start..(i - 1)]));
522+
}
523+
524+
try!(write!(wr,"\\u{:04X}", *byte));
525+
526+
start = i + 1;
527+
528+
continue;
529+
},
530+
b'\xC2' => {
531+
last_byte_was_c2 = true;
532+
continue;
533+
},
518534
_ => { continue; }
519535
};
520536

0 commit comments

Comments
 (0)