spinel_kit 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a45aac9b6f91813e7d837ea8878d4dcee1731b5582f4a42e5fae4f73e9f764ce
4
- data.tar.gz: f201acc7e5c64ba8f5666e4c9c454440c7d1cc6835a30dd8adfb07442b1b42e8
3
+ metadata.gz: fd39c286c6d7c659824f19858df58b9ff2cf21c2d951bd9bbc109538fd99496a
4
+ data.tar.gz: 49d68b394bff6bbb0168b1d76db3eeeb452d42e854116b2908acde8ffe7c9a47
5
5
  SHA512:
6
- metadata.gz: 5a6f770a93f9975455637ed2fb2ffd8874dcc69d1e6fdf8547b7120c13ea341f2154a0427f2bf317851d0cb86072232649f6e1b011354c8ab68760f120a4bc2e
7
- data.tar.gz: 45d722737fce1cce7d606765af43f405fa6f34c96fa8a0167c8794262dee1ffebaa034600d865399577aaf9e826491059f338a8e688502bfd52b00c1071157b3
6
+ metadata.gz: de3d1fe679ddd3221521c3fd0f24717d876e099996201b20064820b2bc239a911b685ffb4074718d9131a21838c01589fcbcc52899b585b1ade2d21e9fec97d5
7
+ data.tar.gz: d4b9fe75c8a99b233e43c66517ef0c9eb84087cb4b070ed5cc06b3f424d5dcf40aa82753890b50a170162983ffd2a6167eab85b8300946486ceff9c96821c2a2
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  All notable changes to SpinelKit are documented here.
4
4
 
5
+ ## [0.2.1] - 2026-11-29
6
+
7
+ Maintenance release on the 0.2 line (the last with `SpinelKit::Json`; 0.3.0
8
+ retired the codec).
9
+
10
+ ### Fixed
11
+ - **`SpinelKit::Json` decoder: `\uXXXX` escapes above ASCII decoded to `?`.**
12
+ `get_str` kept only the low byte of a two-digit escape and gave up on the
13
+ rest, so a body from a client that escapes non-ASCII (Python's
14
+ `json.dumps` by default) lost its text: `"\u00e9t\u00e9"` became `?t?`.
15
+ Escapes now decode to the UTF-8 bytes of the code point; a surrogate pair
16
+ is one code point; a surrogate on its own becomes U+FFFD. A malformed
17
+ escape still yields `?` and never raises.
18
+
5
19
  ## [0.2.0] - 2026-06-08
6
20
 
7
21
  ### Added
@@ -242,26 +242,23 @@ module SpinelKit
242
242
  elsif esc == "f"
243
243
  out = out + "\f"
244
244
  elsif esc == "u"
245
- # \u00XX -> map the two-digit hex back to a byte. Wider
246
- # codepoints (U+0100+ or surrogate pairs) aren't decoded; the
247
- # byte we emit is the low byte of the codepoint, which
248
- # round-trips ASCII at minimum.
249
- if pos + 5 < s.length
250
- h1 = Json.hex_nibble(s[pos + 4])
251
- h2 = Json.hex_nibble(s[pos + 5])
252
- if h1 >= 0 && h2 >= 0
253
- # rebuild the byte and push it -- spinel strings are
254
- # byte-blobs, so this works for ASCII; for non-ASCII the
255
- # original encoder would have used a passthrough byte
256
- # anyway.
257
- b = h1 * 16 + h2
258
- out = out + Json.byte_to_chr(b)
245
+ # \uXXXX -> the UTF-8 bytes of the code point; a high surrogate
246
+ # followed by a \uDC00..DFFF escape is one code point.
247
+ cp = Json.hex4(s, pos + 2)
248
+ if cp < 0
249
+ out = out + "?"
250
+ pos += 2
251
+ next
252
+ end
253
+ pos += 6
254
+ if cp >= 0xD800 && cp <= 0xDBFF && pos + 1 < s.length && s[pos] == "\\" && s[pos + 1] == "u"
255
+ lo = Json.hex4(s, pos + 2)
256
+ if lo >= 0xDC00 && lo <= 0xDFFF
257
+ cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00)
259
258
  pos += 6
260
- next
261
259
  end
262
260
  end
263
- out = out + "?"
264
- pos += 2
261
+ out = out + Json.cp_to_utf8(cp)
265
262
  next
266
263
  else
267
264
  out = out + esc
@@ -275,6 +272,43 @@ module SpinelKit
275
272
  out
276
273
  end
277
274
 
275
+ # Four hex digits at s[at, 4] as an integer; -1 when they are not there.
276
+ def self.hex4(s, at)
277
+ if at + 3 >= s.length
278
+ return -1
279
+ end
280
+ v = 0
281
+ i = 0
282
+ while i < 4
283
+ h = Json.hex_nibble(s[at + i])
284
+ if h < 0
285
+ return -1
286
+ end
287
+ v = v * 16 + h
288
+ i += 1
289
+ end
290
+ v
291
+ end
292
+
293
+ # A code point as UTF-8. A surrogate on its own is U+FFFD.
294
+ def self.cp_to_utf8(cp)
295
+ if cp < 0x80
296
+ return cp.chr
297
+ end
298
+ if cp >= 0xD800 && cp <= 0xDFFF
299
+ cp = 0xFFFD
300
+ end
301
+ if cp < 0x800
302
+ return ((0xC0 | (cp >> 6)).chr + (0x80 | (cp & 0x3F)).chr).force_encoding("UTF-8")
303
+ end
304
+ if cp < 0x10000
305
+ return ((0xE0 | (cp >> 12)).chr + (0x80 | ((cp >> 6) & 0x3F)).chr +
306
+ (0x80 | (cp & 0x3F)).chr).force_encoding("UTF-8")
307
+ end
308
+ ((0xF0 | (cp >> 18)).chr + (0x80 | ((cp >> 12) & 0x3F)).chr +
309
+ (0x80 | ((cp >> 6) & 0x3F)).chr + (0x80 | (cp & 0x3F)).chr).force_encoding("UTF-8")
310
+ end
311
+
278
312
  def self.hex_nibble(c)
279
313
  if c >= "0" && c <= "9"
280
314
  return c.getbyte(0) - "0".getbyte(0)
@@ -2,5 +2,5 @@
2
2
  # without loading the rest of the library (the json/git/log modules pull
3
3
  # in no deps, but this matches the toy/tep convention exactly).
4
4
  module SpinelKit
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
@@ -13,6 +13,8 @@ module SpinelKit
13
13
  def self.skip_value: (String s, Integer pos) -> Integer
14
14
  def self.skip_container: (String s, Integer pos) -> Integer
15
15
  def self.parse_str_value: (String s, Integer pos) -> String
16
+ def self.hex4: (String s, Integer at) -> Integer
17
+ def self.cp_to_utf8: (Integer cp) -> String
16
18
  def self.hex_nibble: (String c) -> Integer
17
19
  def self.byte_to_chr: (Integer n) -> String
18
20
  def self.parse_int_value: (String s, Integer pos) -> Integer
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spinel_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ori Pekelman