Commit 65d0d015

65d0d01567b3Merged

ydah <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

analyze,codegen: dispatch String#match and Regexp#match

Details

Commit
65d0d01567b3
Parent
e4d2ffaa5870

lib/sp_runtime.hmodifiedView file

@@ -2700,6 +2700,36 @@
27002700 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); }
27012701 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; }
27022702 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; }
2703+static sp_PolyArray *sp_re_match_data(mrb_regexp_pattern *pat, const char *str) {
2704+ int64_t slen = (int64_t)strlen(str);
2705+ int ncaps = 64;
2706+ int n = re_exec(pat, str, slen, 0, sp_re_caps, ncaps);
2707+ if (n <= 0 || sp_re_caps[0] < 0) {
2708+ for (int i = 0; i < 10; i++) sp_re_captures[i] = NULL;
2709+ sp_re_last_str = NULL;
2710+ sp_re_match_str = NULL;
2711+ sp_re_match_pre = NULL;
2712+ sp_re_match_post = NULL;
2713+ return NULL;
2714+ }
2715+ int pairs = (n > ncaps ? ncaps : n) / 2;
2716+ sp_re_set_captures(str, sp_re_caps, pairs);
2717+ sp_PolyArray *arr = sp_PolyArray_new();
2718+ for (int i = 0; i < pairs; i++) {
2719+ int start = sp_re_caps[i * 2];
2720+ int end = sp_re_caps[i * 2 + 1];
2721+ if (start >= 0 && end >= start) {
2722+ int len = end - start;
2723+ char *buf = sp_str_alloc_raw(len + 1);
2724+ memcpy(buf, str + start, len);
2725+ buf[len] = 0;
2726+ sp_PolyArray_push(arr, sp_box_str(buf));
2727+ } else {
2728+ sp_PolyArray_push(arr, sp_box_nil());
2729+ }
2730+ }
2731+ return arr;
2732+}
27032733 static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) {
27042734 /* Dispatch by recv cls_id: an IntArray / PolyArray / etc. boxed
27052735 into a poly slot still wants Array#<< (push), not Integer#<<