fix escape handling, and handle sql wildcards

This commit is contained in:
Damien Elmes 2020-03-20 09:27:19 +10:00
parent b70668d31c
commit c723adea17

View file

@ -174,8 +174,8 @@ fn text(s: &str) -> IResult<&str, Node> {
fn search_node_for_text(s: &str) -> ParseResult<SearchNode> { fn search_node_for_text(s: &str) -> ParseResult<SearchNode> {
let mut it = s.splitn(2, ':'); let mut it = s.splitn(2, ':');
let (head, tail) = ( let (head, tail) = (
without_escapes(it.next().unwrap()), unescape_quotes(it.next().unwrap()),
it.next().map(without_escapes), it.next().map(unescape_quotes),
); );
if let Some(tail) = tail { if let Some(tail) = tail {
@ -185,10 +185,10 @@ fn search_node_for_text(s: &str) -> ParseResult<SearchNode> {
} }
} }
/// Strip the \ escaping character /// \" -> "
fn without_escapes(s: &str) -> Cow<str> { fn unescape_quotes(s: &str) -> Cow<str> {
if s.find('\\').is_some() { if s.find(r#"\""#).is_some() {
s.replace('\\', "").into() s.replace(r#"\""#, "\"").into()
} else { } else {
s.into() s.into()
} }
@ -216,10 +216,10 @@ fn quoted_term(s: &str) -> IResult<&str, Node> {
} }
/// Quoted text, terminated by a non-escaped double quote /// Quoted text, terminated by a non-escaped double quote
/// Can escape " and \ /// Can escape %, _, " and \
fn quoted_term_inner(s: &str) -> IResult<&str, Node> { fn quoted_term_inner(s: &str) -> IResult<&str, Node> {
map_res( map_res(
escaped(is_not(r#""\"#), '\\', one_of(r#""\"#)), escaped(is_not(r#""\"#), '\\', one_of(r#""\%_"#)),
|o| -> ParseResult<Node> { Ok(Node::Search(search_node_for_text(o)?)) }, |o| -> ParseResult<Node> { Ok(Node::Search(search_node_for_text(o)?)) },
)(s) )(s)
} }