Commit 82ade25b
82ade25be1c2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · ~5 modified
Description
fix(analyze,codegen,runtime): String#<< return-types as string; rindex(regex)
Refs #504 follow-up. Two of the remaining limitations called out when #504 closed are addressed:
-
"foo".send(:<<)(which spinel_parse already rewrites to"foo".<<()at source level) used to print a meaningless integer. analyze's<<arm returnedintfor any receiver that wasn'tmutable_stror array; codegen then wrapped the sp_str_concat result (aconst char *) insp_int_to_sand printed the string pointer cast to int. Added alt == "string"branch returning"string"so the surroundingp/ interpolation emits sp_str_inspect (or the rawconst char *) instead of int-stringifying. -
"foo".rindex(/regex/)SEGV'd because the codegen routed regex args through sp_str_rindex (which expects const char *) — the regex pat lowered to literal0and the helper called strlen(NULL). Newsp_re_rindexruntime helper walks forward through successive re_exec hits and reports the latest match start. Codegen detects a regex arg via regex_pat_c_expr and routes accordingly.
rindex(regex) with no match still returns -1 (Ruby's nil
shape doesn't fit the int return slot for an int-typed call);
that's a deliberate limitation, not a behavior regression.
Test fixture updates: added "abcdabcd".rindex(/c/) (6) for
the positive-match case and "foo".send(:<<) to lock in the
inspect-as-string return.
Local: 477 tests pass, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 82ade25be1c2
- Parent
- 79736a6c60b7
lib/sp_runtime.hmodifiedView file
return 0;}/* `s.rindex(regex)` — last match start, in BYTE offset (matches the way sp_str_rindex reports indices for plain-string search; codepoint translation would require a UTF-8 walk and the handful of call sites that consume rindex don't need it). Walks forward through successive matches and remembers the latest start. Issue #504: previously the codegen routed `s.rindex(/re/)` to sp_str_rindex(s, 0) and SEGV'd at strlen(NULL). Returns -1 on no match. */static mrb_int sp_re_rindex(mrb_regexp_pattern *pat, const char *str) { int64_t slen = (int64_t)strlen(str); int caps[2]; int64_t pos = 0; mrb_int last = -1; while (pos <= slen) { int n = re_exec(pat, str, slen, pos, caps, 2); if (n <= 0) break; last = caps[0]; /* Advance past the match; for zero-width matches step by 1 to avoid an infinite loop. */ int64_t next = caps[1]; if (next <= pos) next = pos + 1; pos = next; } return last;}static mrb_bool sp_re_match_p(mrb_regexp_pattern *pat, const char *str) { int64_t slen = (int64_t)strlen(str); int caps[2];spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/str_method_nil_arg_no_segv.rbmodifiedView file
Loading diff…
test/str_method_nil_arg_no_segv.rb.expectedmodifiedView file
Loading diff…