Commit d7cd05e3
d7cd05e37b34Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(codegen,runtime): split with no arg / nil arg uses whitespace mode
Closes #507. "1 2 3".split segfaulted: the no-arg fallback in
compile_string_method_expr emitted sp_str_split(s, 0) (compile_arg0
returns the literal 0 for missing args), and the runtime's
sp_str_split(const char *s, const char *sep) then called
strlen(sep) on a NULL pointer.
Fix:
-
runtime:
sp_str_split_ws(const char *s)matches Ruby's whitespace-mode split — split on runs of ASCII whitespace (\t\n\r\f\v), skip leading whitespace, emit one token per run. Empty input returns an empty StrArray; input with no whitespace returns a single-element array. -
codegen: when
args_id < 0or the (single) arg is a NilNode, route tosp_str_split_wsinstead ofsp_str_split.
Pulled compile_expr for the explicit-arg case out from
compile_arg0 so the literal-string fast path stays distinct
from the no-arg fallback. The peephole that turns
"".split("...") into sp_StrArray_new() is preserved.
Test fixture covers six shapes: bare no-arg, leading/trailing whitespace, mixed whitespace, empty receiver, explicit nil arg, and the regression guard for non-whitespace sep.
Local: 473 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d7cd05e37b34
- Parent
- 0e4bcad2b8ad
lib/sp_runtime.hmodifiedView file
static mrb_bool sp_str_start_with(const char*s,const char*p){return strncmp(s,p,strlen(p))==0;}static mrb_bool sp_str_end_with(const char*s,const char*suf){size_t ls=strlen(s),lsuf=strlen(suf);if(lsuf>ls)return FALSE;return strcmp(s+ls-lsuf,suf)==0;}static sp_StrArray*sp_str_split(const char*s,const char*sep){sp_StrArray*a=sp_StrArray_new();if(*s==0)return a;size_t sl=strlen(sep);if(sl==0){const char*p=s;while(*p){int cn=sp_utf8_advance(p);char*c=sp_str_alloc_raw(cn+1);memcpy(c,p,cn);c[cn]=0;sp_StrArray_push(a,c);p+=cn;}return a;}const char*p=s;while(1){const char*f=strstr(p,sep);if(!f){char*r=sp_str_alloc_raw(strlen(p)+1);strcpy(r,p);sp_StrArray_push(a,r);break;}size_t n=f-p;char*r=sp_str_alloc_raw(n+1);memcpy(r,p,n);r[n]=0;sp_StrArray_push(a,r);p=f+sl;}return a;}/* `s.split` / `s.split(nil)` -- whitespace mode: split on runs of ASCII whitespace, skip leading whitespace. Issue #507: the no-arg form previously emitted `sp_str_split(s, 0)` and segfaulted at strlen(NULL). */static sp_StrArray*sp_str_split_ws(const char*s){sp_StrArray*a=sp_StrArray_new();const char*p=s;while(*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p=='\f'||*p=='\v')p++;while(*p){const char*start=p;while(*p&&!(*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p=='\f'||*p=='\v'))p++;size_t n=p-start;char*r=sp_str_alloc_raw(n+1);memcpy(r,start,n);r[n]=0;sp_StrArray_push(a,r);while(*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p=='\f'||*p=='\v')p++;}return a;}/* String#lines: split on \n but PRESERVE the trailing newline on each line (CRuby semantics). The last line keeps its terminator if present; if absent, it just stops there. Empty string returns an empty array.spinel_codegen.rbmodifiedView file
Loading diff…
test/split_no_arg_ws.rbaddedView file
Loading diff…
test/split_no_arg_ws.rb.expectedaddedView file
Loading diff…