Skip to content

Commit

Permalink
Fix regular expression bug where braces in character literals were im…
Browse files Browse the repository at this point in the history
…properly counted as part of the surrounding expression
  • Loading branch information
ct-austin committed Apr 29, 2019
1 parent ea32af6 commit 54b78bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ fn consume_expr(s: &str) -> (&str, String) {
// bad hack regex to look for string/character literals
// we don't want to count braces contained within them
static ref LITERAL: Regex = Regex::new(
concat!(r#"^('(?:\\[\s\S]|[^\\])+?'"#,
r##"|"(?:\\[\s\S]|[^\\])+?)""##)).unwrap();
concat!(r#"^('(?:\\[\s\S]+?|\\'|[^\\'])'"#,
r##"|"(?:\\[\s\S]|[^\\])+?")"##)).unwrap();
// have to handle raw strings separately due to no backrefs
static ref RAW_STRING_START: Regex = Regex::new(
r##"^r(#*)""##).unwrap();
Expand Down
20 changes: 20 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,24 @@ mod tests {
assert_eq!(&buffer[..], "\na boring string\nsome math 0xb\nthe 1st end".as_bytes());
}

#[test]
fn internal_lifetimes() {
let test = "a test";
assert_eq!("dirty a test",
iformat!("dirty { let x: &'static str = test; { let y: &'static str = x; y } }"));
assert_eq!("dirty static str = test; { let y: &",
iformat!(r#"dirty { let x = "static str = test; { let y: &"; x }"#));
}

#[test]
fn internal_chars() {
assert_eq!("an open brace: {",
iformat!("an open brace: {'{'}"));
assert_eq!("a close brace: }",
iformat!("a close brace: {'}'}"));
assert_eq!("an open brace: {",
iformat!(r#"an open brace: {'\u{007b}'}"#));
assert_eq!("a close brace: }",
iformat!(r#"a close brace: {'\u{007D}'}"#));
}
}

0 comments on commit 54b78bd

Please sign in to comment.