Skip to content

Commit

Permalink
fix repr of strings/chars with quotes
Browse files Browse the repository at this point in the history
Closes #8743
  • Loading branch information
thestinger committed Sep 7, 2013
1 parent aa1d4ef commit f87578d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'self> ReprVisitor<'self> {
pub fn write_escaped_slice(&mut self, slice: &str) {
self.writer.write(['"' as u8]);
for ch in slice.iter() {
self.write_escaped_char(ch);
self.write_escaped_char(ch, true);
}
self.writer.write(['"' as u8]);
}
Expand Down Expand Up @@ -230,14 +230,26 @@ impl<'self> ReprVisitor<'self> {
v.fill, inner)
}

fn write_escaped_char(&mut self, ch: char) {
fn write_escaped_char(&mut self, ch: char, is_str: bool) {
match ch {
'\t' => self.writer.write("\\t".as_bytes()),
'\r' => self.writer.write("\\r".as_bytes()),
'\n' => self.writer.write("\\n".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]),
_ => {
do char::escape_unicode(ch) |c| {
Expand Down Expand Up @@ -274,7 +286,7 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
fn visit_char(&mut self) -> bool {
do self.get::<char> |this, &ch| {
this.writer.write(['\'' as u8]);
this.write_escaped_char(ch);
this.write_escaped_char(ch, false);
this.writer.write(['\'' as u8]);
}
}
Expand Down Expand Up @@ -684,6 +696,11 @@ fn test_repr() {
exact_test(&(10u64, ~"hello"),
"(10u64, ~\"hello\")");

exact_test(&'\'', "'\\''");
exact_test(&'"', "'\"'");
exact_test(&("'"), "\"'\"");
exact_test(&("\""), "\"\\\"\"");

exact_test(&println, "fn(&str)");
exact_test(&swap::<int>, "fn(&mut int, &mut int)");
exact_test(&is_alphabetic, "fn(char) -> bool");
Expand Down

5 comments on commit f87578d

@bors
Copy link
Contributor

@bors bors commented on f87578d Sep 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on f87578d Sep 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/repr = f87578d into auto

@bors
Copy link
Contributor

@bors bors commented on f87578d Sep 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/repr = f87578d merged ok, testing candidate = f6b36c6

@bors
Copy link
Contributor

@bors bors commented on f87578d Sep 7, 2013

@bors
Copy link
Contributor

@bors bors commented on f87578d Sep 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f6b36c6

Please sign in to comment.