Commit 046f44f6

046f44f66aafMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

String#squeeze: treat a lone "^" charset as the literal caret (refs #1065)

sp_str_squeeze_chars treated any leading "^" as set negation, so "aaa^^^".squeeze("^") negated the empty set and squeezed everything ("a^") instead of squeezing only the caret runs ("aaa^"). CRuby (and spinel's own count/delete) treat a lone "^" as the literal character; "^" only negates when followed by more of the charset. Guard the negation on a non-empty remainder.

Follow-up to PR #1065 (String#squeeze charset arg).

Refs #1065

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
046f44f66aaf
Parent
1f0fcbad87c4

lib/sp_runtime.hmodifiedView file

@@ -1468,7 +1468,7 @@
14681468 /* String#squeeze(chars) — only squeeze chars listed in the charset
14691469 (same charset syntax as tr: a-z, ^x, etc.). Consecutive runs of
14701470 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;}
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=='^'&&*(csp+1)){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;}
14721472
14731473 /* Forward decl from sp_crypto.h (libspinel_rt.a). Used by
14741474 sp_str_crypt below to provide a deterministic crypt-like