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

@@ -207,6 +207,7 @@
207207 re_thread *t = &list->threads[list->count++];
208208 t->pc = pc;
209209 t->cap_slot = cap_slot;
210+ t->sp = sp;
210211 }
211212 }
212213
@@ -326,6 +327,16 @@
326327 for (int i = 0; i < curr.count; i++) {
327328 re_thread *th = &curr.threads[i];
328329 if (th->pc >= pat->code_len) continue;
330+ /* A thread enqueued at sp+advance (RE_CLASS over a multi-byte
331+ char) waits in the list until the byte-stepped outer sp
332+ catches up to its own sp. Until then, carry it forward to
333+ next iteration's curr unchanged. */
334+ if (th->sp != sp) {
335+ if (next.count < next.capa) {
336+ next.threads[next.count++] = *th;
337+ }
338+ continue;
339+ }
329340
330341 re_inst inst = pat->code[th->pc];
331342 switch (inst.op) {