Commit 3f4c596d
3f4c596dedc7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
Merge pull request #1003 from ydah/fix-880-string-scan-captures
fix(runtime,analyze,codegen): nest String#scan captures
Details
- Commit
- 3f4c596dedc7
- Parent
- 7d668587e609
- Merge parent
- 535372347118
lib/sp_runtime.hmodifiedView file
@@ -2540,6 +2540,43 @@
25402540
static void sp_PolyArray_fin(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; free(a->data); }25412541
static sp_PolyArray *sp_PolyArray_new(void) { sp_PolyArray *a = (sp_PolyArray *)sp_gc_alloc(sizeof(sp_PolyArray), sp_PolyArray_fin, sp_PolyArray_scan); a->cap = 16; a->data = (sp_RbVal *)malloc(sizeof(sp_RbVal) * a->cap); if (!a->data) sp_oom_die(); a->len = 0; { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } return a; }25422542
static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (!a) return; if (a->len >= a->cap) { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; a->cap = a->cap * 2 + 1; void *nd = realloc(a->data, sizeof(sp_RbVal) * a->cap); if (!nd) sp_oom_die(); a->data = (sp_RbVal *)nd; h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } a->data[a->len++] = v; }2543+
static sp_PolyArray *sp_re_scan_poly(mrb_regexp_pattern *pat, const char *str) {2544+
sp_PolyArray *arr = sp_PolyArray_new();2545+
SP_GC_ROOT(arr);2546+
int64_t slen = (int64_t)strlen(str);2547+
int64_t pos = 0;2548+
int ncaps = 64;2549+
int caps[64];2550+
while (pos <= slen) {2551+
int n = re_exec(pat, str, slen, pos, caps, ncaps);2552+
if (n <= 0 || caps[0] < 0) break;2553+
int pairs = (n > ncaps ? ncaps : n) / 2;2554+
if (pairs <= 1) {2555+
int len = caps[1] - caps[0];2556+
char *m = sp_str_alloc_raw(len + 1);2557+
memcpy(m, str + caps[0], len);2558+
m[len] = 0;2559+
sp_PolyArray_push(arr, sp_box_str(m));2560+
} else {2561+
sp_PolyArray *row = sp_PolyArray_new();2562+
for (int gi = 1; gi < pairs; gi++) {2563+
if (caps[gi * 2] >= 0 && caps[gi * 2 + 1] >= 0) {2564+
int glen = caps[gi * 2 + 1] - caps[gi * 2];2565+
char *gm = sp_str_alloc_raw(glen + 1);2566+
memcpy(gm, str + caps[gi * 2], glen);2567+
gm[glen] = 0;2568+
sp_PolyArray_push(row, sp_box_str(gm));2569+
} else {2570+
sp_PolyArray_push(row, sp_box_nil());2571+
}2572+
}2573+
sp_PolyArray_push(arr, sp_box_poly_array(row));2574+
}2575+
pos = caps[1];2576+
if (caps[0] == caps[1]) pos++;2577+
}2578+
return arr;2579+
}25432580
static sp_PolyArray *sp_re_match_data(mrb_regexp_pattern *pat, const char *str) {25442581
int64_t slen = (int64_t)strlen(str);25452582
int ncaps = 64;spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_scan_capture.rbaddedView file
Loading diff…
test/string_scan_capture.rb.expectedaddedView file
Loading diff…