Merge pull request #1237 from hgiesel/tagnone2

Add escape_anki_wildcards_for_search_node
This commit is contained in:
Damien Elmes 2021-06-16 17:30:51 +10:00 committed by GitHub
commit 1dd7bacf75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -12,7 +12,7 @@ use crate::{
parse_search, BoolSeparator, Node, PropertyKind, RatingKind, SearchNode, StateKind,
TemplateKind,
},
text::escape_anki_wildcards,
text::escape_anki_wildcards_for_search_node,
};
impl TryFrom<pb::SearchNode> for Node {
@ -22,9 +22,15 @@ impl TryFrom<pb::SearchNode> for Node {
use pb::search_node::{group::Joiner, Filter, Flag};
Ok(if let Some(filter) = msg.filter {
match filter {
Filter::Tag(s) => Node::Search(SearchNode::Tag(escape_anki_wildcards(&s))),
Filter::Deck(s) => Node::Search(SearchNode::Deck(escape_anki_wildcards(&s))),
Filter::Note(s) => Node::Search(SearchNode::Notetype(escape_anki_wildcards(&s))),
Filter::Tag(s) => {
Node::Search(SearchNode::Tag(escape_anki_wildcards_for_search_node(&s)))
}
Filter::Deck(s) => {
Node::Search(SearchNode::Deck(escape_anki_wildcards_for_search_node(&s)))
}
Filter::Note(s) => Node::Search(SearchNode::Notetype(
escape_anki_wildcards_for_search_node(&s),
)),
Filter::Template(u) => {
Node::Search(SearchNode::CardTemplate(TemplateKind::Ordinal(u as u16)))
}
@ -35,7 +41,7 @@ impl TryFrom<pb::SearchNode> for Node {
text: dupe.first_field,
}),
Filter::FieldName(s) => Node::Search(SearchNode::SingleField {
field: escape_anki_wildcards(&s),
field: escape_anki_wildcards_for_search_node(&s),
text: "*".to_string(),
is_re: false,
}),

View File

@ -399,13 +399,18 @@ pub(crate) fn to_text(txt: &str) -> Cow<str> {
/// Escape Anki wildcards and the backslash for escaping them: \*_
pub(crate) fn escape_anki_wildcards(txt: &str) -> String {
lazy_static! {
static ref RE: Regex = Regex::new(r"[\\*_]").unwrap();
}
RE.replace_all(&txt, r"\$0").into()
}
/// Escape Anki wildcards unless it's _*
pub(crate) fn escape_anki_wildcards_for_search_node(txt: &str) -> String {
if txt == "_*" {
txt.to_string()
} else {
lazy_static! {
static ref RE: Regex = Regex::new(r"[\\*_]").unwrap();
}
RE.replace_all(&txt, r"\$0").into()
escape_anki_wildcards(txt)
}
}