2023-02-24 23:55:49 +08:00
|
|
|
use crate::wchar::{wstr, WString, L};
|
|
|
|
|
|
|
|
/// Adjust a pattern so that it is anchored at both beginning and end.
|
|
|
|
/// This is a workaround for the fact that PCRE2_ENDANCHORED is unavailable on pre-2017 PCRE2
|
|
|
|
/// (e.g. 10.21, on Xenial).
|
|
|
|
pub fn regex_make_anchored(pattern: &wstr) -> WString {
|
|
|
|
// PATTERN -> ^(:?PATTERN)$.
|
|
|
|
let prefix = L!("^(?:");
|
|
|
|
let suffix = L!(")$");
|
2023-05-03 02:13:11 +08:00
|
|
|
let mut anchored = WString::with_capacity(prefix.len() + pattern.len() + suffix.len());
|
|
|
|
anchored.push_utfstr(prefix);
|
|
|
|
anchored.push_utfstr(pattern);
|
2023-02-24 23:55:49 +08:00
|
|
|
anchored.push_utfstr(suffix);
|
|
|
|
anchored
|
|
|
|
}
|
|
|
|
|
2023-03-26 06:57:20 +08:00
|
|
|
/// Copy a wstr to a Box<[char]>.
|
|
|
|
pub fn to_boxed_chars(s: &wstr) -> Box<[char]> {
|
|
|
|
let chars = s.as_char_slice();
|
|
|
|
chars.into()
|
|
|
|
}
|
|
|
|
|
2023-04-09 09:19:48 +08:00
|
|
|
#[test]
|
|
|
|
fn test_regex_make_anchored() {
|
|
|
|
use pcre2::utf32::{Regex, RegexBuilder};
|
|
|
|
|
|
|
|
fn test_match(re: &Regex, subject: &wstr) -> bool {
|
|
|
|
re.is_match(&to_boxed_chars(subject)).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
let builder = RegexBuilder::new();
|
|
|
|
let result = builder.build(to_boxed_chars(®ex_make_anchored(L!("ab(.+?)"))));
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let re = &result.unwrap();
|
|
|
|
|
|
|
|
assert!(!test_match(re, L!("")));
|
|
|
|
assert!(!test_match(re, L!("ab")));
|
|
|
|
assert!(test_match(re, L!("abcd")));
|
|
|
|
assert!(!test_match(re, L!("xabcd")));
|
|
|
|
assert!(test_match(re, L!("abcdefghij")));
|
|
|
|
|
|
|
|
let result = builder.build(to_boxed_chars(®ex_make_anchored(L!("(a+)|(b+)"))));
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let re = &result.unwrap();
|
|
|
|
assert!(!test_match(re, L!("")));
|
|
|
|
assert!(!test_match(re, L!("aabb")));
|
|
|
|
assert!(test_match(re, L!("aaaa")));
|
|
|
|
assert!(test_match(re, L!("bbbb")));
|
|
|
|
assert!(!test_match(re, L!("aaaax")));
|
|
|
|
}
|