Skip to content

Commit

Permalink
syntax: fix printing bug for HIR
Browse files Browse the repository at this point in the history
This commit fixes a bug in the HIR printer where it would not correctly
escape meta characters in character classes.
  • Loading branch information
BurntSushi committed Jul 18, 2018
1 parent 7ebe4ae commit 24c7770
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions regex-syntax/src/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<'p, W: fmt::Write> Writer<'p, W> {
fn write_literal_byte(&mut self, b: u8) -> fmt::Result {
let c = b as char;
if c <= 0x7F as char && !c.is_control() && !c.is_whitespace() {
self.wtr.write_char(c)
self.write_literal_char(c)
} else {
write!(self.wtr, "(?-u:\\x{:02X})", b)
}
Expand All @@ -237,7 +237,7 @@ impl<'p, W: fmt::Write> Writer<'p, W> {
fn write_literal_class_byte(&mut self, b: u8) -> fmt::Result {
let c = b as char;
if c <= 0x7F as char && !c.is_control() && !c.is_whitespace() {
self.wtr.write_char(c)
self.write_literal_char(c)
} else {
write!(self.wtr, "\\x{:02X}", b)
}
Expand Down Expand Up @@ -267,6 +267,10 @@ mod tests {
let mut printer = Printer::new();
let mut dst = String::new();
printer.print(&hir, &mut dst).unwrap();

// Check that the result is actually valid.
builder.build().parse(&dst).unwrap();

assert_eq!(expected, dst);
}

Expand All @@ -291,6 +295,18 @@ mod tests {
roundtrip(r"(?-u)[a]", r"(?-u:[a])");
roundtrip(r"(?-u)[a-z]", r"(?-u:[a-z])");
roundtrip_bytes(r"(?-u)[a-\xFF]", r"(?-u:[a-\xFF])");

// The following test that the printer escapes meta characters
// in character classes.
roundtrip(r"[\[]", r"[\[]");
roundtrip(r"[Z-_]", r"[Z-_]");
roundtrip(r"[Z-_--Z]", r"[\[-_]");

// The following test that the printer escapes meta characters
// in byte oriented character classes.
roundtrip_bytes(r"(?-u)[\[]", r"(?-u:[\[])");
roundtrip_bytes(r"(?-u)[Z-_]", r"(?-u:[Z-_])");
roundtrip_bytes(r"(?-u)[Z-_--Z]", r"(?-u:[\[-_])");
}

#[test]
Expand Down

0 comments on commit 24c7770

Please sign in to comment.