Commit 87f5a835
87f5a835190eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
analyze,codegen,runtime: String#valid_encoding? via UTF-8 walker
sp_str_valid_encoding walks the buffer accepting pure ASCII or well-formed UTF-8 byte sequences (RFC 3629: no overlong forms, no surrogate halves, code points <= U+10FFFF). Returns bool; codegen dispatch arm and analyze type added.
Fixes #991
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 87f5a835190e
- Parent
- 05cab2985bea
lib/sp_runtime.hmodifiedView file
@@ -1389,6 +1389,10 @@
13891389
(avoids strcasecmp which lives in strings.h on POSIX and is named13901390
stricmp on Windows). Returns -1 / 0 / 1 like CRuby's String#casecmp. */13911391
static mrb_int sp_str_casecmp(const char*a,const char*b){if(!a||!b)return a==b?0:(a?1:-1);for(;;){int ca=tolower((unsigned char)*a),cb=tolower((unsigned char)*b);if(ca!=cb)return ca<cb?-1:1;if(!*a)return 0;a++;b++;}}1392+
/* String#valid_encoding? — walks the buffer and accepts pure ASCII1393+
or well-formed UTF-8 (RFC 3629 byte sequences with no overlong1394+
forms, no surrogate halves, code points <= U+10FFFF). */1395+
static mrb_bool sp_str_valid_encoding(const char*s){if(!s)return TRUE;const unsigned char*p=(const unsigned char*)s;while(*p){unsigned c=*p;if(c<0x80){p++;continue;}int extra;unsigned cp;unsigned min;if((c&0xE0)==0xC0){extra=1;cp=c&0x1F;min=0x80;}else if((c&0xF0)==0xE0){extra=2;cp=c&0x0F;min=0x800;}else if((c&0xF8)==0xF0){extra=3;cp=c&0x07;min=0x10000;}else return FALSE;p++;for(int i=0;i<extra;i++){if((*p&0xC0)!=0x80)return FALSE;cp=(cp<<6)|(*p&0x3F);p++;}if(cp<min)return FALSE;if(cp>=0xD800&&cp<=0xDFFF)return FALSE;if(cp>0x10FFFF)return FALSE;}return TRUE;}13921396
static const char*sp_StrArray_join(sp_StrArray*a,const char*sep){size_t sl=strlen(sep),cap=256;char*buf=(char*)malloc(cap);size_t len=0;for(mrb_int i=0;i<a->len;i++){if(i>0){if(len+sl>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,sep,sl);len+=sl;}size_t el=strlen(a->data[i]);if(len+el>=cap){cap=(len+el)*2+1;buf=(char*)realloc(buf,cap);}memcpy(buf+len,a->data[i],el);len+=el;}buf[len]=0;char*r=sp_str_alloc(len);memcpy(r,buf,len);free(buf);return r;}13931397
static mrb_bool sp_StrArray_include(sp_StrArray*a,const char*v){for(mrb_int i=0;i<a->len;i++)if(strcmp(a->data[i],v)==0)return TRUE;return FALSE;}13941398
static sp_StrArray*sp_StrArray_intersect(sp_StrArray*a,sp_StrArray*b){sp_StrArray*r=sp_StrArray_new();for(mrb_int i=0;i<a->len;i++){const char*v=a->data[i];if(sp_StrArray_include(b,v)&&!sp_StrArray_include(r,v))sp_StrArray_push(r,v);}return r;}spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_valid_encoding.rbaddedView file
Loading diff…
test/string_valid_encoding.rb.expectedaddedView file
Loading diff…