use std::sync::Arc;
use php_std::pcre::Regex;
macro_rules! pattern {
($regex:literal) => {{
static PATTERN: std::sync::LazyLock<std::sync::Arc<php_std::pcre::Regex>> =
std::sync::LazyLock::new(|| $crate::pattern::compile($regex.as_bytes()));
&**PATTERN
}};
}
pub(crate) use pattern;
pub(crate) fn compile(regex: &[u8]) -> Arc<Regex> {
match Regex::compiled(regex) {
Ok(re) => re,
Err(e) => {
panic!("invalid pattern {}: {}", String::from_utf8_lossy(regex), String::from_utf8_lossy(&e.message))
}
}
}
pub(crate) fn group<'s>(regex: &Regex, subject: &'s [u8]) -> Option<&'s [u8]> {
regex.captures(subject)?.get(1)
}