Commit 000c15df
000c15df2ed2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
fix(regexp): track per-thread sp so quantifiers over UTF-8 chars match correctly
Stage 5 of issue #61. After stage 2 added UTF-8 char-class support,
[utf8 chars]+ (or any quantifier consuming a multi-byte char) still
matched only one char per attempt. Cause: the Pike VM's outer loop
steps the input pointer one byte at a time, but a class match over a
3-byte UTF-8 char enqueues its retry thread at sp+3. The retry then
ran at sp+1 on the next iteration (mid-multibyte) and died — every
gsub over /[₀-₉]+/ produced one substitution per UTF-8 digit
instead of one per run.
Fix: each thread now carries its own input position (sp). The outer
loop only dispatches a thread when its sp matches the loop's sp;
otherwise the thread is copied unchanged into the next iteration's
list. RE_CHAR (1-byte advance) and RE_CLASS over a UTF-8 char (3-byte
advance) coexist cleanly because each thread waits for its own time.
re_thread_cache grows a const char *sp field. add_thread writes
it. The dispatch site copies non-ready threads to next via plain
struct copy. No changes to the cache allocation (the size is computed
from sizeof(re_thread)) or to the BT engine (it doesn't use threads).
test/regex_utf8_quantifier.rb covers the gsub/scan reproducer plus
{n} repeat, mixed ASCII/UTF-8 classes, and codepoint ranges.
Refs #61
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 000c15df2ed2
- Parent
- 28e52f2452aa
lib/regexp/re_exec.cmodifiedView file
re_thread *t = &list->threads[list->count++]; t->pc = pc; t->cap_slot = cap_slot; t->sp = sp; }} for (int i = 0; i < curr.count; i++) { re_thread *th = &curr.threads[i]; if (th->pc >= pat->code_len) continue; /* A thread enqueued at sp+advance (RE_CLASS over a multi-byte char) waits in the list until the byte-stepped outer sp catches up to its own sp. Until then, carry it forward to next iteration's curr unchanged. */ if (th->sp != sp) { if (next.count < next.capa) { next.threads[next.count++] = *th; } continue; } re_inst inst = pat->code[th->pc]; switch (inst.op) {lib/regexp/re_internal.hmodifiedView file
Loading diff…
test/regex_utf8_quantifier.rbaddedView file
Loading diff…