Commit 1f0fcbad
1f0fcbad87c4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #1065 from neidiom/fix/str-squeeze-arg
Fix String#squeeze with charset argument
Details
- Commit
- 1f0fcbad87c4
- Parent
- ad42e9525da9
- Merge parent
- e398e0956d6c
lib/sp_runtime.hmodifiedView file
@@ -1465,6 +1465,10 @@
14651465
`bytes` / `length` consult the header (not strlen), so callers14661466
would see the alloc size and trailing NULs. */14671467
static const char*sp_str_squeeze(const char*s){if(!s)return sp_str_empty;size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);if(cp!=prev){memcpy(r+n,p,cn);n+=cn;prev=cp;}p+=cn;}r[n]=0;sp_str_set_len(r,n);return r;}1468+
/* String#squeeze(chars) — only squeeze chars listed in the charset1469+
(same charset syntax as tr: a-z, ^x, etc.). Consecutive runs of1470+
non-listed chars pass through untouched. */1471+
static const char*sp_str_squeeze_chars(const char*s,const char*cs){if(!s)return sp_str_empty;if(!cs||!*cs)return sp_str_squeeze(s);int negate=0;const char*csp=cs;if(*csp=='^'){negate=1;csp++;}size_t fn;uint32_t*fcps=sp_utf8_decode_charset(csp,&fn);size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=0;for(size_t j=0;j<fn;j++)if(fcps[j]==cp){in_set=1;break;}if(negate)in_set=!in_set;if(!in_set){memcpy(r+n,p,cn);n+=cn;prev=0xFFFFFFFFu;}else if(cp!=prev){memcpy(r+n,p,cn);n+=cn;prev=cp;}p+=cn;}r[n]=0;sp_str_set_len(r,n);free(fcps);return r;}14681472
14691473
/* Forward decl from sp_crypto.h (libspinel_rt.a). Used by14701474
sp_str_crypt below to provide a deterministic crypt-likespinel_codegen.rbmodifiedView file
Loading diff…
test/str_squeeze_arg.rbaddedView file
Loading diff…
test/str_squeeze_arg.rb.expectedaddedView file
Loading diff…