use regex::bytes::Regex;
pub(crate) fn ascii(pattern: &str) -> Regex {
Regex::new(&format!("(?-u){pattern}")).unwrap_or_else(|e| panic!("bad pattern {pattern}: {e}"))
}
macro_rules! js_re {
($pattern:expr) => {{
static RE: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(|| $crate::re::ascii($pattern));
&*RE
}};
}
pub(crate) use js_re;
macro_rules! uni_re {
($pattern:expr) => {{
static RE: std::sync::LazyLock<regex::Regex> =
std::sync::LazyLock::new(|| regex::Regex::new($pattern).expect("a valid pattern"));
&*RE
}};
}
pub(crate) use uni_re;
pub(crate) fn group_u(re: ®ex::Regex, text: &str, n: usize) -> Option<String> {
let caps = re.captures(text)?;
Some(caps.get(n).map(|m| m.as_str().to_string()).unwrap_or_default())
}
pub(crate) fn test(re: &Regex, text: &str) -> bool {
re.is_match(text.as_bytes())
}
pub(crate) fn group(re: &Regex, text: &str, n: usize) -> Option<String> {
let caps = re.captures(text.as_bytes())?;
Some(caps.get(n).map(|m| String::from_utf8_lossy(m.as_bytes()).into_owned()).unwrap_or_default())
}
#[allow(dead_code)]
pub(crate) fn replace_all(re: &Regex, text: &str, with: &str) -> String {
String::from_utf8_lossy(&re.replace_all(text.as_bytes(), regex::bytes::NoExpand(with.as_bytes()))).into_owned()
}
#[allow(dead_code)]
pub(crate) fn replace_first(re: &Regex, text: &str, with: &str) -> String {
String::from_utf8_lossy(&re.replace(text.as_bytes(), regex::bytes::NoExpand(with.as_bytes()))).into_owned()
}