From 63cc877023ad0001746a9c977e06d9d159c223f6 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Sun, 15 Nov 2020 11:54:21 +0100 Subject: [PATCH] Move whitespace check for tags from to sqlwriter Instead of not parsing a tag containing whitespace, check for spaces when writing sql and if there are any, explicitly match nothing. --- rslib/src/search/parser.rs | 11 +---------- rslib/src/search/sqlwriter.rs | 4 ++++ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/rslib/src/search/parser.rs b/rslib/src/search/parser.rs index ec8727456..a8abc2ee5 100644 --- a/rslib/src/search/parser.rs +++ b/rslib/src/search/parser.rs @@ -281,7 +281,7 @@ fn search_node_for_text_with_argument<'a>( "edited" => SearchNode::EditedInDays(val.parse()?), "deck" => SearchNode::Deck(unescape_quotes(val)), "note" => SearchNode::NoteType(unescape_to_re(val)?), - "tag" => SearchNode::Tag(parse_tag(val)?), + "tag" => SearchNode::Tag(unescape_to_enforced_re(val, r"\S")?), "mid" => SearchNode::NoteTypeID(val.parse()?), "nid" => SearchNode::NoteIDs(check_id_list(val)?), "cid" => SearchNode::CardIDs(check_id_list(val)?), @@ -300,15 +300,6 @@ fn search_node_for_text_with_argument<'a>( }) } -/// Ensure the string doesn't contain whitespace and unescape. -fn parse_tag(s: &str) -> ParseResult { - if s.as_bytes().iter().any(u8::is_ascii_whitespace) { - Err(ParseError {}) - } else { - unescape_to_enforced_re(s, r"\S") - } -} - /// ensure a list of ids contains only numbers and commas, returning unchanged if true /// used by nid: and cid: fn check_id_list(s: &str) -> ParseResult<&str> { diff --git a/rslib/src/search/sqlwriter.rs b/rslib/src/search/sqlwriter.rs index daf6e2af2..3354342ee 100644 --- a/rslib/src/search/sqlwriter.rs +++ b/rslib/src/search/sqlwriter.rs @@ -189,6 +189,9 @@ impl SqlWriter<'_> { } fn write_tag(&mut self, s: &String) -> Result<()> { + if s.contains(" ") { + write!(self.sql, "false").unwrap(); + } else { match s.as_str() { "none" => write!(self.sql, "n.tags = ''").unwrap(), r"\S*" => write!(self.sql, "true").unwrap(), @@ -197,6 +200,7 @@ impl SqlWriter<'_> { self.args.push(format!("(?i).* {} .*", s)); } } + } Ok(()) }