mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 23:12:21 -04:00
Merge pull request #834 from RumovZ/update-re-func
Replace text_to_re with to_re
This commit is contained in:
commit
5cd04f23f1
2 changed files with 15 additions and 38 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
|
||||||
|
@ -375,10 +343,7 @@ pub(crate) fn matches_glob(text: &str, search: &str) -> bool {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::text::without_combining;
|
use super::*;
|
||||||
use crate::text::{
|
|
||||||
extract_av_tags, strip_av_tags, strip_html, strip_html_preserving_media_filenames, AVTag,
|
|
||||||
};
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -427,4 +392,16 @@ mod test {
|
||||||
assert!(matches!(without_combining("test"), Cow::Borrowed(_)));
|
assert!(matches!(without_combining("test"), Cow::Borrowed(_)));
|
||||||
assert!(matches!(without_combining("Über"), Cow::Owned(_)));
|
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*"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue