mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Replace text.rs/text_to_re with text.rs/to_re
This commit is contained in:
parent
e23d40e850
commit
0fc84d19b2
2 changed files with 2 additions and 34 deletions
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
collection::Collection,
|
collection::Collection,
|
||||||
err::{AnkiError, Result},
|
err::{AnkiError, Result},
|
||||||
notes::{NoteID, TransformNoteOutput},
|
notes::{NoteID, TransformNoteOutput},
|
||||||
text::text_to_re,
|
text::to_re,
|
||||||
{text::normalize_to_nfc, types::Usn},
|
{text::normalize_to_nfc, types::Usn},
|
||||||
};
|
};
|
||||||
use regex::{NoExpand, Regex, Replacer};
|
use regex::{NoExpand, Regex, Replacer};
|
||||||
|
@ -134,7 +134,7 @@ impl Collection {
|
||||||
// generate regexps
|
// generate regexps
|
||||||
let tags = split_tags(tags)
|
let tags = split_tags(tags)
|
||||||
.map(|tag| {
|
.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))
|
Regex::new(&format!("(?i)^{}$", tag))
|
||||||
.map_err(|_| AnkiError::invalid_input("invalid regex"))
|
.map_err(|_| AnkiError::invalid_input("invalid regex"))
|
||||||
})
|
})
|
||||||
|
|
|
@ -258,38 +258,6 @@ pub(crate) fn without_combining(s: &str) -> Cow<str> {
|
||||||
.into()
|
.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.
|
/// Check if string contains an unescaped wildcard.
|
||||||
pub(crate) fn is_glob(txt: &str) -> bool {
|
pub(crate) fn is_glob(txt: &str) -> bool {
|
||||||
// even number of \s followed by a wildcard
|
// even number of \s followed by a wildcard
|
||||||
|
|
Loading…
Reference in a new issue