Commit 9f246382
9f24638221ebMerged
NHH <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Fix String#match/match? with position argument
String#match?(/h/, 1) returned true instead of false because the position argument was silently dropped. Unlike Regexp#match?(str, pos) which uses a byte offset, String#match?(/re/, pos) takes a codepoint index (CRuby semantics). Add sp_str_re_match_p_at and sp_re_matchdata_at runtime helpers that convert the codepoint index to a byte offset via sp_utf8_byte_offset before dispatching to re_exec, and route the codegen to them when a second argument is present.
Details
- Commit
- 9f24638221eb
- Parent
- 2b5da1b0fd56
lib/sp_runtime.hmodifiedView file
@@ -1951,6 +1951,18 @@
19511951
int caps[2];19521952
return re_exec(pat, str, slen, (mrb_int)pos, caps, 2) > 0;19531953
}1954+
/* String#match?(/re/, pos) — pos is a codepoint index (CRuby semantics),1955+
unlike Regexp#match?(str, pos) which uses byte offset. Convert the1956+
codepoint index to a byte offset before dispatching to re_exec. */1957+
static mrb_bool sp_str_re_match_p_at(mrb_regexp_pattern *pat, const char *str, mrb_int cpos) {1958+
mrb_int cl = sp_str_length(str);1959+
if (cpos < 0) cpos += cl;1960+
if (cpos < 0 || cpos > cl) return FALSE;1961+
size_t boff = sp_utf8_byte_offset(str, cpos);1962+
int64_t slen = (int64_t)strlen(str);1963+
int caps[2];1964+
return re_exec(pat, str, slen, (mrb_int)boff, caps, 2) > 0;1965+
}19541966
19551967
/* Issue #855: expand `\1`..`\9` / `\&` / `\0` backreferences in19561968
the replacement string against the current caps[] array. `\\`@@ -2578,6 +2590,29 @@
25782590
for (int i = 0; i < pairs * 2; i++) m->caps[i] = caps[i];25792591
return m;25802592
}2593+
/* String#match(/re/, pos) — pos is a codepoint index (CRuby semantics). */2594+
static sp_MatchData *sp_re_matchdata_at(mrb_regexp_pattern *pat, const char *str, mrb_int cpos) {2595+
mrb_int cl = sp_str_length(str);2596+
if (cpos < 0) cpos += cl;2597+
if (cpos < 0 || cpos > cl) return NULL;2598+
size_t boff = sp_utf8_byte_offset(str, cpos);2599+
int64_t slen = (int64_t)strlen(str);2600+
int caps[64];2601+
int n = re_exec(pat, str, slen, (mrb_int)boff, caps, 64);2602+
if (n <= 0 || caps[0] < 0) {2603+
for (int i = 0; i < 10; i++) sp_re_captures[i] = NULL;2604+
sp_re_last_str = NULL; sp_re_match_str = NULL;2605+
sp_re_match_pre = NULL; sp_re_match_post = NULL;2606+
return NULL;2607+
}2608+
int pairs = (n > 64 ? 64 : n) / 2;2609+
sp_re_set_captures(str, caps, pairs);2610+
sp_MatchData *m = (sp_MatchData *)sp_gc_alloc(sizeof(sp_MatchData), NULL, sp_MatchData_scan);2611+
m->source = str;2612+
m->ncap = pairs;2613+
for (int i = 0; i < pairs * 2; i++) m->caps[i] = caps[i];2614+
return m;2615+
}25812616
/* group i substring, or NULL for a non-participating / out-of-range group */25822617
static const char *sp_MatchData_aref(sp_MatchData *m, mrb_int i) {25832618
if (!m || i < 0 || i >= m->ncap) return NULL;spinel_codegen.rbmodifiedView file
Loading diff…
test/str_match_position.rbaddedView file
Loading diff…
test/str_match_position.rb.expectedaddedView file
Loading diff…