Commit 68e2846f
68e2846f780dMerged
fc-anjos <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
Root split helpers internally
Details
- Commit
- 68e2846f780d
- Parent
- d0f45efbaec0
lib/sp_runtime.hmodifiedView file
@@ -1271,11 +1271,40 @@
12711271
12721272
/* Reuse an existing StrArray for split, avoiding GC alloc.12731273
Clears a->len and refills. Substring strings are still malloc'd. */1274+
static inline void sp_str_split_push(sp_StrArray*a,const char*p,size_t n){1275+
char*r=sp_str_alloc_raw(n+1);1276+
memcpy(r,p,n);1277+
r[n]=0;1278+
sp_StrArray_push(a,r);1279+
}12741280
static void sp_str_split_into(sp_StrArray*a,const char*s,const char*sep){1275-
a->len=0;if(*s==0)return;size_t sl=strlen(sep);1276-
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;}1277-
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;}1278-
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;}}1281+
SP_GC_ROOT(a);1282+
SP_GC_ROOT(s);1283+
SP_GC_ROOT(sep);1284+
a->len=0;1285+
if(*s==0)return;1286+
size_t sl=strlen(sep);1287+
if(sl==0){1288+
const char*p=s;1289+
while(*p){1290+
int cn=sp_utf8_advance(p);1291+
sp_str_split_push(a,p,(size_t)cn);1292+
p+=cn;1293+
}1294+
return;1295+
}1296+
const char*p=s;1297+
while(1){1298+
const char*f=strstr(p,sep);1299+
if(!f){1300+
sp_str_split_push(a,p,strlen(p));1301+
break;1302+
}1303+
size_t n=f-p;1304+
sp_str_split_push(a,p,n);1305+
p=f+sl;1306+
}1307+
}12791308
/* Extract the n-th field (0-based) from s split by sep, without12801309
allocating a full StrArray. Returns a newly allocated string.12811310
If the field doesn't exist, returns "". */@@ -1419,7 +1448,13 @@
14191448
static mrb_bool sp_str_include(const char*s,const char*sub){if(!sub)sp_raise_cls("TypeError","no implicit conversion of nil into String");if(!s)return FALSE;return strstr(s,sub)!=NULL;}14201449
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;}14211450
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;}1422-
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;}1451+
static sp_StrArray*sp_str_split(const char*s,const char*sep){1452+
SP_GC_ROOT(s);1453+
SP_GC_ROOT(sep);1454+
sp_StrArray*a=sp_StrArray_new();1455+
sp_str_split_into(a,s,sep);1456+
return a;1457+
}14231458
/* Same as sp_str_split but removes trailing empty strings14241459
(CRuby default limit behavior: split without limit drops14251460
trailing empties; split(sep, -1) keeps them). */@@ -1431,12 +1466,61 @@
14311466
empties. Empty separator works the same as the no-limit path --14321467
splits into Unicode characters; the limit caps the array.14331468
Issue #619 puzzle 2. */1434-
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;}1469+
static sp_StrArray*sp_str_split_limit(const char*s,const char*sep,mrb_int n){1470+
if(n==0)return sp_str_split_drop_trailing(s,sep);1471+
if(n<0)return sp_str_split(s,sep);1472+
SP_GC_ROOT(s);1473+
SP_GC_ROOT(sep);1474+
sp_StrArray*a=sp_StrArray_new();1475+
SP_GC_ROOT(a);1476+
if(*s==0)return a;1477+
size_t sl=strlen(sep);1478+
if(sl==0){1479+
const char*p=s;1480+
mrb_int k=0;1481+
while(*p&&k<n-1){1482+
int cn=sp_utf8_advance(p);1483+
sp_str_split_push(a,p,(size_t)cn);1484+
p+=cn;1485+
k++;1486+
}1487+
if(*p){1488+
sp_str_split_push(a,p,strlen(p));1489+
}1490+
return a;1491+
}1492+
const char*p=s;1493+
mrb_int k=0;1494+
while(k<n-1){1495+
const char*f=strstr(p,sep);1496+
if(!f)break;1497+
size_t m=f-p;1498+
sp_str_split_push(a,p,m);1499+
p=f+sl;1500+
k++;1501+
}1502+
sp_str_split_push(a,p,strlen(p));1503+
return a;1504+
}14351505
/* `s.split` / `s.split(nil)` -- whitespace mode: split on runs of14361506
ASCII whitespace, skip leading whitespace. Issue #507: the no-arg14371507
form previously emitted `sp_str_split(s, 0)` and segfaulted at14381508
strlen(NULL). */1439-
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;}1509+
static sp_StrArray*sp_str_split_ws(const char*s){1510+
SP_GC_ROOT(s);1511+
sp_StrArray*a=sp_StrArray_new();1512+
SP_GC_ROOT(a);1513+
const char*p=s;1514+
while(*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p=='\f'||*p=='\v')p++;1515+
while(*p){1516+
const char*start=p;1517+
while(*p&&!(*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p=='\f'||*p=='\v'))p++;1518+
size_t n=p-start;1519+
sp_str_split_push(a,start,n);1520+
while(*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p=='\f'||*p=='\v')p++;1521+
}1522+
return a;1523+
}14401524
/* String#lines: split on \n but PRESERVE the trailing newline on each14411525
line (CRuby semantics). The last line keeps its terminator if present;14421526
if absent, it just stops there. Empty string returns an empty array.node_table_loader.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_split_inline_arg_gc_root.rbaddedView file
Loading diff…
test/string_split_inline_arg_gc_root.rb.expectedaddedView file
Loading diff…