Commit 9b245eb2
9b245eb2db6eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
regexp: import mruby bug fixes + pick best match per Ruby semantics
Three regex engine fixes:
-
re_compile.c first_set_walk: epsilon-transition reach of RE_MATCH means the regex can match zero characters at any position; skipping bytes outside the first-byte set would skip past valid empty-match positions, so bail out instead. Imported from mruby d21eceb286.
-
re_compile.c insert_inst: an offset equal to
posalready points to the inserted instruction's new slot, so don't bump it; bumping shifts the target onto whatever code got displaced. Imported from mruby b9b8186f00. -
re_exec.c RE_MATCH (issue #909): pick the best match per Ruby semantics instead of letting the LAST MATCH overwrite. Earliest start position wins; within the same start, longest length (greedy) wins. Without this,
"abc" =~ /\w\w/reported the match at position 1 instead of 0 because a later-starting Pike VM thread clobbered the earlier-starting match's captures. Greedy quantifiers (\w+, scan loops) still work because the longer match at the same start wins.
Fixes #909
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 9b245eb2db6e
- Parent
- 15b216b66030
lib/regexp/re_compile.cmodifiedView file
c->code[pos].a = a; c->code[pos].offset = offset; /* fix all jump targets that point at or past the insertion point. Issue #824: previously only JMP/SPLIT/SPLITNG offsets were patched, leaving LOOKAHEAD/NEG_LOOKAHEAD/LOOKBEHIND/NEG_LOOKBEHIND dangling (their offset is the jump-to-end target). */ /* Fix jump targets that point past the insertion point. An offset equal to `pos` already points to the inserted instruction's new location and must not be bumped -- bumping it would shift the target to whatever code got displaced by the insertion (e.g. the body of the quantified atom), corrupting "skip past this atom" jumps emitted earlier. Imported from mruby b9b8186f00. Issue #824: also patches LOOKAHEAD/NEG_LOOKAHEAD/LOOKBEHIND/ NEG_LOOKBEHIND (their offset is the jump-to-end target). */ for (uint32_t i = 0; i < c->code_len; i++) { if (i == pos) continue; switch (c->code[i].op) { case RE_JMP: case RE_SPLIT: case RE_SPLITNG: case RE_LOOKAHEAD: case RE_NEG_LOOKAHEAD: case RE_LOOKBEHIND: case RE_NEG_LOOKBEHIND: if (c->code[i].offset >= pos && c->code[i].offset < 0xffff) { if (c->code[i].offset > pos && c->code[i].offset < 0xffff) { c->code[i].offset++; } break; case RE_ANY: case RE_ANY_NL: return FALSE; /* any byte possible */ case RE_MATCH: return TRUE; /* empty match; first_bytes still valid for other branches */ /* Reaching MATCH via epsilon transitions means the regex can match zero characters at any position. Skipping bytes that aren't in the first-byte set would skip past valid empty-match positions, so the optimization isn't safe -- bail out and accept any starting byte. Imported from mruby d21eceb286. */ return FALSE; default: return FALSE; } } return TRUE; /* Walked off the end without hitting MATCH or a consuming op. Treat as empty-matchable, same as RE_MATCH. */ return FALSE;}static mrb_boollib/regexp/re_exec.cmodifiedView file
Loading diff…
test/regex_unanchored_class_seq.rbaddedView file
Loading diff…
test/regex_unanchored_class_seq.rb.expectedaddedView file
Loading diff…