use crate::host::with_host;
use fusevm::Value;
use rustc_hash::FxHashMap;
use std::cell::RefCell;
use std::rc::Rc;
use regex::bytes::{Regex, RegexBuilder};
fn bstr(b: &[u8]) -> String {
String::from_utf8_lossy(b).into_owned()
}
const SPLIT_NO_EMPTY: i64 = 1;
const SPLIT_DELIM_CAPTURE: i64 = 2;
const SPLIT_OFFSET_CAPTURE: i64 = 4;
const GREP_INVERT: i64 = 1;
const SET_ORDER: i64 = 2;
const OFFSET_CAPTURE: i64 = 256;
const UNMATCHED_AS_NULL: i64 = 512;
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
Some(match name {
"preg_match" => preg_match(args),
"preg_match_all" => preg_match_all(args),
"preg_replace" => preg_replace(args),
"preg_replace_callback" => preg_replace_callback(args),
"preg_split" => preg_split(args),
"preg_quote" => preg_quote(args),
"preg_grep" => preg_grep(args),
"preg_last_error" => Ok(Value::int(with_host(|h| h.preg_error()))),
"preg_last_error_msg" => Ok(Value::str(error_msg(with_host(|h| h.preg_error())))),
_ => return None,
})
}
pub const PREG_NO_ERROR: i64 = 0;
pub const PREG_INTERNAL_ERROR: i64 = 1;
pub const PREG_BACKTRACK_LIMIT_ERROR: i64 = 2;
fn error_msg(code: i64) -> &'static str {
match code {
PREG_NO_ERROR => "No error",
PREG_INTERNAL_ERROR => "Internal error",
2 => "Backtrack limit exhausted",
3 => "Recursion limit exhausted",
4 => "Malformed UTF-8 characters, possibly incorrectly encoded",
5 => "The offset did not correspond to the beginning of a valid UTF-8 code point",
6 => "JIT stack limit exhausted",
_ => "Internal error",
}
}
enum PatternError {
Php(String),
Unsupported,
}
#[derive(Clone, Copy)]
pub(crate) struct Span<'h> {
start: usize,
end: usize,
hay: &'h [u8],
}
impl<'h> Span<'h> {
fn start(&self) -> usize {
self.start
}
fn end(&self) -> usize {
self.end
}
fn as_bytes(&self) -> &'h [u8] {
&self.hay[self.start..self.end]
}
}
pub(crate) struct Caps<'h> {
groups: Vec<Option<(usize, usize)>>,
hay: &'h [u8],
}
impl<'h> Caps<'h> {
fn len(&self) -> usize {
self.groups.len()
}
fn get(&self, i: usize) -> Option<Span<'h>> {
self.groups.get(i).copied().flatten().map(|(s, e)| Span {
start: s,
end: e,
hay: self.hay,
})
}
fn expand(&self, repl: &[u8], out: &mut Vec<u8>) {
let mut i = 0usize;
while i < repl.len() {
if repl[i] != b'$' {
out.push(repl[i]);
i += 1;
continue;
}
if repl.get(i + 1) == Some(&b'$') {
out.push(b'$');
i += 2;
continue;
}
let (digits_at, braced) = match repl.get(i + 1) {
Some(b'{') => (i + 2, true),
_ => (i + 1, false),
};
let mut j = digits_at;
while j < repl.len() && repl[j].is_ascii_digit() {
j += 1;
}
let closed = !braced || repl.get(j) == Some(&b'}');
if j == digits_at || !closed {
out.push(b'$');
i += 1;
continue;
}
let n: usize = std::str::from_utf8(&repl[digits_at..j])
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(usize::MAX);
if let Some(m) = self.get(n) {
out.extend_from_slice(m.as_bytes());
}
i = if braced { j + 1 } else { j };
}
}
}
enum Engine {
Bytes(Regex),
Fancy(fancy_regex::Regex),
}
const END_ANCHOR: &str = "(?=\\n?\\z)";
struct DollarNl {
body: String,
unicode: bool,
re: std::cell::OnceCell<Option<fancy_regex::Regex>>,
notempty: std::cell::OnceCell<Option<fancy_regex::Regex>>,
}
#[derive(Clone)]
struct Flags {
case_insensitive: bool,
multi_line: bool,
dot_all: bool,
extended: bool,
swap_greed: bool,
}
pub(crate) struct Pattern {
engine: Engine,
anchored: bool,
body: String,
flags: Flags,
notempty: std::cell::OnceCell<Option<fancy_regex::Regex>>,
dollar_nl: Option<DollarNl>,
}
impl Pattern {
fn captures_all<'h>(&self, hay: &'h [u8]) -> Vec<Caps<'h>> {
self.captures_all_from(hay, 0)
}
fn captures_all_from<'h>(&self, hay: &'h [u8], from: usize) -> Vec<Caps<'h>> {
if from > hay.len() {
return Vec::new();
}
let dn = self.dollar_variant(hay);
let mut out = Vec::new();
let mut pos = from;
let mut retry_nonempty = false;
while pos <= hay.len() {
let hit = if retry_nonempty {
self.captures_nonempty_at(dn, hay, pos)
} else {
self.captures_at(dn, hay, pos)
.filter(|c| !self.anchored || c.get(0).is_some_and(|m| m.start() == pos))
};
let Some(caps) = hit else {
if retry_nonempty && pos < hay.len() {
retry_nonempty = false;
pos = next_boundary(hay, pos);
continue;
}
break;
};
let m = caps.get(0).expect("group 0 always participates");
let (start, end) = (m.start(), m.end());
out.push(caps);
retry_nonempty = end == start;
pos = end;
}
out
}
fn captures_nonempty_at<'h>(
&self,
dn: Option<&DollarNl>,
hay: &'h [u8],
pos: usize,
) -> Option<Caps<'h>> {
let (cell, body) = match dn {
Some(d) => (&d.notempty, &d.body),
None => (&self.notempty, &self.body),
};
let re = cell
.get_or_init(|| build_fancy(body, &self.flags, true))
.as_ref()?;
let caps = fancy_captures_at(re, hay, pos)?;
if caps.get(0)?.start() != pos {
return None;
}
Some(caps)
}
fn dollar_variant(&self, hay: &[u8]) -> Option<&DollarNl> {
let d = self.dollar_nl.as_ref()?;
if !hay.ends_with(b"\n") {
return None;
}
if !hay.is_ascii() && !(d.unicode && std::str::from_utf8(hay).is_ok()) {
return None;
}
d.re.get_or_init(|| build_fancy(&d.body, &self.flags, false))
.as_ref()?;
Some(d)
}
fn captures_first<'h>(&self, hay: &'h [u8]) -> Option<Caps<'h>> {
self.captures_first_from(hay, 0)
}
fn captures_first_from<'h>(&self, hay: &'h [u8], from: usize) -> Option<Caps<'h>> {
if from > hay.len() {
return None;
}
let dn = self.dollar_variant(hay);
self.captures_at(dn, hay, from)
.filter(|c| !self.anchored || c.get(0).is_some_and(|m| m.start() == from))
}
fn captures_at<'h>(
&self,
dn: Option<&DollarNl>,
hay: &'h [u8],
pos: usize,
) -> Option<Caps<'h>> {
if let Some(d) = dn {
let re = d.re.get()?.as_ref()?;
return fancy_captures_at(re, hay, pos);
}
match &self.engine {
Engine::Bytes(re) => {
let caps = re.captures_at(hay, pos)?;
Some(Caps {
groups: (0..caps.len())
.map(|i| caps.get(i).map(|m| (m.start(), m.end())))
.collect(),
hay,
})
}
Engine::Fancy(re) => fancy_captures_at(re, hay, pos),
}
}
pub(crate) fn is_match(&self, hay: &[u8]) -> bool {
self.captures_first(hay).is_some()
}
fn captures_len(&self) -> usize {
match &self.engine {
Engine::Bytes(re) => re.captures_len(),
Engine::Fancy(re) => re.captures_len(),
}
}
fn group_names(&self) -> Vec<Option<String>> {
match &self.engine {
Engine::Bytes(re) => re.capture_names().map(|n| n.map(str::to_string)).collect(),
Engine::Fancy(re) => re.capture_names().map(|n| n.map(str::to_string)).collect(),
}
}
}
fn push_keyed(out: &mut Vec<(Value, Value)>, names: &[Option<String>], i: usize, v: Value) {
if let Some(Some(name)) = names.get(i) {
let dup = with_host(|h| h.copy_on_assign(v.clone()));
out.push((Value::str(name.clone()), dup));
}
out.push((Value::int(i as i64), v));
}
fn build_fancy(body: &str, flags: &Flags, not_empty: bool) -> Option<fancy_regex::Regex> {
let body = if flags.swap_greed {
format!("(?U){body}")
} else {
body.to_string()
};
let mut b = fancy_regex::RegexBuilder::new(&body);
b.case_insensitive(flags.case_insensitive);
b.multi_line(flags.multi_line);
b.dot_matches_new_line(flags.dot_all);
b.ignore_whitespace(flags.extended);
b.find_not_empty(not_empty);
b.build().ok()
}
fn fancy_captures_at<'h>(re: &fancy_regex::Regex, hay: &'h [u8], pos: usize) -> Option<Caps<'h>> {
let text = std::str::from_utf8(hay).ok()?;
if !text.is_char_boundary(pos) {
return None;
}
match re.captures_from_pos(text, pos) {
Ok(caps) => {
let caps = caps?;
Some(Caps {
groups: (0..caps.len())
.map(|i| caps.get(i).map(|m| (m.start(), m.end())))
.collect(),
hay,
})
}
Err(e) => {
with_host(|h| h.set_preg_error(runtime_error_code(&e)));
None
}
}
}
fn next_boundary(hay: &[u8], from: usize) -> usize {
let mut i = from + 1;
while i < hay.len() && (hay[i] & 0xC0) == 0x80 {
i += 1;
}
i
}
fn runtime_error_code(e: &fancy_regex::Error) -> i64 {
match e {
fancy_regex::Error::RuntimeError(fancy_regex::RuntimeError::BacktrackLimitExceeded) => {
PREG_BACKTRACK_LIMIT_ERROR
}
_ => PREG_INTERNAL_ERROR,
}
}
fn compile(pattern: &str) -> Result<Pattern, PatternError> {
let chars: Vec<char> = pattern.chars().collect();
let mut i = 0;
while i < chars.len() && chars[i].is_whitespace() {
i += 1;
}
if i >= chars.len() {
return Err(PatternError::Php("Empty regular expression".into()));
}
let open = chars[i];
if open.is_alphanumeric() || open == '\\' || open == '\0' {
return Err(PatternError::Php(
"Delimiter must not be alphanumeric, backslash, or NUL byte".into(),
));
}
let bracket = matches!(open, '(' | '{' | '[' | '<');
let close = match open {
'(' => ')',
'{' => '}',
'[' => ']',
'<' => '>',
c => c,
};
let body_start = i + 1;
let mut depth = 1usize;
let mut j = body_start;
let close_idx = loop {
if j >= chars.len() {
return Err(PatternError::Php(if bracket {
format!("No ending matching delimiter '{close}' found")
} else {
format!("No ending delimiter '{close}' found")
}));
}
if chars[j] == '\\' && j + 1 < chars.len() {
j += 2;
continue;
}
if chars[j] == close {
depth -= 1;
if depth == 0 {
break j;
}
} else if bracket && chars[j] == open {
depth += 1;
}
j += 1;
};
let body: String = chars[body_start..close_idx].iter().collect();
let flags: String = chars[close_idx + 1..].iter().collect();
let mut unicode = false;
let mut no_auto_capture = false;
let mut case_insensitive = false;
let mut multi_line = false;
let mut dot_all = false;
let mut extended = false;
let mut swap_greed = false;
let mut anchored = false;
let mut dollar_endonly = false;
for f in flags.chars() {
match f {
'i' => case_insensitive = true,
'm' => multi_line = true,
's' => dot_all = true,
'x' => extended = true,
'U' => swap_greed = true,
'u' => unicode = true,
'n' => no_auto_capture = true,
'A' => anchored = true,
'D' => dollar_endonly = true,
'r' | 'X' | 'S' | 'J' => {}
c if c.is_whitespace() => {}
other => {
return Err(PatternError::Php(format!("Unknown modifier '{other}'")));
}
}
}
let scanned = match scan_body(&body, no_auto_capture) {
Ok(t) => t,
Err(msg) => return Err(PatternError::Php(format!("Compilation failed: {msg}"))),
};
let translated = scanned.text;
let flags = Flags {
case_insensitive,
multi_line,
dot_all,
extended,
swap_greed,
};
let dollar_nl = scanned
.dollar_nl
.filter(|_| !multi_line && !dollar_endonly)
.map(|body| DollarNl {
body,
unicode,
re: std::cell::OnceCell::new(),
notempty: std::cell::OnceCell::new(),
});
let mut b = RegexBuilder::new(&translated);
b.case_insensitive(case_insensitive);
b.multi_line(multi_line);
b.dot_matches_new_line(dot_all);
b.ignore_whitespace(extended);
b.swap_greed(swap_greed);
b.unicode(unicode);
let engine = match b.build() {
Ok(re) => Engine::Bytes(re),
Err(_) => {
Engine::Fancy(build_fancy(&translated, &flags, false).ok_or(PatternError::Unsupported)?)
}
};
Ok(Pattern {
engine,
anchored,
body: translated,
flags,
notempty: std::cell::OnceCell::new(),
dollar_nl,
})
}
fn scan_body(body: &str, no_auto_capture: bool) -> Result<Scanned, String> {
let c: Vec<char> = body.chars().collect();
let mut out = String::with_capacity(body.len());
let mut dollars: Vec<usize> = Vec::new();
let mut inline_multi_line = false;
let mut depth: usize = 0;
let mut repeatable = false;
let mut i = 0;
while i < c.len() {
match c[i] {
'\\' if i + 1 < c.len() => {
if c[i + 1] == 'Z' {
out.push_str(END_ANCHOR);
} else {
out.push(c[i]);
out.push(c[i + 1]);
}
repeatable = true;
i += 2;
}
'$' => {
dollars.push(out.len());
out.push('$');
repeatable = true;
i += 1;
}
'[' => {
let mut k = i + 1;
if k < c.len() && c[k] == '^' {
k += 1;
}
if k < c.len() && c[k] == ']' {
k += 1;
}
loop {
if k >= c.len() {
return Err(format!(
"missing terminating ] for character class at offset {}",
c.len()
));
}
if c[k] == '\\' {
k += 2;
continue;
}
if c[k] == ']' {
break;
}
k += 1;
}
out.extend(&c[i..=k]);
repeatable = true;
i = k + 1;
}
'(' => {
depth += 1;
repeatable = false;
i += 1;
if i < c.len() && c[i] == '?' {
out.push_str("(?");
i += 1;
let mut k = i;
while k < c.len() && (c[k].is_ascii_alphabetic() || c[k] == '-') {
if c[k] == 'm' {
inline_multi_line = true;
}
k += 1;
}
} else if no_auto_capture {
out.push_str("(?:");
} else {
out.push('(');
}
}
')' => {
if depth == 0 {
return Err(format!("unmatched closing parenthesis at offset {}", i + 1));
}
depth -= 1;
repeatable = true;
out.push(')');
i += 1;
}
'|' => {
repeatable = false;
out.push('|');
i += 1;
}
'*' | '+' | '?' => {
if !repeatable {
return Err(format!(
"quantifier does not follow a repeatable item at offset {}",
i + 1
));
}
repeatable = false;
out.push(c[i]);
i += 1;
if i < c.len() && (c[i] == '?' || c[i] == '+') {
out.push(c[i]);
i += 1;
}
}
'{' => match brace_quantifier(&c, i) {
None => {
out.push_str("\\{");
repeatable = true;
i += 1;
}
Some((lo, hi, end)) => {
if !repeatable {
return Err(format!(
"quantifier does not follow a repeatable item at offset {}",
end + 1
));
}
if let (Some(lo), Some(hi)) = (lo, hi) {
if lo > hi {
return Err(format!(
"numbers out of order in {{}} quantifier at offset {end}"
));
}
}
match lo {
Some(_) => out.extend(&c[i..=end]),
None => {
out.push_str("{0");
out.extend(&c[i + 1..=end]);
}
}
repeatable = false;
i = end + 1;
if i < c.len() && (c[i] == '?' || c[i] == '+') {
out.push(c[i]);
i += 1;
}
}
},
other => {
out.push(other);
repeatable = true;
i += 1;
}
}
}
if depth > 0 {
return Err(format!("missing closing parenthesis at offset {}", c.len()));
}
let dollar_nl = (!dollars.is_empty() && !inline_multi_line).then(|| {
let mut v = String::with_capacity(out.len() + dollars.len() * END_ANCHOR.len());
let mut last = 0;
for &d in &dollars {
v.push_str(&out[last..d]);
v.push_str(END_ANCHOR);
last = d + 1; }
v.push_str(&out[last..]);
v
});
Ok(Scanned {
text: out,
dollar_nl,
})
}
struct Scanned {
text: String,
dollar_nl: Option<String>,
}
fn brace_quantifier(c: &[char], open: usize) -> Option<(Option<u64>, Option<u64>, usize)> {
let mut k = open + 1;
let digits = |k: &mut usize| -> Option<u64> {
let start = *k;
while *k < c.len() && c[*k].is_ascii_digit() {
*k += 1;
}
(*k > start).then(|| {
c[start..*k]
.iter()
.collect::<String>()
.parse()
.unwrap_or(u64::MAX)
})
};
let lo = digits(&mut k);
let hi = if k < c.len() && c[k] == ',' {
k += 1;
digits(&mut k)
} else {
lo
};
if lo.is_none() && hi.is_none() {
return None;
}
(k < c.len() && c[k] == '}').then_some((lo, hi, k))
}
pub(crate) fn compile_for(func: &str, pat: &str) -> Option<Rc<Pattern>> {
match cached_compile(pat) {
Cached::Ok(re) => {
with_host(|h| h.set_preg_error(PREG_NO_ERROR));
Some(re)
}
Cached::Php(msg) => {
with_host(|h| {
h.set_preg_error(PREG_INTERNAL_ERROR);
h.warn(format_args!("{func}(): {msg}"));
});
None
}
Cached::Unsupported => None,
}
}
#[derive(Clone)]
enum Cached {
Ok(Rc<Pattern>),
Php(String),
Unsupported,
}
const PATTERN_CACHE_LIMIT: usize = 4096;
thread_local! {
static PATTERN_CACHE: RefCell<FxHashMap<String, Cached>> =
RefCell::new(FxHashMap::default());
}
fn cached_compile(pat: &str) -> Cached {
if let Some(hit) = PATTERN_CACHE.with(|c| c.borrow().get(pat).cloned()) {
return hit;
}
let outcome = match compile(pat) {
Ok(re) => Cached::Ok(Rc::new(re)),
Err(PatternError::Php(msg)) => Cached::Php(msg),
Err(PatternError::Unsupported) => Cached::Unsupported,
};
PATTERN_CACHE.with(|c| {
let mut map = c.borrow_mut();
if map.len() >= PATTERN_CACHE_LIMIT {
map.clear();
}
map.insert(pat.to_string(), outcome.clone());
});
outcome
}
fn caps_values(caps: &Caps<'_>, fmt: CellFmt) -> Vec<Value> {
(0..caps.len()).map(|i| fmt.cell(caps, i)).collect()
}
fn last_participating(caps: &Caps<'_>) -> usize {
(0..caps.len())
.rfind(|&i| caps.get(i).is_some())
.unwrap_or(0)
}
#[derive(Clone, Copy, Default)]
struct CellFmt {
offset: bool,
as_null: bool,
}
impl CellFmt {
fn from_flags(flags: i64) -> Self {
Self {
offset: flags & OFFSET_CAPTURE != 0,
as_null: flags & UNMATCHED_AS_NULL != 0,
}
}
fn cell(&self, caps: &Caps<'_>, i: usize) -> Value {
let m = caps.get(i);
let text = match &m {
Some(m) => Value::str(bstr(m.as_bytes())),
None if self.as_null => Value::Undef,
None => Value::str(String::new()),
};
if !self.offset {
return text;
}
let at = m.map(|m| m.start() as i64).unwrap_or(-1);
make_list(vec![text, Value::int(at)])
}
fn row_end(&self, caps: &Caps<'_>) -> usize {
if self.as_null {
caps.len().saturating_sub(1)
} else {
last_participating(caps)
}
}
}
fn key_row(values: Vec<Value>, names: &[Option<String>]) -> Vec<(Value, Value)> {
let mut out = Vec::with_capacity(values.len());
for (i, v) in values.into_iter().enumerate() {
push_keyed(&mut out, names, i, v);
}
out
}
fn caps_trimmed(caps: &Caps<'_>, names: &[Option<String>], fmt: CellFmt) -> Vec<(Value, Value)> {
let last = fmt.row_end(caps);
let mut out = Vec::with_capacity(last + 1);
for i in 0..=last {
push_keyed(&mut out, names, i, fmt.cell(caps, i));
}
out
}
fn fill_out(target: &Value, pos: usize, rows: Vec<(Value, Value)>) {
with_host(|h| {
let out = h.new_array();
for (k, v) in &rows {
h.arr_set_key(&out, k, v.clone());
}
h.byref_out_put(pos, out);
if h.is_array(target) {
h.arr_set_reindexed(target, vec![]);
for (k, v) in rows {
h.arr_set_key(target, &k, v);
}
}
});
}
fn preg_match(args: &[Value]) -> Result<Value, String> {
let pat = with_host(|h| h.to_str(&arg(args, 0)));
let subject = with_host(|h| h.to_str(&arg(args, 1)));
let fmt = CellFmt::from_flags(args.get(3).map(|v| v.to_int()).unwrap_or(0));
let Some(re) = compile_for("preg_match", &pat) else {
return Ok(Value::bool(false));
};
let Some(from) = start_offset(args.get(4), subject.len()) else {
return bad_offset(args);
};
match re.captures_first_from(subject.as_bytes(), from) {
Some(caps) => {
if args.len() > 2 {
fill_out(&args[2], 2, caps_trimmed(&caps, &re.group_names(), fmt));
}
Ok(Value::int(1))
}
None => {
if args.len() > 2 {
fill_out(&args[2], 2, vec![]);
}
Ok(Value::int(0))
}
}
}
fn start_offset(v: Option<&Value>, len: usize) -> Option<usize> {
let raw = v.map(|v| v.to_int()).unwrap_or(0);
if raw < 0 {
return Some((len as i64 + raw).max(0) as usize);
}
(raw as u64 <= len as u64).then_some(raw as usize)
}
fn bad_offset(args: &[Value]) -> Result<Value, String> {
with_host(|h| h.set_preg_error(PREG_INTERNAL_ERROR));
if args.len() > 2 {
fill_out(&args[2], 2, vec![]);
}
Ok(Value::bool(false))
}
fn preg_match_all(args: &[Value]) -> Result<Value, String> {
let pat = with_host(|h| h.to_str(&arg(args, 0)));
let subject = with_host(|h| h.to_str(&arg(args, 1)));
let flags = args.get(3).map(|v| v.to_int()).unwrap_or(0);
let Some(re) = compile_for("preg_match_all", &pat) else {
return Ok(Value::bool(false));
};
let names = re.group_names();
let fmt = CellFmt::from_flags(flags);
let Some(from) = start_offset(args.get(4), subject.len()) else {
return bad_offset(args);
};
let all: Vec<(Vec<Value>, usize)> = re
.captures_all_from(subject.as_bytes(), from)
.iter()
.map(|c| (caps_values(c, fmt), fmt.row_end(c)))
.collect();
let count = all.len();
if args.len() > 2 {
let group_count = re.captures_len(); let rows: Vec<(Value, Value)> = if flags & SET_ORDER != 0 {
all.into_iter()
.enumerate()
.map(|(i, (mut row, last))| {
row.truncate(last + 1);
(Value::int(i as i64), make_map(key_row(row, &names)))
})
.collect()
} else {
let mut rows = Vec::with_capacity(group_count);
for g in 0..group_count {
let col = make_list(all.iter().map(|(row, _)| row[g].clone()).collect());
push_keyed(&mut rows, &names, g, col);
}
rows
};
fill_out(&args[2], 2, rows);
}
Ok(Value::int(count as i64))
}
fn translate_replacement(s: &str) -> String {
let chars: Vec<char> = s.chars().collect();
let mut out = String::new();
let mut i = 0;
let take_digits = |from: usize| -> usize {
let mut j = from;
while j < chars.len() && chars[j].is_ascii_digit() {
j += 1;
}
j
};
while i < chars.len() {
match chars[i] {
'\\' => {
if i + 1 < chars.len() && chars[i + 1].is_ascii_digit() {
let j = take_digits(i + 1);
out.push_str("${");
out.extend(&chars[i + 1..j]);
out.push('}');
i = j;
} else if i + 1 < chars.len() && chars[i + 1] == '\\' {
out.push('\\');
i += 2;
} else {
out.push('\\');
i += 1;
}
}
'$' => {
if i + 1 < chars.len() && chars[i + 1] == '{' {
let j = take_digits(i + 2);
if j > i + 2 && j < chars.len() && chars[j] == '}' {
out.push_str("${");
out.extend(&chars[i + 2..j]);
out.push('}');
i = j + 1;
} else {
out.push_str("$$");
i += 1;
}
} else if i + 1 < chars.len() && chars[i + 1].is_ascii_digit() {
let j = take_digits(i + 1);
out.push_str("${");
out.extend(&chars[i + 1..j]);
out.push('}');
i = j;
} else {
out.push_str("$$");
i += 1;
}
}
c => {
out.push(c);
i += 1;
}
}
}
out
}
fn replace_one(re: &Pattern, repl: &[u8], subject: &[u8], limit: i64, count: &mut i64) -> Vec<u8> {
let mut out: Vec<u8> = Vec::new();
let mut last = 0usize;
for (n, caps) in re.captures_all(subject).into_iter().enumerate() {
if limit >= 0 && n as i64 >= limit {
break;
}
let whole = caps.get(0).expect("group 0 always participates");
out.extend_from_slice(&subject[last..whole.start()]);
caps.expand(repl, &mut out);
last = whole.end();
*count += 1;
}
out.extend_from_slice(&subject[last..]);
out
}
fn pattern_list(v: &Value) -> Vec<String> {
with_host(|h| {
if h.is_array(v) {
h.array_pairs(v)
.unwrap_or_default()
.into_iter()
.map(|(_, p)| h.to_str(&p))
.collect()
} else {
vec![h.to_str(v)]
}
})
}
fn preg_replace(args: &[Value]) -> Result<Value, String> {
let pats = pattern_list(&arg(args, 0));
let repl_arg = arg(args, 1);
let repls: Vec<String> = pattern_list(&repl_arg); let repl_is_array = with_host(|h| h.is_array(&repl_arg));
let limit = args.get(3).map(|v| v.to_int()).unwrap_or(-1);
let mut compiled: Vec<(Rc<Pattern>, String)> = Vec::with_capacity(pats.len());
for (idx, p) in pats.iter().enumerate() {
let Some(re) = compile_for("preg_replace", p) else {
return Ok(Value::Undef);
};
let repl = if repl_is_array {
repls.get(idx).cloned().unwrap_or_default()
} else {
repls.first().cloned().unwrap_or_default()
};
compiled.push((re, translate_replacement(&repl)));
}
let subj = arg(args, 2);
let mut count: i64 = 0;
let mut apply = |s: &str| -> String {
let mut cur: Vec<u8> = s.as_bytes().to_vec();
for (re, repl) in &compiled {
cur = replace_one(re, repl.as_bytes(), &cur, limit, &mut count);
}
bstr(&cur)
};
let out = if with_host(|h| h.is_array(&subj)) {
let pairs = with_host(|h| h.array_pairs(&subj)).unwrap_or_default();
make_map(
pairs
.into_iter()
.map(|(k, v)| (k, Value::str(apply(&with_host(|h| h.to_str(&v))))))
.collect(),
)
} else {
Value::str(apply(&with_host(|h| h.to_str(&subj))))
};
if args.len() > 4 {
with_host(|h| h.byref_out_put(4, Value::int(count)));
}
Ok(out)
}
fn preg_replace_callback(args: &[Value]) -> Result<Value, String> {
let pats = pattern_list(&arg(args, 0));
let cb = arg(args, 1);
let limit = args.get(3).map(|v| v.to_int()).unwrap_or(-1);
let mut compiled: Vec<Rc<Pattern>> = Vec::with_capacity(pats.len());
for p in &pats {
let Some(re) = compile_for("preg_replace_callback", p) else {
return Ok(Value::Undef);
};
compiled.push(re);
}
let subj = arg(args, 2);
let fmt = CellFmt::from_flags(args.get(5).map(|v| v.to_int()).unwrap_or(0));
let mut count: i64 = 0;
let run = |s: &str, count: &mut i64| -> Result<String, String> {
let mut cur = s.to_string();
for re in &compiled {
cur = replace_all_cb(re, &cur, &cb, limit, fmt, count)?;
}
Ok(cur)
};
let out = if with_host(|h| h.is_array(&subj)) {
let pairs = with_host(|h| h.array_pairs(&subj)).unwrap_or_default();
let mut out = Vec::with_capacity(pairs.len());
for (k, v) in pairs {
let s = with_host(|h| h.to_str(&v));
out.push((k, Value::str(run(&s, &mut count)?)));
}
make_map(out)
} else {
let s = with_host(|h| h.to_str(&subj));
Value::str(run(&s, &mut count)?)
};
if args.len() > 4 {
with_host(|h| h.byref_out_put(4, Value::int(count)));
}
Ok(out)
}
fn replace_all_cb(
re: &Pattern,
s: &str,
cb: &Value,
limit: i64,
fmt: CellFmt,
count: &mut i64,
) -> Result<String, String> {
let bytes = s.as_bytes();
let names = re.group_names();
let mut out: Vec<u8> = Vec::new();
let mut last = 0;
for (n, caps) in re.captures_all(bytes).into_iter().enumerate() {
if limit >= 0 && n as i64 >= limit {
break;
}
let whole = caps.get(0).unwrap();
out.extend_from_slice(&bytes[last..whole.start()]);
*count += 1;
let matches = make_map(caps_trimmed(&caps, &names, fmt));
let ret = crate::host::call_value(cb.clone(), vec![matches])?;
if crate::host::unwinding() {
return Ok(String::new());
}
out.extend_from_slice(with_host(|h| h.to_str(&ret)).as_bytes());
last = whole.end();
}
out.extend_from_slice(&bytes[last..]);
Ok(bstr(&out))
}
fn preg_split(args: &[Value]) -> Result<Value, String> {
let pat = with_host(|h| h.to_str(&arg(args, 0)));
let subject = with_host(|h| h.to_str(&arg(args, 1)));
let limit = args.get(2).map(|v| v.to_int()).unwrap_or(-1);
let flags = args.get(3).map(|v| v.to_int()).unwrap_or(0);
let Some(re) = compile_for("preg_split", &pat) else {
return Ok(Value::bool(false));
};
let no_empty = flags & SPLIT_NO_EMPTY != 0;
let delim_capture = flags & SPLIT_DELIM_CAPTURE != 0;
let offset_capture = flags & SPLIT_OFFSET_CAPTURE != 0;
let cap: usize = if limit <= 0 {
usize::MAX
} else {
limit as usize
};
let bytes = subject.as_bytes();
let mut pieces: Vec<(String, i64)> = Vec::new();
let mut last = 0usize;
for (n, caps) in re.captures_all(bytes).into_iter().enumerate() {
let whole = caps.get(0).unwrap();
if n + 1 >= cap {
break;
}
pieces.push((bstr(&bytes[last..whole.start()]), last as i64));
if delim_capture {
if let Some(last_grp) = (1..caps.len()).rfind(|&g| caps.get(g).is_some()) {
for g in 1..=last_grp {
match caps.get(g) {
Some(m) => pieces.push((bstr(m.as_bytes()), m.start() as i64)),
None => pieces.push((String::new(), -1)),
}
}
}
}
last = whole.end();
}
pieces.push((bstr(&bytes[last..]), last as i64));
let mut out: Vec<Value> = Vec::with_capacity(pieces.len());
for (piece, off) in pieces {
if no_empty && piece.is_empty() {
continue;
}
if offset_capture {
out.push(make_list(vec![Value::str(piece), Value::int(off)]));
} else {
out.push(Value::str(piece));
}
}
Ok(make_list(out))
}
fn preg_quote(args: &[Value]) -> Result<Value, String> {
let s = with_host(|h| h.to_str(&arg(args, 0)));
let delim = with_host(|h| h.to_str(&arg(args, 1)));
let delim_ch = delim.chars().next();
const SPECIAL: &str = ".\\+*?[^]$(){}=!<>|:-#";
let mut out = String::with_capacity(s.len());
for c in s.chars() {
if c == '\0' {
out.push_str("\\000");
} else if SPECIAL.contains(c) || Some(c) == delim_ch {
out.push('\\');
out.push(c);
} else {
out.push(c);
}
}
Ok(Value::str(out))
}
fn preg_grep(args: &[Value]) -> Result<Value, String> {
let pat = with_host(|h| h.to_str(&arg(args, 0)));
let input = arg(args, 1);
let flags = args.get(2).map(|v| v.to_int()).unwrap_or(0);
let invert = flags & GREP_INVERT != 0;
let Some(re) = compile_for("preg_grep", &pat) else {
return Ok(Value::bool(false));
};
let pairs = with_host(|h| h.array_pairs(&input)).unwrap_or_default();
let mut kept: Vec<(Value, Value)> = Vec::new();
for (k, v) in pairs {
let s = with_host(|h| h.to_str(&v));
if re.is_match(s.as_bytes()) != invert {
kept.push((k, v));
}
}
Ok(make_map(kept))
}
fn arg(args: &[Value], i: usize) -> Value {
args.get(i).cloned().unwrap_or(Value::Undef)
}
fn make_list(vals: Vec<Value>) -> Value {
with_host(|h| {
let arr = h.new_array();
for v in vals {
h.arr_push_auto(&arr, v);
}
arr
})
}
fn make_map(pairs: Vec<(Value, Value)>) -> Value {
with_host(|h| {
let arr = h.new_array();
for (k, v) in pairs {
h.arr_set_key(&arr, &k, v);
}
arr
})
}