Repository navigation
fix(pattern): honour ^ anchor in gsub and literal caret in gmatch - #406
Conversation
Pattern.compile/1 returns an {anchored, elements} tuple, but only find/3
used the flag - gsub/4, gsub_stateful/5, and gmatch/2 discarded it.
Because compile strips the leading ^, those entry points matched the
remaining pattern at every position, so string.gsub("xax", "^x", "Y")
returned "YaY", 2 instead of "Yax", 1, silently corrupting the common
leading-trim idiom s:gsub("^%s+", "").
Route gsub and gsub_stateful through an anchored branch that tries the
match only at position 0 and replaces at most once, mirroring PUC-Lua
str_gsub's anchor flag. An n of 0 still suppresses the replacement, and
an empty anchored match (e.g. ^a* on "bbb") replaces once at the start.
gmatch needed the opposite treatment: per Lua 5.3 (and PUC-Lua's
gmatch_aux, which never strips the caret), a leading ^ in gmatch is not
an anchor but an ordinary literal character - so recompile the pattern
keeping the caret literal instead of dropping it.
Expected values in the regression tests are verified against PUC-Lua
5.3.6.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F3LBtzmXgAMHA8QiGPa5Ek
🤖 Automated review — Opus + Sonnet consolidated findingsTwo independent AI reviewers (Opus, Sonnet) reviewed this PR, both by differential testing against a real Lua interpreter. The fix is correct: a 10,200-case differential fuzz (20 subjects × 34 Everything below is take-it-or-leave-it polish, no blockers: 🟡 LOW
Suggested test additions (all verified to behave correctly already — just unpinned)
Out of scope (observed while testing; will be filed as issues, not this PR's problem)
|
The anchored gsub path was only pinned at n = 0, and the literal-caret gmatch path had no coverage for captures, quantifiers, or a trailing end anchor. Add the missing cases: - anchored gsub with n > 1 (2, 3, 5) still replaces at most once and reports a count of 1 — the core claim of the anchor fix - anchored gsub with a negative n leaves the subject untouched, count 0 - anchored gsub with a table replacement, which previously never reached the anchored branch in any test - an anchored pattern whose trailing $ fails to match, count 0 - gmatch literal caret with a capture, yielding each captured letter - gmatch literal caret with a quantifier bound to it - gmatch literal caret followed by an end anchor - Pattern.gsub/4 directly, for max_n above 1 and for a negative max_n Also correct a spec citation: the gmatch caret rule lives in the string.gmatch entry of Lua 5.3 §6.4, not in a nonexistent §6.4.3. All expected values were verified against the reference interpreter (PUC-Lua 5.4.8 and 5.5.0, which agree on every case here) before being written into the assertions. No lib behaviour changes. Claude-Session: https://claude.ai/code/session_01BYHGFLoHBJAsUrTzjVp5nC
|
Thanks for this — nice catch, and the diagnosis is exactly right. I ran a differential fuzz of the patched I pushed one test-only commit (
All expected values were taken from the reference interpreter rather than written by hand. I also corrected the No |
|
awesome, thank you! |
What
Fix two related
stringpattern bugs where a leading^was mishandled:string.gsubignored the^anchor. An anchored pattern onlymatches at the start of the subject (Lua 5.3 §6.4.1), so
gsubmustattempt at most one replacement at position 0 and leave the remainder
untouched. We were discarding the
anchoredflag fromcompile/1andscanning every position, so
("hello"):gsub("^h", "H")-style callsreplaced interior matches too.
string.gmatchtreated a leading^as an anchor. Ingmatchaleading
^has no special meaning (Lua 5.3 §6.4.3) — PUC-Lua'sgmatch_auxnever strips it, somatchsees it as an ordinarycharacter. We were honouring it as an anchor, which prevented iteration.
How
gsub/gsub_statefulnow route through a newdo_gsub/6thatbranches on the
anchoredflag:untouched remainder, return a count of 0 or 1.
max_n <= 0short-circuits to zero replacements.
gsub_from/10full scan.gmatchrecompiles the pattern with the caret kept literal whencompile/1reports it as anchored, mirroring PUC-Lua's behaviour.Tests
New
test/lua/vm/stdlib/pattern_anchor_test.exscovering:string.gsub— leading-only replacement, no match whenthe subject doesn't start with the pattern, multi-char runs replaced at
most once, empty anchored match, leading-whitespace trim preserving
interior/trailing whitespace,
n = 0suppression, captures reaching afunction replacement exactly once, and whole-subject matches.
string.gmatch—^does not anchor and matchesonly as a literal character.
Pattern.gsub/4— at-most-once replacement andmax_n = 0.