Skip to content

Commit

Permalink
search: properly handle and escape backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanueltouzery committed Jan 30, 2022
1 parent c72b596 commit 82a7e24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/search_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ fn quoted_string_char(input: &str) -> nom::IResult<&str, char> {
alt((none_of("\\\""), escaped_char))(input)
}

// meant for \" mostly for now
// meant for \" and \\ mostly for now
fn escaped_char(input: &str) -> nom::IResult<&str, char> {
let (input, _) = char('\\')(input)?;
none_of("\\")(input)
anychar(input)
}

fn parse_word(input: &str) -> nom::IResult<&str, String> {
Expand All @@ -279,6 +279,14 @@ mod tests {
);
}

#[test]
fn parse_quoted_string_quoted_backslash() {
assert_eq!(
"my \"str\\ing",
parse_quoted_string("\"my \\\"str\\\\ing\"").unwrap().1
);
}

#[test]
fn should_reject_unknown_filter_key() {
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/headerbar_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl Widget for HeaderbarSearch {
}
if val.contains(' ') || val.contains('"') {
t.push('"');
t.push_str(&val.replace('"', "\\\""));
t.push_str(&val.replace('\\', "\\\\").replace('"', "\\\""));
t.push('"');
} else {
t.push_str(&val);
Expand Down

0 comments on commit 82a7e24

Please sign in to comment.