Escape control characters when encoding strings

Closes #126
This commit is contained in:
Alex Crichton 2016-12-30 17:34:11 -08:00
parent 414a61cb84
commit f66b9913f9
2 changed files with 9 additions and 0 deletions

View file

@ -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)),
}
}

View file

@ -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""#);
}
}