use super::R;
use super::pattern::{Cap, MatchState, Matcher, Watch, find_plain, no_specials};
use crate::LimitKind;
use crate::value::{StrRef, Value};
use crate::bytecode::ArithOp;
use crate::vm::{Args, Lua, LuaError};
pub(super) fn open(lua: &mut Lua) {
let t = lua.lib_table("string");
let fns: [(&str, &'static str, crate::vm::NativeFn); 17] = [
("len", "string.len", len),
("sub", "string.sub", sub),
("upper", "string.upper", upper),
("lower", "string.lower", lower),
("rep", "string.rep", rep),
("reverse", "string.reverse", reverse),
("byte", "string.byte", byte),
("char", "string.char", char_),
("find", "string.find", find),
("match", "string.match", match_),
("gmatch", "string.gmatch", gmatch),
("gsub", "string.gsub", gsub),
("format", "string.format", super::strformat::format),
("pack", "string.pack", super::pack::pack),
("unpack", "string.unpack", super::pack::unpack),
("packsize", "string.packsize", super::pack::packsize),
("dump", "string.dump", dump),
];
for (n, q, f) in fns {
lua.set_fn(t, n, q, f);
}
register_in_place(lua, t, 0, IN_PLACE.get(..8).unwrap_or_default());
let Ok(meta) = lua.new_table(0, 16) else { return };
lua.set_field(meta, "__index", Value::Table(t));
let mms: [(&str, &'static str, crate::vm::NativeFn); 8] = [
("__add", "string.__add", |l, a| str_arith(l, a, ArithOp::Add)),
("__sub", "string.__sub", |l, a| str_arith(l, a, ArithOp::Sub)),
("__mul", "string.__mul", |l, a| str_arith(l, a, ArithOp::Mul)),
("__mod", "string.__mod", |l, a| str_arith(l, a, ArithOp::Mod)),
("__pow", "string.__pow", |l, a| str_arith(l, a, ArithOp::Pow)),
("__div", "string.__div", |l, a| str_arith(l, a, ArithOp::Div)),
("__idiv", "string.__idiv", |l, a| str_arith(l, a, ArithOp::IDiv)),
("__unm", "string.__unm", str_unm),
];
for (n, q, f) in mms {
lua.set_fn(meta, n, q, f);
}
lua.string_meta = Some(meta);
lua.ctx_epoch += 1;
}
pub(crate) fn posrelat_i(pos: i64, len: usize) -> usize {
if pos > 0 {
usize::try_from(pos).unwrap_or(usize::MAX)
} else if pos == 0 {
1
} else if pos.unsigned_abs() > len as u64 {
1
} else {
len - pos.unsigned_abs() as usize + 1
}
}
pub(crate) fn end_pos(pos: i64, len: usize) -> usize {
if pos > len as i64 {
len
} else if pos >= 0 {
pos as usize
} else if pos.unsigned_abs() > len as u64 {
0
} else {
len - pos.unsigned_abs() as usize + 1
}
}
fn len(lua: &mut Lua, a: Args) -> R {
let s = lua.check_str(a, 0)?;
let n = lua.bytes(s).len() as i64;
lua.ret1(Value::Int(n))
}
fn sub(lua: &mut Lua, a: Args) -> R {
let s = lua.check_str(a, 0)?;
let l = lua.bytes(s).len();
let i = posrelat_i(lua.check_int(a, 1)?, l);
let j = end_pos(lua.opt_int(a, 2, -1)?, l);
if i > j {
return lua.ret_str(b"");
}
lua.charge((j + 1).saturating_sub(i) as u64 / 64)?;
lua.ret_derived(s, i - 1, j, |src, dest| dest.copy_from_slice(src))
}
fn map_bytes(lua: &mut Lua, a: Args, f: impl Fn(u8) -> u8) -> R {
let s = lua.check_str(a, 0)?;
let l = lua.bytes(s).len();
lua.charge(l as u64 / 32)?;
lua.ret_mapped(s, |src, at, dest| {
for (d, &c) in dest.iter_mut().zip(src.get(at..).unwrap_or_default()) {
*d = f(c);
}
})
}
fn upper(lua: &mut Lua, a: Args) -> R {
map_bytes(lua, a, |c| c.to_ascii_uppercase())
}
fn lower(lua: &mut Lua, a: Args) -> R {
map_bytes(lua, a, |c| c.to_ascii_lowercase())
}
fn reverse(lua: &mut Lua, a: Args) -> R {
let s = lua.check_str(a, 0)?;
let l = lua.bytes(s).len();
lua.charge(l as u64 / 32)?;
lua.ret_mapped(s, |src, at, dest| {
let end = src.len().saturating_sub(at);
let from = end.saturating_sub(dest.len());
for (d, &c) in dest.iter_mut().zip(src.get(from..end).unwrap_or_default().iter().rev()) {
*d = c;
}
})
}
const REP_CHUNK: usize = super::COPY_PIECE;
fn rep(lua: &mut Lua, a: Args) -> R {
let s = lua.check_str(a, 0)?;
let n = lua.check_int(a, 1)?;
let sep = lua.opt_str(a, 2)?;
let (ls, lsep) = (lua.bytes(s).len(), sep.map_or(0, |x| lua.bytes(x).len()));
if n <= 0 || (ls == 0 && lsep == 0) {
return lua.ret_str(b"");
}
let total = (ls as u128 + lsep as u128) * n as u128 - lsep as u128;
if total > lua.limits.string_len as u128 {
return Err(LuaError::msg("resulting string too large"));
}
let total = total as usize;
lua.charge(total as u64 / 8 + 1)?;
let first = |h: &crate::heap::Heap, dest: &mut [u8]| {
let (sb, sepb) = (h.str_bytes(s), sep.map_or(&[][..], |x| h.str_bytes(x)));
let unit = (ls + lsep).min(dest.len());
let (a, b) = dest.split_at_mut(ls.min(unit));
a.copy_from_slice(sb.get(..a.len()).unwrap_or_default());
if let Some(b) = b.get_mut(..unit - a.len()) {
b.copy_from_slice(sepb.get(..b.len()).unwrap_or_default());
}
unit
};
let unit = ls + lsep;
let double = |dest: &mut [u8], done: usize, most: usize| {
let r = done % unit;
let k = (done - r).min(dest.len() - done).min(most);
dest.copy_within(r..r + k, done);
done + k
};
if total <= REP_CHUNK {
let v = lua.new_built(total, |h, dest| {
let mut done = first(h, dest);
while done < dest.len() {
done = double(dest, done, usize::MAX);
}
})?;
return lua.ret1(v);
}
let charged = crate::heap::str_mem(total);
lua.charge_mem(charged)?;
let mut buf = vec![0u8; total];
let mut done = 0;
for (part, len) in [(Some(s), ls), (sep, lsep)] {
let Some(part) = part else { continue };
let mut at = 0;
while at < len && done < total {
let end = at.saturating_add(REP_CHUNK).min(len).min(at + (total - done));
let src = lua.heap.str_bytes(part).get(at..end).unwrap_or_default();
if let Some(d) = buf.get_mut(done..done + src.len()) {
d.copy_from_slice(src);
}
done += end - at;
at = end;
if let Err(e) = lua.check_time() {
lua.heap.mem.release(charged);
return Err(e);
}
}
}
while done < total {
done = double(&mut buf, done, REP_CHUNK);
if let Err(e) = lua.check_time() {
lua.heap.mem.release(charged);
return Err(e);
}
}
let v = Value::Str(lua.heap.new_long_charged(buf.into_boxed_slice()));
lua.hold(v);
lua.ret1(v)
}
fn byte(lua: &mut Lua, a: Args) -> R {
let s = lua.check_str(a, 0)?;
let l = lua.bytes(s).len();
let raw = lua.opt_int(a, 1, 1)?;
let pe = end_pos(lua.opt_int(a, 2, raw)?, l);
let pi = posrelat_i(raw, l);
if pi > pe {
return lua.ret(&[]);
}
if pi == pe {
let c = lua.bytes(s).get(pi - 1).copied().unwrap_or(0);
return lua.ret1(Value::Int(i64::from(c)));
}
if pe - pi >= lua.limits.stack_slots / 2 {
return Err(LuaError::msg("string slice too long"));
}
let most = (pe - pi + 1).saturating_mul(16);
lua.charge_mem(most)?;
lua.heap.mem.release(most);
let mut hold = super::MemHold::default();
hold.cover(lua, most)?;
let n = pe - pi + 1;
lua.charge(n as u64 / 8)?;
let mut rets = std::mem::take(&mut lua.rets);
rets.clear();
rets.reserve(n);
let mut at = pi - 1;
while at < pe {
let end = (at + super::NATIVE_CHUNK).min(pe);
rets.extend(lua.bytes(s).get(at..end).unwrap_or_default().iter().map(|&c| Value::Int(i64::from(c))));
at = end;
if at < pe
&& let Err(e) = lua.check_time()
{
lua.rets = rets;
return Err(e);
}
}
lua.rets = rets;
Ok(crate::vm::Flow::Return)
}
fn char_(lua: &mut Lua, a: Args) -> R {
super::owe(a.n as u64 / 8);
let mut out = Vec::new();
let mut short = [0u8; crate::heap::SHORT_LEN];
for i in 0..a.n {
let c = lua.check_int(a, i)?;
if !(0..=255).contains(&c) {
return Err(LuaError::Arg(i + 1, "value out of range".into()));
}
match short.get_mut(i) {
Some(b) if a.n <= crate::heap::SHORT_LEN => *b = c as u8,
_ => out.push(c as u8),
}
}
if a.n <= crate::heap::SHORT_LEN {
return lua.ret_str(short.get(..a.n).unwrap_or_default());
}
lua.ret_str(&out)
}
fn dump(_lua: &mut Lua, _a: Args) -> R {
Err(LuaError::msg("unable to dump given function (RemedyLua has no bytecode format)"))
}
const STEPS_PER_FUEL: u64 = 4;
fn max_steps(len: usize) -> u64 {
4_000_000 + 64 * len as u64
}
fn step_cap(lua: &Lua, len: usize) -> u64 {
let fuel = lua.fuel_left();
max_steps(len).min(fuel.saturating_add(1).saturating_mul(STEPS_PER_FUEL))
}
fn pat_err(e: super::pattern::PatError) -> LuaError {
LuaError::Msg(e.0)
}
fn cap_value(lua: &mut Lua, s: StrRef, c: Cap) -> Result<Value, LuaError> {
match c {
Cap::Pos(p) => Ok(Value::Int(p as i64)),
Cap::Str(x, y) => {
lua.new_derived(s, x, y, |src, dest| dest.copy_from_slice(src))
}
}
}
fn find_aux(lua: &mut Lua, a: Args, find: bool) -> R {
let s = lua.check_str(a, 0)?;
let p = lua.check_str(a, 1)?;
let ls = lua.bytes(s).len();
let init = posrelat_i(lua.opt_int(a, 2, 1)?, ls);
if init > ls + 1 {
return lua.ret1(Value::Nil);
}
let plain = find && (lua.arg(a, 3).truthy() || no_specials(lua.bytes(p)));
if plain {
let budget = lua.fuel_left().saturating_mul(64).saturating_add(64);
let (r, work, stop) = find_plain(lua.bytes(s), lua.bytes(p), init - 1, budget, &Watch::of(lua));
lua.charge(work / 64 + 1)?;
if let Some(k) = stop {
return Err(LuaError::Limit(k));
}
return match r {
Some(i) => {
let lp = lua.bytes(p).len();
lua.ret(&[Value::Int(i as i64 + 1), Value::Int((i + lp) as i64)])
}
None => lua.ret1(Value::Nil),
};
}
let Some((st, e, caps)) = search(lua, s, p, init - 1, true, -1, !find, 1)? else { return lua.ret1(Value::Nil) };
let mut out = [Value::Nil; super::pattern::MAX_CAPTURES + 2];
let mut n = 0;
if find {
let [a, b, ..] = &mut out;
(*a, *b) = (Value::Int(st as i64 + 1), Value::Int(e as i64));
n = 2;
}
for c in caps.iter() {
let v = cap_value(lua, s, c)?;
if let Some(slot) = out.get_mut(n) {
*slot = v;
}
n += 1;
}
lua.ret(out.get(..n).unwrap_or_default())
}
fn find(lua: &mut Lua, a: Args) -> R {
find_aux(lua, a, true)
}
fn match_(lua: &mut Lua, a: Args) -> R {
find_aux(lua, a, false)
}
#[allow(clippy::too_many_arguments)]
#[inline(always)]
fn search(
lua: &mut Lua,
s: StrRef,
p: StrRef,
s1: usize,
anchor_ok: bool,
last: i64,
whole: bool,
setup: u64,
) -> Result<Option<(usize, usize, super::pattern::Captures)>, LuaError> {
let cap = step_cap(lua, lua.bytes(s).len());
let mut st = lua.match_state.take().unwrap_or_else(|| Box::new(MatchState::new(0, Watch::default())));
st.reuse(cap, Watch::of(lua));
let (found, steps, stop) = search_in(lua.bytes(s), lua.bytes(p), s1, anchor_ok, last, whole, &mut st);
lua.match_state = Some(st);
lua.charge(steps / STEPS_PER_FUEL + setup)?;
if let Some(k) = stop {
return Err(LuaError::Limit(k));
}
found.transpose().map_err(pat_err)
}
type Found = Option<Result<(usize, usize, super::pattern::Captures), super::pattern::PatError>>;
#[allow(clippy::too_many_arguments)]
#[inline(always)]
fn search_in(src: &[u8], pat: &[u8], mut s1: usize, anchor_ok: bool, last: i64, whole: bool, st: &mut MatchState) -> (Found, u64, Option<LimitKind>) {
let anchor = anchor_ok && pat.first() == Some(&b'^');
let pat = if anchor { pat.get(1..).unwrap_or_default() } else { pat };
let mut m = Matcher::new(src, pat, st);
let mut found = None;
while s1 <= src.len() {
match m.attempt(s1, !anchor) {
Err(e) => {
found = Some(Err(e));
break;
}
Ok(Some(e)) if e as i64 != last => {
found = Some(m.captures(s1, e, whole).map(|caps| (s1, e, caps)));
break;
}
_ => {}
}
if anchor {
break;
}
s1 += 1;
}
(found, m.steps, m.stop)
}
fn gmatch(lua: &mut Lua, a: Args) -> R {
let s = lua.check_str(a, 0)?;
let p = lua.check_str(a, 1)?;
let ls = lua.bytes(s).len();
let init = posrelat_i(lua.opt_int(a, 2, 1)?, ls);
let start = if init > ls + 1 { ls + 1 } else { init - 1 };
let f = lua.new_native(
gmatch_aux,
"gmatch_aux",
vec![Value::Str(s), Value::Str(p), Value::Int(start as i64), Value::Int(-1)],
)?;
lua.ret1(f)
}
fn gmatch_aux(lua: &mut Lua, a: Args) -> R {
let (Value::Str(s), Value::Str(p)) = (lua.native_upval(a, 0), lua.native_upval(a, 1)) else {
return lua.ret1(Value::Nil);
};
let pos = lua.native_upval(a, 2).as_int().unwrap_or(0).max(0) as usize;
let last = lua.native_upval(a, 3).as_int().unwrap_or(-1);
let Some((_, e, caps)) = search(lua, s, p, pos, false, last, true, 2)? else {
lua.set_native_upval(a, 2, Value::Int(lua.bytes(s).len() as i64 + 1));
return lua.ret(&[]);
};
lua.set_native_upval(a, 2, Value::Int(e as i64));
lua.set_native_upval(a, 3, Value::Int(e as i64));
let mut out = [Value::Nil; super::pattern::MAX_CAPTURES];
let mut n = 0;
for (slot, c) in out.iter_mut().zip(caps.iter()) {
*slot = cap_value(lua, s, c)?;
n += 1;
}
lua.ret(out.get(..n).unwrap_or_default())
}
fn gsub(lua: &mut Lua, a: Args) -> R {
let mut out = super::OutBuf::new(64);
let mut steps = 0u64;
let r = gsub_into(lua, a, &mut out, &mut steps);
super::owe(steps / STEPS_PER_FUEL + 1);
let n = r?;
let b = out.into_vec(lua);
let r = lua.new_owned(b)?;
lua.ret(&[r, Value::Int(n)])
}
fn repl_len(rs: &[u8], m: &Matcher<'_>, pos: usize, e: usize) -> usize {
let (mut n, mut i) = (0usize, 0);
while i < rs.len() {
if rs.get(i) != Some(&b'%') {
n += 1;
i += 1;
continue;
}
n += match rs.get(i + 1).copied().unwrap_or(0) {
b'0' => e - pos,
d @ b'1'..=b'9' => match m.capture(usize::from(d - b'1'), pos, e) {
Ok(Cap::Str(x, y)) => y - x,
Ok(Cap::Pos(p)) => p.to_string().len(),
Err(_) => 0,
},
_ => 1,
};
i += 2;
}
n
}
fn gsub_into(lua: &mut Lua, a: Args, out: &mut super::OutBuf, steps_total: &mut u64) -> Result<i64, LuaError> {
let s = lua.check_str(a, 0)?;
let p = lua.check_str(a, 1)?;
let repl = lua.arg(a, 2);
match repl {
Value::Str(_) | Value::Int(_) | Value::Num(_) | Value::Table(_) | Value::Func(_) => {}
_ => return Err(lua.type_error(a, 2, "string/function/table")),
}
let repl_str = match repl {
Value::Int(_) | Value::Num(_) => Some(lua.check_str(a, 2)?),
Value::Str(r) => Some(r),
_ => None,
};
let ls = lua.bytes(s).len();
let max_n = lua.opt_int(a, 3, ls as i64 + 1)?;
let anchor = lua.bytes(p).first() == Some(&b'^');
let calls = matches!(repl, Value::Table(_) | Value::Func(_));
let limit = lua.limits.string_len;
let mut n = 0i64;
let mut pos = 0usize;
let mut last: Option<usize> = None;
let mut state = MatchState::new(step_cap(lua, ls), Watch::of(lua));
let watch = Watch::of(lua);
let (mut since, mut over) = (0usize, false);
let mut carried: Option<Option<usize>> = None;
enum Stop {
End,
Fail(super::pattern::PatError),
Charge,
Call(usize),
Reserve(usize),
}
loop {
let stop = {
let src = lua.heap.str_bytes(s);
let pat_all = lua.heap.str_bytes(p);
let pat = if anchor { pat_all.get(1..).unwrap_or_default() } else { pat_all };
let rs = repl_str.map_or(&[][..], |r| lua.heap.str_bytes(r));
let mut m = Matcher::new(src, pat, &mut state);
loop {
let r = match carried.take() {
Some(r) => r,
None => {
if n >= max_n {
break Stop::End;
}
m.steps = 0;
let r = m.attempt(pos, !anchor);
*steps_total += m.steps;
let r = match r {
Ok(r) => r,
Err(e) => break Stop::Fail(e),
};
if *steps_total > 1 << 12 {
carried = Some(r);
break Stop::Charge;
}
r
}
};
match r {
Some(e) if Some(e) != last => {
if !calls {
let need = repl_len(rs, &m, pos, e).min((limit + 1).saturating_sub(out.len()));
if out.len().saturating_add(need) > out.capacity() {
carried = Some(r);
break Stop::Reserve(need);
}
}
n += 1;
if calls {
break Stop::Call(e);
}
let whole = src.get(pos..e).unwrap_or_default();
*steps_total += rs.len() as u64;
let mut put = |out: &mut super::OutBuf, b: &[u8]| -> Result<(), LuaError> {
over = over || out.len() + b.len() > limit;
for piece in b.chunks(super::COPY_PIECE).take_while(|_| !over) {
out.extend_from_slice(piece);
since += piece.len();
if since >= super::COPY_PIECE {
since = 0;
if let Some(k) = watch.stop() {
return Err(LuaError::Limit(k));
}
}
}
Ok(())
};
let mut i = 0;
while i < rs.len() {
let c = rs.get(i).copied().unwrap_or(0);
if c != b'%' {
let run = rs.get(i..).unwrap_or_default().iter().position(|&b| b == b'%').map_or(rs.len(), |k| i + k);
put(out, rs.get(i..run).unwrap_or_default())?;
i = run;
continue;
}
i += 1;
let d = rs.get(i).copied().unwrap_or(0);
if d == b'%' {
out.push(b'%');
} else if d.is_ascii_digit() {
if d == b'0' {
put(out, whole)?;
} else {
let idx = usize::from(d - b'1');
match m.capture(idx, pos, e).map_err(pat_err)? {
Cap::Str(x, y) => put(out, src.get(x..y).unwrap_or_default())?,
Cap::Pos(p) => put(out, p.to_string().as_bytes())?,
}
}
} else {
return Err(LuaError::msg("invalid use of '%' in replacement string"));
}
i += 1;
}
pos = e;
last = Some(e);
}
_ => {
if pos < src.len() && out.len() == out.capacity() {
carried = Some(r);
break Stop::Reserve(1);
}
if pos < src.len() {
out.push(src.get(pos).copied().unwrap_or(0));
pos += 1;
} else {
break Stop::End;
}
}
}
if over || out.len() > limit {
return Err(LuaError::Limit(LimitKind::Memory));
}
if anchor {
break Stop::End;
}
}
};
match stop {
Stop::End => break,
Stop::Fail(e) => {
if let Some(k) = state.stop {
return Err(LuaError::Limit(k));
}
return Err(pat_err(e));
}
Stop::Reserve(need) => out.reserve(lua, need)?,
Stop::Charge => {
lua.charge(*steps_total / STEPS_PER_FUEL)?;
*steps_total = 0;
out.pay(lua)?;
}
Stop::Call(e) => {
out.pay(lua)?;
*steps_total += 4 * STEPS_PER_FUEL;
let mut args = [Value::Nil; super::pattern::MAX_CAPTURES];
let ncap = if matches!(repl, Value::Table(_)) { 1 } else { state.level.max(1) };
for (i, slot) in args.iter_mut().enumerate().take(ncap) {
*slot = match state.capture(i, pos, e).map_err(pat_err)? {
Cap::Pos(p) => Value::Int(p as i64),
Cap::Str(x, y) => lua.new_derived(s, x, y, |src, dest| dest.copy_from_slice(src))?,
};
}
let v = if let Value::Table(_) = repl {
let v = lua.index_nested(repl, args.first().copied().unwrap_or_default())?;
lua.charge_probes()?;
v
} else {
lua.call_nested1(repl, args.get(..ncap).unwrap_or_default())?
};
match v {
Value::Nil | Value::Bool(false) => copy_out(lua, out, s, pos, e, limit)?,
Value::Str(r) => copy_out(lua, out, r, 0, usize::MAX, limit)?,
Value::Int(_) | Value::Num(_) => lua.append_stringable(&mut **out, v),
other => {
return Err(LuaError::msg(format!("invalid replacement value (a {})", other.type_name())));
}
}
pos = e;
last = Some(e);
if out.len() > limit {
return Err(LuaError::Limit(LimitKind::Memory));
}
if anchor {
break;
}
}
}
}
copy_out(lua, out, s, pos, usize::MAX, limit)?;
Ok(n)
}
fn copy_out(lua: &mut Lua, out: &mut super::OutBuf, s: StrRef, from: usize, to: usize, limit: usize) -> Result<(), LuaError> {
let n = lua.bytes(s).len().min(to).saturating_sub(from);
if out.len().saturating_add(n) > limit {
return Err(LuaError::Limit(LimitKind::Memory));
}
out.reserve(lua, n)?;
lua.extend_watched(&mut **out, s, from, from + n)
}
fn str_arith(lua: &mut Lua, a: Args, op: ArithOp) -> R {
let (x, y) = (lua.arg(a, 0), lua.arg(a, 1));
let v = lua.string_arith_now(op, x, y)?;
lua.ret1(v)
}
fn str_unm(lua: &mut Lua, a: Args) -> R {
let x = lua.arg(a, 0);
match lua.tonumber_charged(x)? {
Some(Value::Int(i)) => lua.ret1(Value::Int(i.wrapping_neg())),
Some(Value::Num(f)) => lua.ret1(Value::Num(-f)),
_ => Err(LuaError::msg(format!("attempt to unm a '{}' with a '{}'", x.type_name(), lua.arg(a, 1).type_name()))),
}
}
pub(crate) const IN_PLACE: [&str; 10] = ["byte", "sub", "find", "match", "upper", "lower", "rep", "char", "tostring", "type"];
pub(crate) fn register_in_place(lua: &mut Lua, t: crate::value::TableRef, first: usize, names: &[&str]) {
for (i, n) in names.iter().enumerate() {
let f = lua.new_str(n.as_bytes()).map_or(Value::Nil, |k| lua.raw_get(t, k));
if let Some(fr) = f.as_func()
&& let Some(crate::heap::Func::Native { in_place, .. }) = lua.heap.funcs.get_mut(fr.idx)
{
*in_place = u8::try_from(first + i + 1).unwrap_or(0);
}
}
}
const IN_PLACE_STEPS: u64 = super::pattern::WATCH_EVERY - 1;
const IN_PLACE_WORK: u64 = super::pattern::WATCH_EVERY * 16 - 1;
pub(crate) fn in_place(
heap: &mut crate::heap::Heap,
types: &[StrRef; 8],
ms: &mut Option<Box<MatchState>>,
op: usize,
args: &[Value],
fuel: i64,
string_len: usize,
) -> Option<([Value; 2], usize, i64)> {
if crate::table::take_probes() != 0 {
return None;
}
let arg = |i: usize| args.get(i).copied().unwrap_or(Value::Nil);
let opt_int = |i: usize, d: i64| match arg(i) {
Value::Nil => Some(d),
Value::Int(n) => Some(n),
_ => None,
};
let one = |v: Value| Some(([v, Value::Nil], 1, 0));
let r = (|| match (op, arg(0)) {
(7, _) => {
let mut buf = [0u8; 4];
for (d, v) in buf.iter_mut().zip(args) {
let Value::Int(c @ 0..=255) = *v else { return None };
*d = c as u8;
}
made(heap, buf.get(..args.len())?, 0, fuel, string_len)
}
(8, v) => {
args.first()?;
let mut buf = [0u8; 24];
let (text, pre): (&[u8], u64) = match v {
Value::Int(i) => {
let mut w: &mut [u8] = &mut buf;
let _ = crate::vm::write_int(&mut w, i);
let n = 24 - w.len();
(buf.get(..n)?, 0)
}
Value::Num(f) => {
let t = crate::number::fmt_float(f);
let b = buf.get_mut(..t.len())?;
b.copy_from_slice(t.as_bytes());
(b, crate::vm::FLOAT_TEXT_FUEL)
}
Value::Bool(b) => (if b { b"true" } else { b"false" }, 0),
Value::Nil => (b"nil", 0),
_ => return None,
};
made(heap, text, pre, fuel, string_len)
}
(9, v) => {
args.first()?;
one(Value::Str(*types.get(crate::stdlib::type_index(v))?))
}
(_, Value::Str(s)) => {
let l = heap.str_bytes(s).len();
match op {
0 => {
let raw = opt_int(1, 1)?;
let (pe, pi) = (end_pos(opt_int(2, raw)?, l), posrelat_i(raw, l));
if pi > pe {
return Some(([Value::Nil; 2], 0, 0));
}
(pi == pe).then_some(())?;
one(Value::Int(i64::from(*heap.str_bytes(s).get(pi - 1)?)))
}
1 => {
let Value::Int(i) = arg(1) else { return None };
let (i, j) = (posrelat_i(i, l), end_pos(opt_int(2, -1)?, l));
if i > j {
return made(heap, b"", 0, fuel, string_len);
}
let (buf, n) = short_copy(heap.str_bytes(s).get(i - 1..j)?)?;
made(heap, buf.get(..n)?, (j + 1 - i) as u64 / 64, fuel, string_len)
}
4 | 5 => {
let (mut buf, n) = short_copy(heap.str_bytes(s))?;
for c in buf.iter_mut() {
*c = if op == 4 { c.to_ascii_uppercase() } else { c.to_ascii_lowercase() };
}
made(heap, buf.get(..n)?, l as u64 / 32, fuel, string_len)
}
6 => {
let Value::Int(n) = arg(1) else { return None };
let sep = match arg(2) {
Value::Nil => b"" as &[u8],
Value::Str(x) => heap.str_bytes(x),
_ => return None,
};
if n <= 0 || (l == 0 && sep.is_empty()) {
return made(heap, b"", 0, fuel, string_len);
}
let total = (l as u128 + sep.len() as u128) * n as u128 - sep.len() as u128;
if total > crate::heap::SHORT_LEN as u128 {
return None;
}
let mut buf = [0u8; crate::heap::SHORT_LEN];
let mut w: &mut [u8] = &mut buf;
for k in 0..n {
if k > 0 {
std::io::Write::write_all(&mut w, sep).ok()?;
}
std::io::Write::write_all(&mut w, heap.str_bytes(s)).ok()?;
}
made(heap, buf.get(..total as usize)?, total as u64 / 8 + 1, fuel, string_len)
}
_ => {
let Value::Str(p) = arg(1) else { return None };
let init = posrelat_i(opt_int(2, 1)?, l);
if init > l + 1 {
return one(Value::Nil);
}
let find = op == 2;
if find && (arg(3).truthy() || no_specials(heap.str_bytes(p))) {
let (hay, needle) = (heap.str_bytes(s), heap.str_bytes(p));
let budget = u64::try_from(fuel).ok()?.saturating_mul(64).saturating_add(64).min(IN_PLACE_WORK);
if hay.len() - (init - 1) > IN_PLACE_WORK as usize {
return None;
}
let (r, work, _) = find_plain(hay, needle, init - 1, budget, &Watch::NONE);
if work > budget {
return None;
}
let out = match r {
Some(i) => ([Value::Int(i as i64 + 1), Value::Int((i + needle.len()) as i64)], 2),
None => ([Value::Nil; 2], 1),
};
return in_budget(out.0, out.1, work / 64 + 1, fuel);
}
let cap = max_steps(l).min(u64::try_from(fuel).ok()?.saturating_add(1).saturating_mul(STEPS_PER_FUEL)).min(IN_PLACE_STEPS);
let mut st = ms.take().unwrap_or_else(|| Box::new(MatchState::new(0, Watch::default())));
st.reuse_keeping_watch(cap);
let (found, steps, _) = search_in(heap.str_bytes(s), heap.str_bytes(p), init - 1, true, -1, !find, &mut st);
*ms = Some(st);
if steps > cap {
return None;
}
let cost = steps / STEPS_PER_FUEL + 1;
let Some((st, e, caps)) = found.transpose().ok()? else { return in_budget([Value::Nil; 2], 1, cost, fuel) };
let (mut out, mut n) = if find { ([Value::Int(st as i64 + 1), Value::Int(e as i64)], 2) } else { ([Value::Nil; 2], 0) };
for c in caps.iter() {
*out.get_mut(n)? = match c {
Cap::Pos(p) => Value::Int(p as i64),
Cap::Str(x, y) => {
let (buf, len) = short_copy(heap.str_bytes(s).get(x..y)?)?;
(len <= string_len).then_some(())?;
Value::Str(heap.find_short(buf.get(..len)?)?)
}
};
n += 1;
}
in_budget(out, n, cost, fuel)
}
}
}
_ => None,
})();
if r.is_none() {
let _ = crate::table::take_probes();
}
r
}
fn short_copy(b: &[u8]) -> Option<([u8; crate::heap::SHORT_LEN], usize)> {
let mut buf = [0u8; crate::heap::SHORT_LEN];
buf.get_mut(..b.len())?.copy_from_slice(b);
Some((buf, b.len()))
}
fn in_budget(out: [Value; 2], n: usize, cost: u64, fuel: i64) -> Option<([Value; 2], usize, i64)> {
let total = i64::try_from(cost.saturating_add(crate::table::take_probes())).unwrap_or(i64::MAX);
(total < fuel).then_some((out, n, total))
}
fn made(heap: &mut crate::heap::Heap, bytes: &[u8], pre: u64, fuel: i64, string_len: usize) -> Option<([Value; 2], usize, i64)> {
if bytes.len() > string_len || bytes.len() > crate::heap::SHORT_LEN || i64::try_from(pre).ok()? >= fuel {
return None;
}
let r = heap.intern(bytes).ok()?;
Some(([Value::Str(r), Value::Nil], 1, i64::try_from(pre.saturating_add(crate::table::take_probes())).unwrap_or(i64::MAX)))
}