From f6992255dff64e42e690a2ab02bf3ea054157dd2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 30 Dec 2016 17:34:11 -0800 Subject: Escape control characters when encoding strings Closes #126 --- src/display.rs | 3 +++ src/lib.rs | 6 ++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/display.rs b/src/display.rs index f563b77..0a41e2a 100644 --- a/src/display.rs +++ b/src/display.rs @@ -49,6 +49,9 @@ fn write_str(f: &mut fmt::Formatter, s: &str) -> fmt::Result { '\u{d}' => try!(write!(f, "\\r")), '\u{22}' => try!(write!(f, "\\\"")), '\u{5c}' => try!(write!(f, "\\\\")), + c if c < '\u{1f}' => { + try!(write!(f, "\\u{:04}", ch as u32)) + } ch => try!(write!(f, "{}", ch)), } } diff --git a/src/lib.rs b/src/lib.rs index e8dcb92..74fe1b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -489,4 +489,10 @@ mod tests { assert_eq!(Value::Integer(0), *value.lookup("table.\"element\".\"value\".0").unwrap()); } + #[test] + fn control_characters() { + let value = Value::String("\x05".to_string()); + assert_eq!(value.to_string(), r#""\u0005""#); + } + } -- cgit v1.2.3