Commit af3bc1b7
af3bc1b7787eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Implement the U (UTF-8) directive for Array#pack
[cp].pack("U*") returned an empty string -- sp_pack.c had no U
directive, so the codepoints were silently dropped. Add a pk_utf8
helper (shared by the IntArray and PolyArray pack paths) that encodes
each integer codepoint as UTF-8, and wire a case 'U' into both
switches.
The encoder mirrors CRuby's permissive scheme: 1 byte through U+007F up to 6 bytes through 0x7FFFFFFF (so values past U+10FFFF still round-trip the way CRuby's pack does). Negative / out-of-range values encode nothing -- CRuby raises RangeError there, but sp_pack.c has no exception path.
[0x1E0A].pack("U*").bytes # => [225, 184, 138] (was []) [0x1F600].pack("U*").bytes # => [240, 159, 152, 128]
Fixes #1130
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- af3bc1b7787e
- Parent
- fe4eafb67f84
lib/sp_pack.cmodifiedView file
*len += n;}/* Encode codepoint `v` as UTF-8 (the `U` directive) into out[0..5]. Mirrors CRuby's permissive scheme: up to 6 bytes for values through 0x7FFFFFFF. Negative / out-of-range values encode nothing (CRuby raises RangeError; spinel has no exception path here). Returns the byte count written. */static int pk_utf8(unsigned char *out, int64_t v) { if (v < 0) return 0; uint64_t uv = (uint64_t)v; if (uv <= 0x7F) { out[0] = (unsigned char)uv; return 1; } if (uv <= 0x7FF) { out[0] = (unsigned char)(0xC0 | (uv >> 6)); out[1] = (unsigned char)(0x80 | (uv & 0x3F)); return 2; } if (uv <= 0xFFFF) { out[0] = (unsigned char)(0xE0 | (uv >> 12)); out[1] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F)); out[2] = (unsigned char)(0x80 | (uv & 0x3F)); return 3; } if (uv <= 0x1FFFFF) { out[0] = (unsigned char)(0xF0 | (uv >> 18)); out[1] = (unsigned char)(0x80 | ((uv >> 12) & 0x3F)); out[2] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F)); out[3] = (unsigned char)(0x80 | (uv & 0x3F)); return 4; } if (uv <= 0x3FFFFFF) { out[0] = (unsigned char)(0xF8 | (uv >> 24)); out[1] = (unsigned char)(0x80 | ((uv >> 18) & 0x3F)); out[2] = (unsigned char)(0x80 | ((uv >> 12) & 0x3F)); out[3] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F)); out[4] = (unsigned char)(0x80 | (uv & 0x3F)); return 5; } if (uv <= 0x7FFFFFFF) { out[0] = (unsigned char)(0xFC | (uv >> 30)); out[1] = (unsigned char)(0x80 | ((uv >> 24) & 0x3F)); out[2] = (unsigned char)(0x80 | ((uv >> 18) & 0x3F)); out[3] = (unsigned char)(0x80 | ((uv >> 12) & 0x3F)); out[4] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F)); out[5] = (unsigned char)(0x80 | (uv & 0x3F)); return 6; } return 0;}static int64_t pk_parse_count(const char **pp) { const char *p = *pp; if (*p == '*') { *pp = p + 1; return -1; } pk_append(&buf, &len, &cap, tmp, 1); idx--; break; case 'U': { unsigned char ub[6]; int ulen = pk_utf8(ub, v); pk_append(&buf, &len, &cap, (const char *)ub, (size_t)ulen); break; } default: break; } pk_append(&buf, &len, &cap, tmp, 1); idx--; break; case 'U': { unsigned char ub[6]; int ulen = pk_utf8(ub, v); pk_append(&buf, &len, &cap, (const char *)ub, (size_t)ulen); break; } default: break; }test/pack_utf8.rbaddedView file
Loading diff…
test/pack_utf8.rb.expectedaddedView file
Loading diff…