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

@@ -87,6 +87,56 @@
8787 *len += n;
8888 }
8989
90+/* Encode codepoint `v` as UTF-8 (the `U` directive) into out[0..5].
91+ Mirrors CRuby's permissive scheme: up to 6 bytes for values through
92+ 0x7FFFFFFF. Negative / out-of-range values encode nothing (CRuby
93+ raises RangeError; spinel has no exception path here). Returns the
94+ byte count written. */
95+static int pk_utf8(unsigned char *out, int64_t v) {
96+ if (v < 0) return 0;
97+ uint64_t uv = (uint64_t)v;
98+ if (uv <= 0x7F) {
99+ out[0] = (unsigned char)uv;
100+ return 1;
101+ }
102+ if (uv <= 0x7FF) {
103+ out[0] = (unsigned char)(0xC0 | (uv >> 6));
104+ out[1] = (unsigned char)(0x80 | (uv & 0x3F));
105+ return 2;
106+ }
107+ if (uv <= 0xFFFF) {
108+ out[0] = (unsigned char)(0xE0 | (uv >> 12));
109+ out[1] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F));
110+ out[2] = (unsigned char)(0x80 | (uv & 0x3F));
111+ return 3;
112+ }
113+ if (uv <= 0x1FFFFF) {
114+ out[0] = (unsigned char)(0xF0 | (uv >> 18));
115+ out[1] = (unsigned char)(0x80 | ((uv >> 12) & 0x3F));
116+ out[2] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F));
117+ out[3] = (unsigned char)(0x80 | (uv & 0x3F));
118+ return 4;
119+ }
120+ if (uv <= 0x3FFFFFF) {
121+ out[0] = (unsigned char)(0xF8 | (uv >> 24));
122+ out[1] = (unsigned char)(0x80 | ((uv >> 18) & 0x3F));
123+ out[2] = (unsigned char)(0x80 | ((uv >> 12) & 0x3F));
124+ out[3] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F));
125+ out[4] = (unsigned char)(0x80 | (uv & 0x3F));
126+ return 5;
127+ }
128+ if (uv <= 0x7FFFFFFF) {
129+ out[0] = (unsigned char)(0xFC | (uv >> 30));
130+ out[1] = (unsigned char)(0x80 | ((uv >> 24) & 0x3F));
131+ out[2] = (unsigned char)(0x80 | ((uv >> 18) & 0x3F));
132+ out[3] = (unsigned char)(0x80 | ((uv >> 12) & 0x3F));
133+ out[4] = (unsigned char)(0x80 | ((uv >> 6) & 0x3F));
134+ out[5] = (unsigned char)(0x80 | (uv & 0x3F));
135+ return 6;
136+ }
137+ return 0;
138+}
139+
90140 static int64_t pk_parse_count(const char **pp) {
91141 const char *p = *pp;
92142 if (*p == '*') { *pp = p + 1; return -1; }
@@ -184,6 +234,12 @@
184234 pk_append(&buf, &len, &cap, tmp, 1);
185235 idx--;
186236 break;
237+ case 'U': {
238+ unsigned char ub[6];
239+ int ulen = pk_utf8(ub, v);
240+ pk_append(&buf, &len, &cap, (const char *)ub, (size_t)ulen);
241+ break;
242+ }
187243 default:
188244 break;
189245 }
@@ -270,6 +326,12 @@
270326 pk_append(&buf, &len, &cap, tmp, 1);
271327 idx--;
272328 break;
329+ case 'U': {
330+ unsigned char ub[6];
331+ int ulen = pk_utf8(ub, v);
332+ pk_append(&buf, &len, &cap, (const char *)ub, (size_t)ulen);
333+ break;
334+ }
273335 default:
274336 break;
275337 }