Commit f4fe6e5f

f4fe6e5f68e2Merged

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

Description

runtime,codegen: Regexp#match?(str, pos) honors byte offset

/world/.match?("hello world", 6) returned true for /hello/ too because the codegen dispatch dropped the second argument. Add sp_re_match_p_at that starts re_exec at the given byte offset (negative pos counts from the end, out-of-range returns false), and route the 2-arg call through it.

Refs #869

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
f4fe6e5f68e2
Parent
23efd4ac1a3e

lib/sp_runtime.hmodifiedView file

@@ -1930,6 +1930,17 @@
19301930 return re_exec(pat, str, slen, 0, caps, 2) > 0;
19311931 }
19321932
1933+/* Issue #869: Regexp#match?(str, pos) starts matching at byte
1934+ offset `pos`. Negative pos counts from the end (CRuby compat).
1935+ Out-of-range pos returns false. */
1936+static mrb_bool sp_re_match_p_at(mrb_regexp_pattern *pat, const char *str, mrb_int pos) {
1937+ int64_t slen = (int64_t)strlen(str);
1938+ if (pos < 0) pos += slen;
1939+ if (pos < 0 || pos > slen) return FALSE;
1940+ int caps[2];
1941+ return re_exec(pat, str, slen, (mrb_int)pos, caps, 2) > 0;
1942+}
1943+
19331944 static const char *sp_re_gsub(mrb_regexp_pattern *pat, const char *str, const char *rep) {
19341945 int64_t slen = (int64_t)strlen(str); size_t rlen = strlen(rep);
19351946 size_t cap = slen * 2 + 64; char *out = sp_str_alloc_raw(cap); size_t olen = 0;