Commit b01adfa6

b01adfa683bbMerged

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

Description

runtime,codegen: String#scrub replaces invalid UTF-8 with replacement

Walk the byte stream; for each valid UTF-8 lead + continuation sequence, copy through. For invalid bytes (lone continuation, truncated multi-byte, raw 0x80-0xFF on its own), emit the replacement string and skip one byte.

The default replacement (NULL arg) lands as U+FFFD (EF BF BD) to match CRuby. unicode_normalize is deferred — needs ICU / full Unicode table data.

Refs #904 (scrub side fixed; unicode_normalize still open)

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

Details

Commit
b01adfa683bb
Parent
06bf009dd646

lib/sp_runtime.hmodifiedView file

@@ -1746,6 +1746,52 @@
17461746 `bytes` / `length` consult the header (not strlen), so callers
17471747 would see the alloc size and trailing NULs. */
17481748 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;}
1749+
1750+/* String#scrub — walk the bytes; for each valid UTF-8 lead +
1751+ continuation sequence, copy through. For invalid bytes (lone
1752+ continuation, truncated multi-byte, overlong, etc.), emit the
1753+ replacement string and skip one byte. NULL replacement uses
1754+ U+FFFD (3 UTF-8 bytes: EF BF BD), matching CRuby. */
1755+static const char *sp_str_scrub(const char *s, const char *repl) {
1756+ if (!s) return sp_str_empty;
1757+ static const char fffd[] = "\xEF\xBF\xBD";
1758+ const char *r = repl ? repl : fffd;
1759+ size_t rlen = strlen(r);
1760+ size_t bl = sp_str_byte_len(s);
1761+ size_t cap = bl + 64;
1762+ char *out = sp_str_alloc_raw(cap);
1763+ size_t olen = 0;
1764+ size_t i = 0;
1765+ while (i < bl) {
1766+ unsigned char c = (unsigned char)s[i];
1767+ int expected = sp_utf8_char_len(c);
1768+ int valid = 1;
1769+ if (expected == 1) {
1770+ if (c >= 0x80) valid = 0;
1771+ } else {
1772+ if (i + (size_t)expected > bl) valid = 0;
1773+ else {
1774+ for (int k = 1; k < expected; k++) {
1775+ if (((unsigned char)s[i + k] & 0xC0) != 0x80) { valid = 0; break; }
1776+ }
1777+ }
1778+ }
1779+ if (valid) {
1780+ if (olen + (size_t)expected + 1 >= cap) { cap = (olen + expected) * 2 + 64; out = (char*)realloc(out, cap); }
1781+ memcpy(out + olen, s + i, (size_t)expected);
1782+ olen += (size_t)expected;
1783+ i += (size_t)expected;
1784+ } else {
1785+ if (olen + rlen + 1 >= cap) { cap = (olen + rlen) * 2 + 64; out = (char*)realloc(out, cap); }
1786+ memcpy(out + olen, r, rlen);
1787+ olen += rlen;
1788+ i += 1;
1789+ }
1790+ }
1791+ out[olen] = 0;
1792+ sp_str_set_len(out, olen);
1793+ return out;
1794+}
17491795 static const char*sp_str_ljust(const char*s,mrb_int w){if(!s)return sp_str_empty;mrb_int cl=sp_str_length(s);if(cl>=w)return s;size_t bl=strlen(s);size_t pad=(size_t)(w-cl);char*r=sp_str_alloc_raw(bl+pad+1);memcpy(r,s,bl);memset(r+bl,' ',pad);r[bl+pad]=0;return r;}
17501796 static const char*sp_str_rjust(const char*s,mrb_int w){if(!s)return sp_str_empty;mrb_int cl=sp_str_length(s);if(cl>=w)return s;size_t bl=strlen(s);size_t pad=(size_t)(w-cl);char*r=sp_str_alloc_raw(bl+pad+1);memset(r,' ',pad);memcpy(r+pad,s,bl);r[bl+pad]=0;return r;}
17511797 static const char*sp_str_center(const char*s,mrb_int w){if(!s)return sp_str_empty;mrb_int cl=sp_str_length(s);if(cl>=w)return s;size_t bl=strlen(s);mrb_int pad=w-cl;mrb_int left=pad/2;mrb_int right=pad-left;char*r=sp_str_alloc_raw(bl+pad+1);memset(r,' ',left);memcpy(r+left,s,bl);memset(r+left+bl,' ',right);r[bl+pad]=0;return r;}