Skip to content

Commit

Permalink
Fix string escaping (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored May 5, 2022
1 parent cd3988f commit 62c7110
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,12 @@ mod tests {
#[test]
fn it_recognizes_escaped_strings() {
let inputs = [
"\"foo \\\" JOIN bar\"",
"'foo \\' JOIN bar'",
"`foo `` JOIN bar`",
r#""foo \" JOIN bar""#,
r#"'foo \' JOIN bar'"#,
r#"`foo `` JOIN bar`"#,
r#"'foo '' JOIN bar'"#,
r#"'two households"'"#,
r#"'two households'''"#,
];
let options = FormatOptions::default();
for input in &inputs {
Expand Down
16 changes: 8 additions & 8 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,22 @@ pub fn take_till_escaping<'a, Error: ParseError<&'a str>>(
) -> impl Fn(&'a str) -> IResult<&'a str, &'a str, Error> {
move |input: &str| {
let mut chars = input.chars().enumerate().peekable();
let mut last = None;
loop {
let item = chars.next();
let next = chars.peek().map(|item| item.1);
match item {
Some(item) => {
if item.1 == desired
&& !last.map(|item| escapes.contains(&item)).unwrap_or(false)
&& !(escapes.contains(&item.1) && Some(desired) == next)
{
// escape?
if escapes.contains(&item.1) && next.map(|n| n == desired).unwrap_or(false) {
// consume this and next char
chars.next();
continue;
}

if item.1 == desired {
let byte_pos = input.chars().take(item.0).map(|c| c.len()).sum::<usize>();
return Ok((&input[byte_pos..], &input[..byte_pos]));
}

last = Some(item.1);
continue;
}
None => {
return Ok(("", input));
Expand Down

0 comments on commit 62c7110

Please sign in to comment.