Commit e398e095

e398e0956d6cMerged

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

Description

Fix String#squeeze with charset argument

squeeze("a") was silently dropping the argument and squeezing all consecutive characters (same as the no-arg form). Add sp_str_squeeze_chars runtime helper that checks each character against the charset before deciding to squeeze, with ^-negation support (squeeze("^a") squeezes everything except 'a'). Route codegen to the new helper when an argument is present.

Details

Commit
e398e0956d6c
Parent
2b5da1b0fd56

lib/sp_runtime.hmodifiedView file

@@ -1461,6 +1461,10 @@
14611461 `bytes` / `length` consult the header (not strlen), so callers
14621462 would see the alloc size and trailing NULs. */
14631463 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;}
1464+/* String#squeeze(chars) — only squeeze chars listed in the charset
1465+ (same charset syntax as tr: a-z, ^x, etc.). Consecutive runs of
1466+ non-listed chars pass through untouched. */
1467+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;}
14641468
14651469 /* Forward decl from sp_crypto.h (libspinel_rt.a). Used by
14661470 sp_str_crypt below to provide a deterministic crypt-like