|
|
#![feature(arbitrary_self_types_pointers)] |
|
|
|
|
|
use std::vec; |
|
|
|
|
|
use anyhow::{Result, bail}; |
|
|
|
|
|
pub fn register() { |
|
|
turbo_tasks::register(); |
|
|
include!(concat!(env!("OUT_DIR"), "/register.rs")); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)] |
|
|
#[turbo_tasks::value(eq = "manual", shared)] |
|
|
#[serde(into = "RegexForm", try_from = "RegexForm")] |
|
|
pub struct EsRegex { |
|
|
#[turbo_tasks(trace_ignore)] |
|
|
delegate: EsRegexImpl, |
|
|
|
|
|
|
|
|
pub pattern: String, |
|
|
pub flags: String, |
|
|
} |
|
|
|
|
|
#[derive(Debug, Clone)] |
|
|
enum EsRegexImpl { |
|
|
Regex(regex::Regex), |
|
|
Regress(regress::Regex), |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl PartialEq for EsRegex { |
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
self.pattern == other.pattern && self.flags == other.flags |
|
|
} |
|
|
} |
|
|
impl Eq for EsRegex {} |
|
|
|
|
|
impl TryFrom<RegexForm> for EsRegex { |
|
|
type Error = anyhow::Error; |
|
|
|
|
|
fn try_from(value: RegexForm) -> std::result::Result<Self, Self::Error> { |
|
|
EsRegex::new(&value.pattern, &value.flags) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] |
|
|
struct RegexForm { |
|
|
pattern: String, |
|
|
flags: String, |
|
|
} |
|
|
|
|
|
impl From<EsRegex> for RegexForm { |
|
|
fn from(value: EsRegex) -> Self { |
|
|
Self { |
|
|
pattern: value.pattern, |
|
|
flags: value.flags, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl EsRegex { |
|
|
|
|
|
|
|
|
pub fn new(pattern: &str, flags: &str) -> Result<Self> { |
|
|
|
|
|
let pattern = pattern.replace("\\/", "/"); |
|
|
|
|
|
let mut applied_flags = String::new(); |
|
|
for flag in flags.chars() { |
|
|
match flag { |
|
|
|
|
|
'd' => {} |
|
|
|
|
|
'g' => {} |
|
|
|
|
|
'i' => applied_flags.push('i'), |
|
|
|
|
|
'm' => applied_flags.push('m'), |
|
|
|
|
|
's' => applied_flags.push('s'), |
|
|
|
|
|
'u' => applied_flags.push('u'), |
|
|
|
|
|
'y' => {} |
|
|
_ => bail!("unsupported flag `{flag}` in regex: `{pattern}` with flags: `{flags}`"), |
|
|
} |
|
|
} |
|
|
|
|
|
let regex = if !applied_flags.is_empty() { |
|
|
regex::Regex::new(&format!("(?{applied_flags}){pattern}")) |
|
|
} else { |
|
|
regex::Regex::new(&pattern) |
|
|
}; |
|
|
|
|
|
let delegate = match regex { |
|
|
Ok(reg) => Ok(EsRegexImpl::Regex(reg)), |
|
|
Err(_e) => { |
|
|
|
|
|
|
|
|
match regress::Regex::with_flags(&pattern, regress::Flags::from(flags)) { |
|
|
Ok(reg) => Ok(EsRegexImpl::Regress(reg)), |
|
|
|
|
|
Err(e) => Err(e), |
|
|
} |
|
|
} |
|
|
}?; |
|
|
Ok(Self { |
|
|
delegate, |
|
|
pattern, |
|
|
flags: flags.to_string(), |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn is_match(&self, haystack: &str) -> bool { |
|
|
match &self.delegate { |
|
|
EsRegexImpl::Regex(r) => r.is_match(haystack), |
|
|
EsRegexImpl::Regress(r) => r.find(haystack).is_some(), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn captures<'h>(&self, haystack: &'h str) -> Option<Captures<'h>> { |
|
|
let delegate = match &self.delegate { |
|
|
EsRegexImpl::Regex(r) => CapturesImpl::Regex { |
|
|
captures: r.captures(haystack)?, |
|
|
idx: 0, |
|
|
}, |
|
|
EsRegexImpl::Regress(r) => { |
|
|
let re_match = r.find(haystack)?; |
|
|
CapturesImpl::Regress { |
|
|
captures_iter: re_match.captures.into_iter(), |
|
|
haystack, |
|
|
match_range: Some(re_match.range), |
|
|
} |
|
|
} |
|
|
}; |
|
|
Some(Captures { delegate }) |
|
|
} |
|
|
} |
|
|
|
|
|
pub struct Captures<'h> { |
|
|
delegate: CapturesImpl<'h>, |
|
|
} |
|
|
|
|
|
enum CapturesImpl<'h> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Regex { |
|
|
captures: regex::Captures<'h>, |
|
|
idx: usize, |
|
|
}, |
|
|
|
|
|
Regress { |
|
|
captures_iter: vec::IntoIter<Option<regress::Range>>, |
|
|
haystack: &'h str, |
|
|
match_range: Option<regress::Range>, |
|
|
}, |
|
|
} |
|
|
|
|
|
impl<'h> Iterator for Captures<'h> { |
|
|
type Item = Option<&'h str>; |
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> { |
|
|
match &mut self.delegate { |
|
|
CapturesImpl::Regex { captures, idx } => { |
|
|
if *idx >= captures.len() { |
|
|
None |
|
|
} else { |
|
|
let capture = Some(captures.get(*idx).map(|sub_match| sub_match.as_str())); |
|
|
*idx += 1; |
|
|
capture |
|
|
} |
|
|
} |
|
|
CapturesImpl::Regress { |
|
|
captures_iter, |
|
|
haystack, |
|
|
match_range, |
|
|
} => { |
|
|
if let Some(range) = match_range.take() { |
|
|
|
|
|
Some(Some(&haystack[range])) |
|
|
} else { |
|
|
Some(captures_iter.next()?.map(|range| &haystack[range])) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use super::{EsRegex, EsRegexImpl}; |
|
|
|
|
|
#[test] |
|
|
fn round_trip_serialize() { |
|
|
let regex = EsRegex::new("[a-z]", "i").unwrap(); |
|
|
let serialized = serde_json::to_string(®ex).unwrap(); |
|
|
let parsed = serde_json::from_str::<EsRegex>(&serialized).unwrap(); |
|
|
assert_eq!(regex, parsed); |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn es_regex_matches_simple() { |
|
|
let regex = EsRegex::new("a", "").unwrap(); |
|
|
assert!(matches!(regex.delegate, EsRegexImpl::Regex { .. })); |
|
|
assert!(regex.is_match("a")); |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn es_regex_matches_negative_lookahead() { |
|
|
|
|
|
let regex = EsRegex::new("a(?!b)", "").unwrap(); |
|
|
assert!(matches!(regex.delegate, EsRegexImpl::Regress { .. })); |
|
|
assert!(!regex.is_match("ab")); |
|
|
assert!(regex.is_match("ac")); |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn invalid_regex() { |
|
|
|
|
|
|
|
|
|
|
|
assert!(matches!(EsRegex::new("*", ""), Err { .. })) |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn captures_with_regex() { |
|
|
let regex = EsRegex::new(r"(notmatched)|(\d{4})-(\d{2})-(\d{2})", "").unwrap(); |
|
|
assert!(matches!(regex.delegate, EsRegexImpl::Regex { .. })); |
|
|
|
|
|
let captures = regex.captures("Today is 2024-01-15"); |
|
|
assert!(captures.is_some()); |
|
|
let caps: Vec<_> = captures.unwrap().collect(); |
|
|
assert_eq!(caps.len(), 5); |
|
|
assert_eq!(caps[0], Some("2024-01-15")); |
|
|
assert_eq!(caps[1], None); |
|
|
assert_eq!(caps[2], Some("2024")); |
|
|
assert_eq!(caps[3], Some("01")); |
|
|
assert_eq!(caps[4], Some("15")); |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn captures_with_regress() { |
|
|
let regex = EsRegex::new(r"(\w+)(?=baz)", "").unwrap(); |
|
|
assert!(matches!(regex.delegate, EsRegexImpl::Regress { .. })); |
|
|
|
|
|
let captures = regex.captures("foobar"); |
|
|
assert!(captures.is_none()); |
|
|
|
|
|
let captures = regex.captures("foobaz"); |
|
|
assert!(captures.is_some()); |
|
|
let caps: Vec<_> = captures.unwrap().collect(); |
|
|
assert_eq!(caps.len(), 2); |
|
|
assert_eq!(caps[0], Some("foo")); |
|
|
assert_eq!(caps[1], Some("foo")); |
|
|
} |
|
|
} |
|
|
|