pub enum PyRegex {
Fast(regex::Regex),
Fancy(fancy_regex::Regex),
}
pub type Spans = Vec<Option<(usize, usize)>>;
impl PyRegex {
pub fn new(pattern: &str) -> Result<Self, String> {
let fast_err = match regex::Regex::new(pattern) {
Ok(re) => return Ok(PyRegex::Fast(re)),
Err(e) => e,
};
match fancy_regex::Regex::new(pattern) {
Ok(re) => Ok(PyRegex::Fancy(re)),
Err(_) => Err(last_line(&fast_err.to_string())),
}
}
pub fn captures_len(&self) -> usize {
match self {
PyRegex::Fast(re) => re.captures_len(),
PyRegex::Fancy(re) => re.captures_len(),
}
}
pub fn named_groups(&self) -> Vec<(String, usize)> {
match self {
PyRegex::Fast(re) => named_from(re.capture_names()),
PyRegex::Fancy(re) => named_from(re.capture_names()),
}
}
pub fn first_captures(&self, text: &str) -> Option<Spans> {
let n = self.captures_len();
match self {
PyRegex::Fast(re) => re.captures(text).map(|c| collect_spans(&c, n)),
PyRegex::Fancy(re) => re
.captures(text)
.ok()
.flatten()
.map(|c| collect_fancy_spans(&c, n)),
}
}
pub fn all_captures(&self, text: &str) -> Vec<Spans> {
let n = self.captures_len();
match self {
PyRegex::Fast(re) => re.captures_iter(text).map(|c| collect_spans(&c, n)).collect(),
PyRegex::Fancy(re) => re
.captures_iter(text)
.filter_map(|c| c.ok())
.map(|c| collect_fancy_spans(&c, n))
.collect(),
}
}
pub fn split_n(&self, text: &str, limit: Option<usize>) -> Vec<String> {
match (self, limit) {
(PyRegex::Fast(re), Some(n)) => re.splitn(text, n).map(str::to_string).collect(),
(PyRegex::Fast(re), None) => re.split(text).map(str::to_string).collect(),
(PyRegex::Fancy(re), Some(n)) => re
.splitn(text, n)
.filter_map(|s| s.ok())
.map(str::to_string)
.collect(),
(PyRegex::Fancy(re), None) => re
.split(text)
.filter_map(|s| s.ok())
.map(str::to_string)
.collect(),
}
}
pub fn replace_n(&self, text: &str, count: Option<usize>, repl: &str) -> String {
match (self, count) {
(PyRegex::Fast(re), Some(n)) => re.replacen(text, n, repl).into_owned(),
(PyRegex::Fast(re), None) => re.replace_all(text, repl).into_owned(),
(PyRegex::Fancy(re), Some(n)) => re.replacen(text, n, repl).into_owned(),
(PyRegex::Fancy(re), None) => re.replace_all(text, repl).into_owned(),
}
}
pub fn count_matches(&self, text: &str, limit: Option<usize>) -> usize {
match self {
PyRegex::Fast(re) => match limit {
Some(n) => re.find_iter(text).take(n).count(),
None => re.find_iter(text).count(),
},
PyRegex::Fancy(re) => {
let it = re.find_iter(text).filter_map(|m| m.ok());
match limit {
Some(n) => it.take(n).count(),
None => it.count(),
}
}
}
}
}
fn named_from<'a>(names: impl Iterator<Item = Option<&'a str>>) -> Vec<(String, usize)> {
names
.enumerate()
.filter_map(|(i, n)| n.map(|n| (n.to_string(), i)))
.collect()
}
fn collect_spans(caps: ®ex::Captures<'_>, n: usize) -> Spans {
(0..n).map(|i| caps.get(i).map(|m| (m.start(), m.end()))).collect()
}
fn collect_fancy_spans(caps: &fancy_regex::Captures<'_>, n: usize) -> Spans {
(0..n).map(|i| caps.get(i).map(|m| (m.start(), m.end()))).collect()
}
fn last_line(msg: &str) -> String {
msg.lines().last().unwrap_or("bad pattern").trim().to_string()
}