use fancy_regex::Regex;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
pub type Compiled = Rc<Result<Pattern, String>>;
pub struct Pattern {
re: Regex,
}
thread_local! {
static CACHE: RefCell<HashMap<(String, bool), Compiled>> =
RefCell::new(HashMap::new());
}
pub fn compile(source: &str) -> Compiled {
cached(source, false)
}
pub fn compile_whole(source: &str) -> Compiled {
cached(source, true)
}
fn cached(source: &str, anchored: bool) -> Compiled {
let key = (source.to_string(), anchored);
CACHE.with(|c| {
if let Some(hit) = c.borrow().get(&key) {
return Rc::clone(hit);
}
let built = Rc::new(build(source, anchored));
c.borrow_mut().insert(key, Rc::clone(&built));
built
})
}
fn build(source: &str, anchored: bool) -> Result<Pattern, String> {
let translated = translate(source)?;
let final_src = if anchored {
format!(r"\A(?:{translated})\z")
} else {
translated
};
match Regex::new(&final_src) {
Ok(re) => Ok(Pattern { re }),
Err(e) => Err(format!("{e} near index 0\n{source}")),
}
}
impl Pattern {
pub fn matches_whole(&self, text: &str) -> Result<bool, String> {
self.re.is_match(text).map_err(|e| e.to_string())
}
pub fn split(&self, text: &str, limit: i64) -> Result<Vec<String>, String> {
let mut parts: Vec<String> = Vec::new();
let mut start = 0usize; let mut search = 0usize; let mut matched = false;
while search <= text.len() {
if limit > 0 && parts.len() as i64 == limit - 1 {
break;
}
let Some(m) = self
.re
.find_from_pos(text, search)
.map_err(|e| e.to_string())?
else {
break;
};
if m.start() == 0 && m.end() == 0 {
search = next_char_boundary(text, 0);
continue;
}
matched = true;
parts.push(text[start..m.start()].to_string());
start = m.end();
search = if m.end() == m.start() {
next_char_boundary(text, m.end())
} else {
m.end()
};
}
if !matched {
return Ok(vec![text.to_string()]);
}
parts.push(text[start..].to_string());
if limit == 0 {
while parts.last().is_some_and(|p| p.is_empty()) {
parts.pop();
}
}
Ok(parts)
}
pub fn replace(
&self,
text: &str,
replacement: &str,
first_only: bool,
) -> Result<String, String> {
let mut out = String::new();
let mut last = 0usize;
let mut search = 0usize;
while search <= text.len() {
let Some(caps) = self
.re
.captures_from_pos(text, search)
.map_err(|e| e.to_string())?
else {
break;
};
let m = caps.get(0).expect("group 0 always participates");
out.push_str(&text[last..m.start()]);
out.push_str(&expand(replacement, &caps)?);
last = m.end();
search = if m.end() == m.start() {
next_char_boundary(text, m.end())
} else {
m.end()
};
if first_only {
break;
}
}
out.push_str(&text[last..]);
Ok(out)
}
}
fn next_char_boundary(text: &str, i: usize) -> usize {
match text[i..].chars().next() {
Some(c) => i + c.len_utf8(),
None => text.len() + 1,
}
}
fn expand(replacement: &str, caps: &fancy_regex::Captures) -> Result<String, String> {
let mut out = String::new();
let mut it = replacement.chars().peekable();
while let Some(c) = it.next() {
match c {
'\\' => match it.next() {
Some(e) => out.push(e),
None => return Err("character to be escaped is missing".to_string()),
},
'$' => {
if it.peek().is_none() {
return Err("Illegal group reference: group index is missing".to_string());
}
if it.peek() == Some(&'{') {
it.next();
let mut name = String::new();
while let Some(&ch) = it.peek() {
if !ch.is_ascii_alphanumeric() {
break;
}
name.push(ch);
it.next();
}
if name.is_empty() {
return Err("named capturing group has 0 length name".to_string());
}
if it.next() != Some('}') {
return Err("named capturing group is missing trailing '}'".to_string());
}
match caps.name(&name) {
Some(m) => out.push_str(m.as_str()),
None => {
return Err(format!("No group with name {{{name}}}"));
}
}
continue;
}
let mut n: usize = match it.peek().and_then(|c| c.to_digit(10)) {
Some(d) => {
it.next();
d as usize
}
None => return Err("Illegal group reference".to_string()),
};
if n >= caps.len() {
return Err(format!("No group {n}"));
}
while let Some(d) = it.peek().and_then(|c| c.to_digit(10)) {
let wider = n * 10 + d as usize;
if wider >= caps.len() {
break;
}
n = wider;
it.next();
}
if let Some(m) = caps.get(n) {
out.push_str(m.as_str());
}
}
other => out.push(other),
}
}
Ok(out)
}
const TERMINATORS: &str = "\\n\\r\\u{85}\\u{2028}\\u{2029}";
const DOT: &str = "[^\\n\\r\\u{85}\\u{2028}\\u{2029}]";
const HORIZ: &str = "[ \\t\\u{a0}\\u{1680}\\u{180e}\\u{2000}-\\u{200a}\\u{202f}\\u{205f}\\u{3000}]";
const VERT: &str = "[\\n\\u{b}\\u{c}\\r\\u{85}\\u{2028}\\u{2029}]";
const LINEBREAK: &str = "(?:\\r\\n|[\\n\\u{b}\\u{c}\\r\\u{85}\\u{2028}\\u{2029}])";
const WORD: &str = "[:word:]";
const DIGIT: &str = "[:digit:]";
const SPACE: &str = "[:space:]";
const WORD_BOUNDARY: &str = "(?:(?<![[:word:]])(?=[[:word:]])|(?<=[[:word:]])(?![[:word:]]))";
const NOT_WORD_BOUNDARY: &str = "(?:(?<=[[:word:]])(?=[[:word:]])|(?<![[:word:]])(?![[:word:]]))";
#[derive(Clone, Copy, Default)]
struct Flags {
ci: bool,
dotall: bool,
}
pub fn translate(pattern: &str) -> Result<String, String> {
let cs: Vec<char> = pattern.chars().collect();
let mut out = String::with_capacity(pattern.len());
let mut i = 0usize;
let mut in_class = false;
let mut flags = Flags::default();
let mut stack: Vec<Flags> = Vec::new();
while i < cs.len() {
let c = cs[i];
match c {
'\\' => {
let Some(&e) = cs.get(i + 1) else {
return Err(unsupported("a trailing backslash", pattern));
};
i += 2;
match e {
'Q' => {
let mut lit = String::new();
while i < cs.len() {
if cs[i] == '\\' && cs.get(i + 1) == Some(&'E') {
i += 2;
break;
}
lit.push(cs[i]);
i += 1;
}
for ch in lit.chars() {
push_literal(&mut out, ch, in_class, flags.ci, true);
}
}
'E' => {} 'd' => out.push_str(&posix_class(DIGIT, false, in_class)),
'D' => out.push_str(&posix_class(DIGIT, true, in_class)),
'w' => out.push_str(&posix_class(WORD, false, in_class)),
'W' => out.push_str(&posix_class(WORD, true, in_class)),
's' => out.push_str(&posix_class(SPACE, false, in_class)),
'S' => out.push_str(&posix_class(SPACE, true, in_class)),
'b' | 'B' => {
if in_class {
return Err(unsupported(
&format!("\\{e} inside a character class"),
pattern,
));
}
out.push_str(if e == 'b' {
WORD_BOUNDARY
} else {
NOT_WORD_BOUNDARY
});
}
'h' => out.push_str(HORIZ),
'H' => out.push_str(&negate_class(HORIZ)),
'v' => out.push_str(VERT),
'V' => out.push_str(&negate_class(VERT)),
'R' => {
if in_class {
return Err(unsupported("\\R inside a character class", pattern));
}
out.push_str(LINEBREAK);
}
'Z' => {
if in_class {
return Err(unsupported("\\Z inside a character class", pattern));
}
out.push_str(&format!("(?={LINEBREAK}?\\z)"));
}
'u' | 'x' => {
let (ch, next) = read_code_point(&cs, i, e, pattern)?;
i = next;
push_literal(&mut out, ch, in_class, flags.ci, true);
}
'p' | 'P' => {
let (name, next) = read_braced(&cs, i, pattern, e)?;
i = next;
out.push_str(&translate_property(e, &name, pattern)?);
}
'G' => return Err(unsupported("\\G (end of previous match)", pattern)),
'X' => return Err(unsupported("\\X (grapheme cluster)", pattern)),
'c' => return Err(unsupported("\\cX (control character)", pattern)),
'N' => return Err(unsupported("\\N{...} (named character)", pattern)),
'0' => return Err(unsupported("\\0n (octal escape)", pattern)),
other => {
out.push('\\');
out.push(other);
}
}
}
'[' if !in_class => {
in_class = true;
out.push('[');
i += 1;
if cs.get(i) == Some(&'^') {
out.push('^');
i += 1;
}
if cs.get(i) == Some(&']') {
out.push_str("\\]");
i += 1;
}
}
']' if in_class => {
in_class = false;
out.push(']');
i += 1;
}
'.' if !in_class => {
out.push_str(if flags.dotall { "(?s:.)" } else { DOT });
i += 1;
}
'^' if !in_class => {
out.push_str("\\A");
i += 1;
}
'$' if !in_class => {
out.push_str(&format!("(?=(?:\\r\\n|[{TERMINATORS}])?\\z)"));
i += 1;
}
'(' if !in_class => {
if cs.get(i + 1) == Some(&'?') {
match cs.get(i + 2) {
Some('>') => return Err(unsupported("(?> (atomic group)", pattern)),
Some('(') => return Err(unsupported("(?(...) (conditional)", pattern)),
Some('#') => return Err(unsupported("(?# (comment group)", pattern)),
_ => {}
}
if let Some((letters, scoped, end)) = inline_flags(&cs, i + 2) {
let mut next = flags;
let mut on = true;
for f in letters.chars() {
match f {
'-' => on = false,
'i' => next.ci = on,
's' => next.dotall = on,
other => {
return Err(unsupported(
&format!("(?{other}) (flag with no equivalent)"),
pattern,
))
}
}
}
if scoped {
stack.push(flags);
flags = next;
out.push_str("(?:");
} else {
flags = next;
}
i = end + 1;
continue;
}
}
stack.push(flags);
out.push('(');
i += 1;
}
')' if !in_class => {
flags = stack.pop().unwrap_or_default();
out.push(')');
i += 1;
}
'*' | '+' | '?' | '}' if !in_class && cs.get(i + 1) == Some(&'+') => {
return Err(unsupported(
&format!("{c}+ (possessive quantifier)"),
pattern,
));
}
other if flags.ci && other.is_ascii_alphabetic() => {
if in_class && cs.get(i + 1) == Some(&'-') {
if let Some(&hi) = cs.get(i + 2) {
if hi.is_ascii_alphabetic() {
out.push(other);
out.push('-');
out.push(hi);
out.push(flip_case(other));
out.push('-');
out.push(flip_case(hi));
i += 3;
continue;
}
}
}
push_literal(&mut out, other, in_class, true, false);
i += 1;
}
other => {
out.push(other);
i += 1;
}
}
}
Ok(out)
}
fn posix_class(posix: &str, negated: bool, in_class: bool) -> String {
match (negated, in_class) {
(false, true) => posix.to_string(),
(false, false) => format!("[{posix}]"),
(true, _) => format!("[^{posix}]"),
}
}
fn push_literal(out: &mut String, c: char, in_class: bool, ci: bool, already_literal: bool) {
if ci && c.is_ascii_alphabetic() {
if !in_class {
out.push('[');
}
out.push(c);
out.push(flip_case(c));
if !in_class {
out.push(']');
}
return;
}
if already_literal {
out.push_str(&fancy_regex::escape(&c.to_string()));
} else {
out.push(c);
}
}
fn flip_case(c: char) -> char {
if c.is_ascii_uppercase() {
c.to_ascii_lowercase()
} else {
c.to_ascii_uppercase()
}
}
fn read_code_point(
cs: &[char],
i: usize,
kind: char,
pattern: &str,
) -> Result<(char, usize), String> {
let (digits, next) = if cs.get(i) == Some(&'{') {
let mut j = i + 1;
let mut d = String::new();
while j < cs.len() && cs[j] != '}' {
d.push(cs[j]);
j += 1;
}
if j == cs.len() {
return Err(unsupported(
&format!("\\{kind}{{...}} with no closing brace"),
pattern,
));
}
(d, j + 1)
} else {
let want = if kind == 'u' { 4 } else { 2 };
let d: String = cs[i..].iter().take(want).collect();
if d.chars().count() < want {
return Err(unsupported(
&format!("a truncated \\{kind} escape"),
pattern,
));
}
(d, i + want)
};
match u32::from_str_radix(&digits, 16)
.ok()
.and_then(char::from_u32)
{
Some(c) => Ok((c, next)),
None => Err(unsupported(
&format!("\\{kind} naming no character (`{digits}`)"),
pattern,
)),
}
}
fn negate_class(class: &str) -> String {
format!("[^{}", &class[1..])
}
fn read_braced(
cs: &[char],
i: usize,
pattern: &str,
kind: char,
) -> Result<(String, usize), String> {
if cs.get(i) != Some(&'{') {
return match cs.get(i) {
Some(c) => Ok((c.to_string(), i + 1)),
None => Err(unsupported(&format!("\\{kind} with no name"), pattern)),
};
}
let mut name = String::new();
let mut j = i + 1;
while j < cs.len() && cs[j] != '}' {
name.push(cs[j]);
j += 1;
}
if j == cs.len() {
return Err(unsupported(
&format!("\\{kind}{{...}} with no closing brace"),
pattern,
));
}
Ok((name, j + 1))
}
fn translate_property(kind: char, name: &str, pattern: &str) -> Result<String, String> {
let posix = |cls: &str| {
if kind == 'p' {
format!("[[:{cls}:]]")
} else {
format!("[[:^{cls}:]]")
}
};
Ok(match name {
"Alpha" => posix("alpha"),
"Digit" => posix("digit"),
"Alnum" => posix("alnum"),
"Upper" => posix("upper"),
"Lower" => posix("lower"),
"Space" => posix("space"),
"Punct" => posix("punct"),
"XDigit" => posix("xdigit"),
"ASCII" => posix("ascii"),
"Blank" => posix("blank"),
"Cntrl" => posix("cntrl"),
"Graph" => posix("graph"),
"Print" => posix("print"),
n if n.starts_with("In") => {
return Err(unsupported(
&format!("\\{kind}{{{n}}} (Unicode block)"),
pattern,
))
}
n if n.starts_with("java") => {
return Err(unsupported(
&format!("\\{kind}{{{n}}} (java.lang.Character predicate)"),
pattern,
))
}
n => format!("\\{kind}{{{}}}", n.strip_prefix("Is").unwrap_or(n)),
})
}
fn inline_flags(cs: &[char], i: usize) -> Option<(String, bool, usize)> {
let mut letters = String::new();
let mut j = i;
while let Some(&c) = cs.get(j) {
match c {
')' if !letters.is_empty() => return Some((letters, false, j)),
':' if !letters.is_empty() => return Some((letters, true, j)),
'a'..='z' | 'A'..='Z' | '-' => {
letters.push(c);
j += 1;
}
_ => return None,
}
}
None
}
fn unsupported(construct: &str, pattern: &str) -> String {
format!("javars supports no equivalent of {construct} near index 0\n{pattern}")
}