Commit 97be6762

97be67625233Merged

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

Description

fix(runtime,codegen): String#split honors the limit argument

"hi!".split("", 2) should return ["h", "i!"] (split into chars, limit caps at 2 with the last element holding the unsplit remainder). Spinel's two-arg split fell through to sp_str_split(s, sep) and ignored the limit, returning the full character-split ["h", "i", "!"].

Two pieces:

  1. lib/sp_runtime.h: add sp_str_split_limit(s, sep, n) for positive n -- caps the result array at n elements; the last entry keeps the unsplit remainder. Handles both empty-separator (Unicode character split) and string-separator paths. n <= 0 (no limit / keep trailing empties) falls back to plain sp_str_split.

  2. spinel_codegen.rb: String#split's two-arg form now lowers to sp_str_split_limit. The one-arg form keeps its existing dispatch.

Refs #619 puzzle 2.

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

Details

Commit
97be67625233
Parent
ba474cce9733

lib/sp_runtime.hmodifiedView file

@@ -1067,6 +1067,13 @@
10671067 static mrb_bool sp_str_start_with(const char*s,const char*p){return strncmp(s,p,strlen(p))==0;}
10681068 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;}
10691069 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;}
1070+/* `s.split(sep, n)` with explicit limit. Positive n caps the result
1071+ at n elements: the last element holds the unsplit remainder.
1072+ n <= 0 (CRuby's "no limit" or "keep trailing empties") falls back
1073+ to the plain split. Empty separator works the same as the no-limit
1074+ path -- splits into Unicode characters; the limit caps the array.
1075+ Issue #619 puzzle 2. */
1076+static sp_StrArray*sp_str_split_limit(const char*s,const char*sep,mrb_int n){if(n<=0)return sp_str_split(s,sep);sp_StrArray*a=sp_StrArray_new();if(*s==0)return a;size_t sl=strlen(sep);if(sl==0){const char*p=s;mrb_int k=0;while(*p&&k<n-1){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;k++;}if(*p){char*r=sp_str_alloc_raw(strlen(p)+1);strcpy(r,p);sp_StrArray_push(a,r);}return a;}const char*p=s;mrb_int k=0;while(k<n-1){const char*f=strstr(p,sep);if(!f)break;size_t m=f-p;char*r=sp_str_alloc_raw(m+1);memcpy(r,p,m);r[m]=0;sp_StrArray_push(a,r);p=f+sl;k++;}char*r=sp_str_alloc_raw(strlen(p)+1);strcpy(r,p);sp_StrArray_push(a,r);return a;}
10701077 /* `s.split` / `s.split(nil)` -- whitespace mode: split on runs of
10711078 ASCII whitespace, skip leading whitespace. Issue #507: the no-arg
10721079 form previously emitted `sp_str_split(s, 0)` and segfaulted at