From 0fc84d19b245e8391c047b9a2dc8c25bc88499ec Mon Sep 17 00:00:00 2001 From: RumovZ Date: Fri, 20 Nov 2020 09:23:25 +0100 Subject: [PATCH 1/2] Replace text.rs/text_to_re with text.rs/to_re --- rslib/src/tags.rs | 4 ++-- rslib/src/text.rs | 32 -------------------------------- 2 files changed, 2 insertions(+), 34 deletions(-) diff --git a/rslib/src/tags.rs b/rslib/src/tags.rs index 7b6c424ff..e0443c7b0 100644 --- a/rslib/src/tags.rs +++ b/rslib/src/tags.rs @@ -5,7 +5,7 @@ use crate::{ collection::Collection, err::{AnkiError, Result}, notes::{NoteID, TransformNoteOutput}, - text::text_to_re, + text::to_re, {text::normalize_to_nfc, types::Usn}, }; use regex::{NoExpand, Regex, Replacer}; @@ -134,7 +134,7 @@ impl Collection { // generate regexps let tags = split_tags(tags) .map(|tag| { - let tag = if regex { tag.into() } else { text_to_re(tag) }; + let tag = if regex { tag.into() } else { to_re(tag) }; Regex::new(&format!("(?i)^{}$", tag)) .map_err(|_| AnkiError::invalid_input("invalid regex")) }) diff --git a/rslib/src/text.rs b/rslib/src/text.rs index 934fa64c0..5a063eafc 100644 --- a/rslib/src/text.rs +++ b/rslib/src/text.rs @@ -258,38 +258,6 @@ pub(crate) fn without_combining(s: &str) -> Cow { .into() } -/// Escape text, converting glob characters to regex syntax, then return. -pub(crate) fn text_to_re(glob: &str) -> String { - lazy_static! { - static ref ESCAPED: Regex = Regex::new(r"(\\\\)?\\\*").unwrap(); - static ref GLOB: Regex = Regex::new(r"(\\\\)?[_%]").unwrap(); - } - - let escaped = regex::escape(glob); - - let text = ESCAPED.replace_all(&escaped, |caps: &Captures| { - if caps.get(0).unwrap().as_str().len() == 2 { - ".*" - } else { - r"\*" - } - }); - - let text2 = GLOB.replace_all(&text, |caps: &Captures| { - match caps.get(0).unwrap().as_str() { - "_" => ".", - "%" => ".*", - other => { - // strip off the escaping char - &other[2..] - } - } - .to_string() - }); - - text2.into() -} - /// Check if string contains an unescaped wildcard. pub(crate) fn is_glob(txt: &str) -> bool { // even number of \s followed by a wildcard From 347c547e10d9925bfb8a3e3018d9d1dbb3d4f7a6 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Fri, 20 Nov 2020 09:45:53 +0100 Subject: [PATCH 2/2] Add tests for conversion functions in text.rs --- rslib/src/text.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/rslib/src/text.rs b/rslib/src/text.rs index 5a063eafc..0cfa814c7 100644 --- a/rslib/src/text.rs +++ b/rslib/src/text.rs @@ -343,10 +343,7 @@ pub(crate) fn matches_glob(text: &str, search: &str) -> bool { #[cfg(test)] mod test { - use crate::text::without_combining; - use crate::text::{ - extract_av_tags, strip_av_tags, strip_html, strip_html_preserving_media_filenames, AVTag, - }; + use super::*; use std::borrow::Cow; #[test] @@ -395,4 +392,16 @@ mod test { assert!(matches!(without_combining("test"), Cow::Borrowed(_))); assert!(matches!(without_combining("Über"), Cow::Owned(_))); } + + #[test] + fn conversion() { + assert_eq!(&to_re(r"[te\*st]"), r"\[te\*st\]"); + assert_eq!(&to_custom_re("f_o*", r"\d"), r"f\do\d*"); + assert_eq!(&to_sql("%f_o*"), r"\%f_o%"); + assert_eq!(&to_text(r"\*\_*_"), "*_*_"); + assert_eq!(&escape_sql(r"1\2%3_"), r"1\\2\%3\_"); + assert!(is_glob(r"\\\\_")); + assert!(!is_glob(r"\\\_")); + assert!(matches_glob("foo*bar123", r"foo\*bar*")); + } }