fix repr of strings/chars with quotes

Closes #8743
This commit is contained in:
Daniel Micay 2013-09-07 14:52:31 -04:00
parent aa1d4ef55a
commit f87578d9fb

View File

@ -178,7 +178,7 @@ impl<'self> ReprVisitor<'self> {
pub fn write_escaped_slice(&mut self, slice: &str) { pub fn write_escaped_slice(&mut self, slice: &str) {
self.writer.write(['"' as u8]); self.writer.write(['"' as u8]);
for ch in slice.iter() { for ch in slice.iter() {
self.write_escaped_char(ch); self.write_escaped_char(ch, true);
} }
self.writer.write(['"' as u8]); self.writer.write(['"' as u8]);
} }
@ -230,14 +230,26 @@ impl<'self> ReprVisitor<'self> {
v.fill, inner) v.fill, inner)
} }
fn write_escaped_char(&mut self, ch: char) { fn write_escaped_char(&mut self, ch: char, is_str: bool) {
match ch { match ch {
'\t' => self.writer.write("\\t".as_bytes()), '\t' => self.writer.write("\\t".as_bytes()),
'\r' => self.writer.write("\\r".as_bytes()), '\r' => self.writer.write("\\r".as_bytes()),
'\n' => self.writer.write("\\n".as_bytes()), '\n' => self.writer.write("\\n".as_bytes()),
'\\' => self.writer.write("\\\\".as_bytes()), '\\' => self.writer.write("\\\\".as_bytes()),
'\'' => self.writer.write("\\'".as_bytes()), '\'' => {
'"' => self.writer.write("\\\"".as_bytes()), if is_str {
self.writer.write("'".as_bytes())
} else {
self.writer.write("\\'".as_bytes())
}
}
'"' => {
if is_str {
self.writer.write("\\\"".as_bytes())
} else {
self.writer.write("\"".as_bytes())
}
}
'\x20'..'\x7e' => self.writer.write([ch as u8]), '\x20'..'\x7e' => self.writer.write([ch as u8]),
_ => { _ => {
do char::escape_unicode(ch) |c| { do char::escape_unicode(ch) |c| {
@ -274,7 +286,7 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
fn visit_char(&mut self) -> bool { fn visit_char(&mut self) -> bool {
do self.get::<char> |this, &ch| { do self.get::<char> |this, &ch| {
this.writer.write(['\'' as u8]); this.writer.write(['\'' as u8]);
this.write_escaped_char(ch); this.write_escaped_char(ch, false);
this.writer.write(['\'' as u8]); this.writer.write(['\'' as u8]);
} }
} }
@ -684,6 +696,11 @@ fn test_repr() {
exact_test(&(10u64, ~"hello"), exact_test(&(10u64, ~"hello"),
"(10u64, ~\"hello\")"); "(10u64, ~\"hello\")");
exact_test(&'\'', "'\\''");
exact_test(&'"', "'\"'");
exact_test(&("'"), "\"'\"");
exact_test(&("\""), "\"\\\"\"");
exact_test(&println, "fn(&str)"); exact_test(&println, "fn(&str)");
exact_test(&swap::<int>, "fn(&mut int, &mut int)"); exact_test(&swap::<int>, "fn(&mut int, &mut int)");
exact_test(&is_alphabetic, "fn(char) -> bool"); exact_test(&is_alphabetic, "fn(char) -> bool");