Commit 53537234
535372347118Merged
ydah <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
runtime,analyze,codegen: nest String#scan captures
Details
- Commit
- 535372347118
- Parent
- 4ff12b676fbe
lib/sp_runtime.hmodifiedView file
@@ -2711,6 +2711,43 @@
27112711
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); }27122712
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; }27132713
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; }2714+
static sp_PolyArray *sp_re_scan_poly(mrb_regexp_pattern *pat, const char *str) {2715+
sp_PolyArray *arr = sp_PolyArray_new();2716+
SP_GC_ROOT(arr);2717+
int64_t slen = (int64_t)strlen(str);2718+
int64_t pos = 0;2719+
int ncaps = 64;2720+
int caps[64];2721+
while (pos <= slen) {2722+
int n = re_exec(pat, str, slen, pos, caps, ncaps);2723+
if (n <= 0 || caps[0] < 0) break;2724+
int pairs = (n > ncaps ? ncaps : n) / 2;2725+
if (pairs <= 1) {2726+
int len = caps[1] - caps[0];2727+
char *m = sp_str_alloc_raw(len + 1);2728+
memcpy(m, str + caps[0], len);2729+
m[len] = 0;2730+
sp_PolyArray_push(arr, sp_box_str(m));2731+
} else {2732+
sp_PolyArray *row = sp_PolyArray_new();2733+
for (int gi = 1; gi < pairs; gi++) {2734+
if (caps[gi * 2] >= 0 && caps[gi * 2 + 1] >= 0) {2735+
int glen = caps[gi * 2 + 1] - caps[gi * 2];2736+
char *gm = sp_str_alloc_raw(glen + 1);2737+
memcpy(gm, str + caps[gi * 2], glen);2738+
gm[glen] = 0;2739+
sp_PolyArray_push(row, sp_box_str(gm));2740+
} else {2741+
sp_PolyArray_push(row, sp_box_nil());2742+
}2743+
}2744+
sp_PolyArray_push(arr, sp_box_poly_array(row));2745+
}2746+
pos = caps[1];2747+
if (caps[0] == caps[1]) pos++;2748+
}2749+
return arr;2750+
}27142751
static sp_PolyArray *sp_re_match_data(mrb_regexp_pattern *pat, const char *str) {27152752
int64_t slen = (int64_t)strlen(str);27162753
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…