Commit a4d975b0
a4d975b0680cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Intersect charsets in String#squeeze with multiple arguments
str.squeeze("a", "b") only used the first argument; CRuby squeezes
runs of characters in the INTERSECTION of the charset args. Add
sp_str_squeeze_n (mirroring sp_str_count_n / sp_str_delete_n) and route
the multi-arg form to it; one-arg and no-arg forms are unchanged.
"aaabbbccc".squeeze("a", "ab") # => "abbbccc" ({a}∩{a,b}={a}) "aaabbbccc".squeeze("a", "b") # => "aaabbbccc" ({a}∩{b}={})
Fixes #1214
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- a4d975b0680c
- Parent
- 8afeab777874
lib/sp_runtime.hmodifiedView file
@@ -1505,6 +1505,7 @@
15051505
are in the INTERSECTION of all n charset args, mirroring15061506
sp_str_count_n. Each arg is a charset spec (^negation, a-z ranges). */15071507
static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}1508+
static const char*sp_str_squeeze_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return sp_str_squeeze(s);size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;prev=0xFFFFFFFFu;}else if(cp!=prev){memcpy(r+m,p,cn);m+=cn;prev=cp;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}15081509
15091510
/* Forward decl from sp_crypto.h (libspinel_rt.a). Used by15101511
sp_str_crypt below to provide a deterministic crypt-likespinel_codegen.rbmodifiedView file
Loading diff…
test/string_squeeze_multi_arg.rbaddedView file
Loading diff…
test/string_squeeze_multi_arg.rb.expectedaddedView file
Loading diff…