Commit 5d28d67b

5d28d67bd444Merged

NHH <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Fix String#split to drop trailing empty strings by default

"a,b,c,".split(",") returned ["a","b","c",""] but CRuby drops trailing empty strings: ["a","b","c"]. Only split(sep, -1) keeps them. Add sp_str_split_drop_trailing wrapper that trims trailing empty entries from the result, and route the default (no-limit) codegen path through it. The split(sep, n) and split(sep, -1) paths still use sp_str_split / sp_str_split_limit which preserve trailing empties.

Details

Commit
5d28d67bd444
Parent
2b5da1b0fd56

lib/sp_runtime.hmodifiedView file

@@ -1273,6 +1273,10 @@
12731273 static mrb_bool sp_str_start_with(const char*s,const char*p){if(!p)sp_raise_cls("TypeError","no implicit conversion of nil into String");if(!s)return FALSE;return strncmp(s,p,strlen(p))==0;}
12741274 static mrb_bool sp_str_end_with(const char*s,const char*suf){if(!suf)sp_raise_cls("TypeError","no implicit conversion of nil into String");if(!s)return FALSE;size_t ls=strlen(s),lsuf=strlen(suf);if(lsuf>ls)return FALSE;return strcmp(s+ls-lsuf,suf)==0;}
12751275 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;}
1276+/* Same as sp_str_split but removes trailing empty strings
1277+ (CRuby default limit behavior: split without limit drops
1278+ trailing empties; split(sep, -1) keeps them). */
1279+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;}
12761280 /* `s.split(sep, n)` with explicit limit. Positive n caps the result
12771281 at n elements: the last element holds the unsplit remainder.
12781282 n <= 0 (CRuby's "no limit" or "keep trailing empties") falls back