Commit b16a6e1c

b16a6e1c51f1Merged

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

Description

Implement String#undump

String#undump (the inverse of String#dump) was unresolved. Add a runtime sp_str_undump that strips the surrounding double quotes and decodes the escapes dump emits (\n \t \r \f \v \a \b \e \s \0 " \ # \xHH \uHHHH \u{...}) back to bytes, raising RuntimeError when the input is not a quoted dump. analyze types the result as string.

With C1 (succ, already working) and C2 (upto), this closes the String method-gap issue.

Fixes #1230

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
b16a6e1c51f1
Parent
e6cea5abecd5

lib/sp_runtime.hmodifiedView file

@@ -1245,6 +1245,35 @@
12451245 static const char*sp_str_upcase(const char*s){if(!s)return sp_str_empty;size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<l;i++)r[i]=toupper((unsigned char)s[i]);r[l]=0;return r;}
12461246 static const char*sp_str_downcase(const char*s){if(!s)return sp_str_empty;size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<l;i++)r[i]=tolower((unsigned char)s[i]);r[l]=0;return r;}
12471247 static const char*sp_str_swapcase(const char*s){if(!s)return sp_str_empty;size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<l;i++){unsigned char c=(unsigned char)s[i];if(isupper(c))r[i]=tolower(c);else if(islower(c))r[i]=toupper(c);else r[i]=s[i];}r[l]=0;return r;}
1248+/* String#undump: reverse of String#dump. The argument must be wrapped in
1249+ double quotes; the escapes dump can emit (\n \t \r \f \v \a \b \e \s \0
1250+ \" \\ \# \xHH \uHHHH \u{...}) are decoded back to bytes. The decoded
1251+ string is never longer than the dumped form, so one buffer suffices. */
1252+static int _sp_hexval(unsigned char d){return (d<='9')?(d-'0'):(tolower(d)-'a'+10);}
1253+static const char*sp_str_undump(const char*s){
1254+ if(!s)return sp_str_empty;
1255+ size_t n=strlen(s);
1256+ if(n<2||s[0]!='"'||s[n-1]!='"'){sp_raise_cls("RuntimeError","invalid dumped string");return sp_str_empty;}
1257+ const char*p=s+1;const char*pe=s+n-1;
1258+ char*out=sp_str_alloc_raw(n+1);size_t oi=0;
1259+ while(p<pe){
1260+ if(*p!='\\'){out[oi++]=*p++;continue;}
1261+ p++;if(p>=pe)break;
1262+ char c=*p++;
1263+ if(c=='n')out[oi++]='\n';else if(c=='t')out[oi++]='\t';else if(c=='r')out[oi++]='\r';
1264+ else if(c=='f')out[oi++]='\f';else if(c=='v')out[oi++]='\v';else if(c=='a')out[oi++]='\a';
1265+ else if(c=='b')out[oi++]='\b';else if(c=='e')out[oi++]='\033';else if(c=='s')out[oi++]=' ';
1266+ else if(c=='0')out[oi++]='\0';else if(c=='\\')out[oi++]='\\';else if(c=='"')out[oi++]='"';
1267+ else if(c=='#')out[oi++]='#';
1268+ else if(c=='x'){int v=0,k=0;while(k<2&&p<pe&&isxdigit((unsigned char)*p)){v=v*16+_sp_hexval((unsigned char)*p);p++;k++;}out[oi++]=(char)v;}
1269+ else if(c=='u'){
1270+ if(p<pe&&*p=='{'){p++;while(p<pe&&*p!='}'){while(p<pe&&*p==' ')p++;uint32_t cp=0;int k=0;while(k<8&&p<pe&&isxdigit((unsigned char)*p)){cp=cp*16+(uint32_t)_sp_hexval((unsigned char)*p);p++;k++;}char enc[4];int el=sp_utf8_encode(cp,enc);for(int j=0;j<el;j++)out[oi++]=enc[j];while(p<pe&&*p==' ')p++;}if(p<pe&&*p=='}')p++;}
1271+ else{uint32_t cp=0;int k=0;while(k<4&&p<pe&&isxdigit((unsigned char)*p)){cp=cp*16+(uint32_t)_sp_hexval((unsigned char)*p);p++;k++;}char enc[4];int el=sp_utf8_encode(cp,enc);for(int j=0;j<el;j++)out[oi++]=enc[j];}
1272+ }
1273+ else out[oi++]=c;
1274+ }
1275+ out[oi]=0;return out;
1276+}
12481277 /* Issue #797: NULL guards on receiver + needle for the chunk of
12491278 string functions that read directly into a non-checked strlen. */
12501279 static const char*sp_str_delete_prefix(const char*s,const char*p){if(!s)return sp_str_empty;if(!p)return s;size_t sl=strlen(s),pl=strlen(p);if(pl<=sl&&memcmp(s,p,pl)==0){char*r=sp_str_alloc_raw(sl-pl+1);memcpy(r,s+pl,sl-pl+1);return r;}char*r=sp_str_alloc_raw(sl+1);memcpy(r,s,sl+1);return r;}