Commit b55b1483
b55b1483270bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +1 added · ~3 modified
Description
Make String#split(sep, 0) drop trailing empty strings (CRuby parity)
PR #1070 routed the no-arg-limit default through sp_str_split_drop_trailing,
but sp_str_split_limit still treated n <= 0 uniformly as plain split, so
"a,b,c,".split(",", 0) kept the trailing empty. CRuby distinguishes the
two no-limit forms: n == 0 drops trailing empties (like the default), only
n < 0 keeps them. Split the n <= 0 branch accordingly and extend the test.
Refs #1070
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b55b1483270b
- Parent
- bcd7c5237212
lib/sp_runtime.hmodifiedView file
@@ -1285,11 +1285,12 @@
12851285
static sp_StrArray*sp_str_split_drop_trailing(const char*s,const char*sep){sp_StrArray*a=sp_str_split(s,sep);while(a->len>0&&a->data[a->len-1][0]==0)a->len--;return a;}12861286
/* `s.split(sep, n)` with explicit limit. Positive n caps the result12871287
at n elements: the last element holds the unsplit remainder.1288-
n <= 0 (CRuby's "no limit" or "keep trailing empties") falls back1289-
to the plain split. Empty separator works the same as the no-limit1290-
path -- splits into Unicode characters; the limit caps the array.1288+
n == 0 means "no limit" and drops trailing empty strings (same as1289+
the no-arg default); n < 0 means "no limit" but keeps trailing1290+
empties. Empty separator works the same as the no-limit path --1291+
splits into Unicode characters; the limit caps the array.12911292
Issue #619 puzzle 2. */1292-
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;}1293+
static sp_StrArray*sp_str_split_limit(const char*s,const char*sep,mrb_int n){if(n==0)return sp_str_split_drop_trailing(s,sep);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;}12931294
/* `s.split` / `s.split(nil)` -- whitespace mode: split on runs of12941295
ASCII whitespace, skip leading whitespace. Issue #507: the no-arg12951296
form previously emitted `sp_str_split(s, 0)` and segfaulted atsp_split0addedView file
Loading diff…
test/str_split_trailing_empty.rbmodifiedView file
Loading diff…
test/str_split_trailing_empty.rb.expectedmodifiedView file
Loading diff…