Commit 9aa588b4

9aa588b48b6dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

Merge pull request #996 from ydah/fix-867-string-regexp-match

fix(analyze,codegen): dispatch String#match and Regexp#match

Details

Commit
9aa588b48b6d
Parent
cb27068fe139
Merge parent
65d0d01567b3

lib/sp_runtime.hmodifiedView file

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