spinel_kit 0.2.1 → 0.3.0
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 +4 -4
- data/CHANGELOG.md +30 -11
- data/README.md +83 -54
- data/docs/adoption.md +8 -0
- data/docs/spinel-discipline.md +17 -20
- data/spin.toml +6 -0
- data/{lib/spinel_kit → spinel_kit}/hex.rb +3 -7
- data/spinel_kit/version.rb +6 -0
- data/spinel_kit.rb +39 -0
- metadata +18 -24
- data/lib/spinel_kit/json.rb +0 -149
- data/lib/spinel_kit/json_builder.rb +0 -142
- data/lib/spinel_kit/json_decoder.rb +0 -428
- data/lib/spinel_kit/version.rb +0 -6
- data/lib/spinel_kit.rb +0 -45
- data/sig/spinel_kit/json.rbs +0 -15
- data/sig/spinel_kit/json_builder.rbs +0 -19
- data/sig/spinel_kit/json_decoder.rbs +0 -23
- /data/{lib/spinel_kit → spinel_kit}/git.rb +0 -0
- /data/{lib/spinel_kit → spinel_kit}/log.rb +0 -0
- /data/{lib/spinel_kit → spinel_kit}/url.rb +0 -0
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
# SpinelKit::Json::Builder -- an incremental, ordered JSON-object builder.
|
|
2
|
-
#
|
|
3
|
-
# WHY A SEPARATE FILE/CLASS. This is toy's hand-rolled builder; the codec
|
|
4
|
-
# (SpinelKit::Json, encode/decode) is tep's. They are split so a consumer
|
|
5
|
-
# compiles only the surface it uses: Spinel has no tree-shaking, so an
|
|
6
|
-
# uncalled method is still compiled and degrades its params, emitting benign
|
|
7
|
-
# but gate-tripping `emitting 0` warnings. A builder-only consumer requires
|
|
8
|
-
# THIS file and never pays for the decoder walkers; a codec-only consumer
|
|
9
|
-
# requires spinel_kit/json and never pays for the builder.
|
|
10
|
-
#
|
|
11
|
-
# To stay self-contained, Builder carries its OWN escape/quote/hex2 rather
|
|
12
|
-
# than calling SpinelKit::Json.* (which would drag the whole codec, decoders
|
|
13
|
-
# included, into a builder-only compile). These are byte-identical to the
|
|
14
|
-
# codec's; the small duplication buys surface isolation. The same-named
|
|
15
|
-
# methods across the two classes are safe -- the Spinel name-collision bug
|
|
16
|
-
# that once made that dangerous is fixed (see docs/spinel-discipline.md).
|
|
17
|
-
#
|
|
18
|
-
# USAGE (mutating appenders, NOT method-chaining -- chaining is a Spinel
|
|
19
|
-
# poly-degradation risk):
|
|
20
|
-
#
|
|
21
|
-
# j = SpinelKit::Json::Builder.new
|
|
22
|
-
# j.add_str("kind", "run_start")
|
|
23
|
-
# j.add_num("t", now) # int OR float via .to_s
|
|
24
|
-
# host = SpinelKit::Json::Builder.new
|
|
25
|
-
# host.add_str("name", host_name)
|
|
26
|
-
# j.add_obj("host", host) # nest a sub-builder
|
|
27
|
-
# j.add_raw("lr", "0.001") # already-encoded JSON literal
|
|
28
|
-
# ev = j.dump # "{...}"
|
|
29
|
-
#
|
|
30
|
-
# NUMERIC NOTE: if a single compiled program passes BOTH Integer and Float to
|
|
31
|
-
# `add_num`'s `value`, Spinel unifies the param to Float and an Integer
|
|
32
|
-
# renders as "N.0". For hardcoded numeric literals where byte-exact output
|
|
33
|
-
# matters, use `add_raw` with an already-encoded string instead of relying on
|
|
34
|
-
# `value.to_s`.
|
|
35
|
-
module SpinelKit
|
|
36
|
-
class Json
|
|
37
|
-
class Builder
|
|
38
|
-
def initialize
|
|
39
|
-
@buf = "{"
|
|
40
|
-
@first = true
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# Append `"key":"escaped-value"`.
|
|
44
|
-
def add_str(key, value)
|
|
45
|
-
comma
|
|
46
|
-
@buf = @buf + Builder.quote(key) + ":" + Builder.quote(value)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# Append `"key":<number>` -- `value.to_s` covers Integer ("5") and
|
|
50
|
-
# Float ("1.5"). For hardcoded literals prefer add_raw.
|
|
51
|
-
def add_num(key, value)
|
|
52
|
-
comma
|
|
53
|
-
@buf = @buf + Builder.quote(key) + ":" + value.to_s
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# Append `"key":true|false`.
|
|
57
|
-
def add_bool(key, value)
|
|
58
|
-
comma
|
|
59
|
-
@buf = @buf + Builder.quote(key) + ":" + (value ? "true" : "false")
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Append `"key":<already-encoded JSON>` -- for arrays / numeric literals.
|
|
63
|
-
def add_raw(key, raw)
|
|
64
|
-
comma
|
|
65
|
-
@buf = @buf + Builder.quote(key) + ":" + raw
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# Append `"key":<nested object>` from another Builder.
|
|
69
|
-
def add_obj(key, child)
|
|
70
|
-
comma
|
|
71
|
-
@buf = @buf + Builder.quote(key) + ":" + child.dump
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# Close the object and return the JSON string.
|
|
75
|
-
def dump
|
|
76
|
-
@buf + "}"
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
# Emit a separator before every entry except the first.
|
|
80
|
-
def comma
|
|
81
|
-
if @first
|
|
82
|
-
@first = false
|
|
83
|
-
else
|
|
84
|
-
@buf = @buf + ","
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
# ---- self-contained string escaping (byte-identical to
|
|
89
|
-
# SpinelKit::Json.escape/quote/hex2; kept local so a builder-only
|
|
90
|
-
# compile never pulls in the codec) ----
|
|
91
|
-
|
|
92
|
-
# Wrap a string in JSON quotes, escaping its body.
|
|
93
|
-
def self.quote(s)
|
|
94
|
-
"\"" + Builder.escape(s) + "\""
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
# Escape a string for inclusion inside a JSON string literal (no
|
|
98
|
-
# surrounding quotes). Handles ", \, and the JSON control-char escapes
|
|
99
|
-
# (\b \f \n \r \t); other control bytes go through \u00XX. ASCII-clean
|
|
100
|
-
# input passes through unchanged.
|
|
101
|
-
def self.escape(s)
|
|
102
|
-
out = ""
|
|
103
|
-
i = 0
|
|
104
|
-
n = s.length
|
|
105
|
-
while i < n
|
|
106
|
-
c = s[i]
|
|
107
|
-
if c == "\""
|
|
108
|
-
out = out + "\\\""
|
|
109
|
-
elsif c == "\\"
|
|
110
|
-
out = out + "\\\\"
|
|
111
|
-
elsif c == "\n"
|
|
112
|
-
out = out + "\\n"
|
|
113
|
-
elsif c == "\r"
|
|
114
|
-
out = out + "\\r"
|
|
115
|
-
elsif c == "\t"
|
|
116
|
-
out = out + "\\t"
|
|
117
|
-
elsif c == "\b"
|
|
118
|
-
out = out + "\\b"
|
|
119
|
-
elsif c == "\f"
|
|
120
|
-
out = out + "\\f"
|
|
121
|
-
elsif c < " "
|
|
122
|
-
b = c.getbyte(0)
|
|
123
|
-
out = out + "\\u00" + Builder.hex2(b)
|
|
124
|
-
else
|
|
125
|
-
out = out + c
|
|
126
|
-
end
|
|
127
|
-
i += 1
|
|
128
|
-
end
|
|
129
|
-
out
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
# Two-digit lowercase hex of a byte (0..255).
|
|
133
|
-
def self.hex2(n)
|
|
134
|
-
hex = "0123456789abcdef"
|
|
135
|
-
out = ""
|
|
136
|
-
out = out + hex[(n / 16) % 16, 1]
|
|
137
|
-
out = out + hex[n % 16, 1]
|
|
138
|
-
out
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
end
|
|
142
|
-
end
|
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
# SpinelKit::Json -- Spinel-safe JSON DECODERS (flat-key, top-level only).
|
|
2
|
-
#
|
|
3
|
-
# The decode half of the codec; encoders are in spinel_kit/json.rb. Split out
|
|
4
|
-
# so an encode-only consumer never compiles these walkers (their dead-code
|
|
5
|
-
# degradation otherwise widens the encoders' string args to int -- see the
|
|
6
|
-
# header of json.rb and docs/spinel-discipline.md).
|
|
7
|
-
#
|
|
8
|
-
# `get_str(s, key)` finds the entry for `key` in the top-level object literal
|
|
9
|
-
# `s` and returns its value as a string. Returns "" when `key` is absent or
|
|
10
|
-
# the value isn't a string. Same shape for `get_int`. `has_key?(s, key)`
|
|
11
|
-
# returns a boolean independent of value type. The parser is a hand-rolled
|
|
12
|
-
# state machine that walks one `{ "k": <value>, ... }` pair at a time,
|
|
13
|
-
# skipping over any value (including nested objects / arrays) it doesn't need.
|
|
14
|
-
# Strings inside values are honoured for escape sequences so that `\"` doesn't
|
|
15
|
-
# terminate the string and corrupt the walk. Decodes the escape sequences
|
|
16
|
-
# `SpinelKit::Json.escape` produces.
|
|
17
|
-
module SpinelKit
|
|
18
|
-
class Json
|
|
19
|
-
def self.get_str(s, key)
|
|
20
|
-
pos = Json.find_value_start(s, key)
|
|
21
|
-
if pos < 0
|
|
22
|
-
return ""
|
|
23
|
-
end
|
|
24
|
-
Json.parse_str_value(s, pos)
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def self.get_int(s, key)
|
|
28
|
-
pos = Json.find_value_start(s, key)
|
|
29
|
-
if pos < 0
|
|
30
|
-
return 0
|
|
31
|
-
end
|
|
32
|
-
Json.parse_int_value(s, pos)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Decode a JSON number value at `key` -> Float. Accepts both
|
|
36
|
-
# integer-literal (`42`) and float-literal (`3.14`, `-0.5`, `1e2`)
|
|
37
|
-
# JSON-number syntax; the integer form returns N.0. Missing key or
|
|
38
|
-
# malformed value returns 0.0 (consistent with the other getters'
|
|
39
|
-
# missing-key defaults).
|
|
40
|
-
#
|
|
41
|
-
# Implementation: delegates the value-span walking to skip_value (already
|
|
42
|
-
# handles all JSON-number syntax + structural-char boundaries), then
|
|
43
|
-
# String#to_f on the substring. Inlined rather than factored into a
|
|
44
|
-
# parse_float_value helper because spinel's type inference mis-widens `s`
|
|
45
|
-
# to int through the indirection. NOTE: that is a value-walk indirection
|
|
46
|
-
# concern, NOT the name-collision bug (which was fixed) -- keep it inlined.
|
|
47
|
-
def self.get_float(s, key)
|
|
48
|
-
pos = Json.find_value_start(s, key)
|
|
49
|
-
if pos < 0
|
|
50
|
-
return 0.0
|
|
51
|
-
end
|
|
52
|
-
pos = Json.skip_ws(s, pos)
|
|
53
|
-
if pos >= s.length
|
|
54
|
-
return 0.0
|
|
55
|
-
end
|
|
56
|
-
end_pos = Json.skip_value(s, pos)
|
|
57
|
-
if end_pos <= pos
|
|
58
|
-
return 0.0
|
|
59
|
-
end
|
|
60
|
-
s[pos, end_pos - pos].to_f
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def self.has_key?(s, key)
|
|
64
|
-
Json.find_value_start(s, key) >= 0
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
# Decode a flat JSON array of integers at `key` -> Array[Integer].
|
|
68
|
-
# A missing or non-array value yields [] (the typed-empty-array idiom);
|
|
69
|
-
# non-int elements are skipped.
|
|
70
|
-
def self.get_int_array(s, key)
|
|
71
|
-
out = [0]
|
|
72
|
-
out.delete_at(0)
|
|
73
|
-
pos = Json.find_value_start(s, key)
|
|
74
|
-
if pos < 0
|
|
75
|
-
return out
|
|
76
|
-
end
|
|
77
|
-
pos = Json.skip_ws(s, pos)
|
|
78
|
-
if pos >= s.length || s[pos] != "["
|
|
79
|
-
return out
|
|
80
|
-
end
|
|
81
|
-
pos += 1
|
|
82
|
-
while pos < s.length
|
|
83
|
-
pos = Json.skip_ws(s, pos)
|
|
84
|
-
if pos >= s.length
|
|
85
|
-
return out
|
|
86
|
-
end
|
|
87
|
-
c = s[pos]
|
|
88
|
-
if c == "]"
|
|
89
|
-
return out
|
|
90
|
-
elsif c == ","
|
|
91
|
-
pos += 1
|
|
92
|
-
elsif (c >= "0" && c <= "9") || c == "-"
|
|
93
|
-
out.push(Json.parse_int_value(s, pos))
|
|
94
|
-
# Advance past the number parse_int_value just consumed
|
|
95
|
-
# (optional '-' then digits).
|
|
96
|
-
if s[pos] == "-"
|
|
97
|
-
pos += 1
|
|
98
|
-
end
|
|
99
|
-
while pos < s.length && s[pos] >= "0" && s[pos] <= "9"
|
|
100
|
-
pos += 1
|
|
101
|
-
end
|
|
102
|
-
else
|
|
103
|
-
# Non-int element (string / object / etc.): skip it.
|
|
104
|
-
pos = Json.skip_value(s, pos)
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
out
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# ---- Internal helpers ----
|
|
111
|
-
|
|
112
|
-
# Skip whitespace starting at `pos`, return the new position.
|
|
113
|
-
def self.skip_ws(s, pos)
|
|
114
|
-
while pos < s.length
|
|
115
|
-
c = s[pos]
|
|
116
|
-
if c == " " || c == "\t" || c == "\n" || c == "\r"
|
|
117
|
-
pos += 1
|
|
118
|
-
else
|
|
119
|
-
return pos
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
pos
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
# Walk a JSON-quoted string starting at `pos` (which must point at the
|
|
126
|
-
# opening `"`). Returns the position one past the closing `"`. Returns
|
|
127
|
-
# -1 on malformed input.
|
|
128
|
-
def self.skip_str(s, pos)
|
|
129
|
-
if pos >= s.length || s[pos] != "\""
|
|
130
|
-
return -1
|
|
131
|
-
end
|
|
132
|
-
pos += 1
|
|
133
|
-
while pos < s.length
|
|
134
|
-
c = s[pos]
|
|
135
|
-
if c == "\\"
|
|
136
|
-
# Skip the escape and the escaped character. \uXXXX spans 6
|
|
137
|
-
# chars total but skipping 2 still keeps us inside the string
|
|
138
|
-
# for the rest of the walk -- the remaining 4 hex digits look
|
|
139
|
-
# like ordinary string bytes and won't terminate the literal.
|
|
140
|
-
pos += 2
|
|
141
|
-
elsif c == "\""
|
|
142
|
-
return pos + 1
|
|
143
|
-
else
|
|
144
|
-
pos += 1
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
-1
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Walk a JSON value starting at `pos` (which must point at the first
|
|
151
|
-
# non-ws char of the value). Returns the position one past the value
|
|
152
|
-
# (or the input length on truncation).
|
|
153
|
-
def self.skip_value(s, pos)
|
|
154
|
-
pos = Json.skip_ws(s, pos)
|
|
155
|
-
if pos >= s.length
|
|
156
|
-
return pos
|
|
157
|
-
end
|
|
158
|
-
c = s[pos]
|
|
159
|
-
if c == "\""
|
|
160
|
-
return Json.skip_str(s, pos)
|
|
161
|
-
end
|
|
162
|
-
if c == "{" || c == "["
|
|
163
|
-
return Json.skip_container(s, pos)
|
|
164
|
-
end
|
|
165
|
-
# number / true / false / null -- read until the next structural /
|
|
166
|
-
# whitespace char.
|
|
167
|
-
while pos < s.length
|
|
168
|
-
c = s[pos]
|
|
169
|
-
if c == "," || c == "}" || c == "]" ||
|
|
170
|
-
c == " " || c == "\t" || c == "\n" || c == "\r"
|
|
171
|
-
return pos
|
|
172
|
-
end
|
|
173
|
-
pos += 1
|
|
174
|
-
end
|
|
175
|
-
pos
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
# Walk a balanced { ... } or [ ... ] starting at `pos`. Honours string
|
|
179
|
-
# literals so that `{` / `}` inside a value-string don't confuse the
|
|
180
|
-
# brace counter. Returns position one past the matching closer.
|
|
181
|
-
def self.skip_container(s, pos)
|
|
182
|
-
open_c = s[pos]
|
|
183
|
-
close_c = open_c == "{" ? "}" : "]"
|
|
184
|
-
depth = 1
|
|
185
|
-
pos += 1
|
|
186
|
-
while pos < s.length && depth > 0
|
|
187
|
-
c = s[pos]
|
|
188
|
-
if c == "\""
|
|
189
|
-
# whole nested string -- skip past it
|
|
190
|
-
npos = Json.skip_str(s, pos)
|
|
191
|
-
if npos < 0
|
|
192
|
-
return s.length
|
|
193
|
-
end
|
|
194
|
-
pos = npos
|
|
195
|
-
elsif c == open_c
|
|
196
|
-
depth += 1
|
|
197
|
-
pos += 1
|
|
198
|
-
elsif c == close_c
|
|
199
|
-
depth -= 1
|
|
200
|
-
pos += 1
|
|
201
|
-
else
|
|
202
|
-
pos += 1
|
|
203
|
-
end
|
|
204
|
-
end
|
|
205
|
-
pos
|
|
206
|
-
end
|
|
207
|
-
|
|
208
|
-
# Read a JSON-quoted string at `pos` and return its decoded contents
|
|
209
|
-
# (no surrounding quotes). Decodes the same escape sequences that
|
|
210
|
-
# `escape` produces. Returns "" on malformed input.
|
|
211
|
-
def self.parse_str_value(s, pos)
|
|
212
|
-
pos = Json.skip_ws(s, pos)
|
|
213
|
-
if pos >= s.length || s[pos] != "\""
|
|
214
|
-
return ""
|
|
215
|
-
end
|
|
216
|
-
pos += 1
|
|
217
|
-
out = ""
|
|
218
|
-
while pos < s.length
|
|
219
|
-
c = s[pos]
|
|
220
|
-
if c == "\""
|
|
221
|
-
return out
|
|
222
|
-
end
|
|
223
|
-
if c == "\\"
|
|
224
|
-
if pos + 1 >= s.length
|
|
225
|
-
return out
|
|
226
|
-
end
|
|
227
|
-
esc = s[pos + 1]
|
|
228
|
-
if esc == "\""
|
|
229
|
-
out = out + "\""
|
|
230
|
-
elsif esc == "\\"
|
|
231
|
-
out = out + "\\"
|
|
232
|
-
elsif esc == "/"
|
|
233
|
-
out = out + "/"
|
|
234
|
-
elsif esc == "n"
|
|
235
|
-
out = out + "\n"
|
|
236
|
-
elsif esc == "r"
|
|
237
|
-
out = out + "\r"
|
|
238
|
-
elsif esc == "t"
|
|
239
|
-
out = out + "\t"
|
|
240
|
-
elsif esc == "b"
|
|
241
|
-
out = out + "\b"
|
|
242
|
-
elsif esc == "f"
|
|
243
|
-
out = out + "\f"
|
|
244
|
-
elsif esc == "u"
|
|
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)
|
|
258
|
-
pos += 6
|
|
259
|
-
end
|
|
260
|
-
end
|
|
261
|
-
out = out + Json.cp_to_utf8(cp)
|
|
262
|
-
next
|
|
263
|
-
else
|
|
264
|
-
out = out + esc
|
|
265
|
-
end
|
|
266
|
-
pos += 2
|
|
267
|
-
else
|
|
268
|
-
out = out + c
|
|
269
|
-
pos += 1
|
|
270
|
-
end
|
|
271
|
-
end
|
|
272
|
-
out
|
|
273
|
-
end
|
|
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
|
-
|
|
312
|
-
def self.hex_nibble(c)
|
|
313
|
-
if c >= "0" && c <= "9"
|
|
314
|
-
return c.getbyte(0) - "0".getbyte(0)
|
|
315
|
-
end
|
|
316
|
-
if c >= "a" && c <= "f"
|
|
317
|
-
return c.getbyte(0) - "a".getbyte(0) + 10
|
|
318
|
-
end
|
|
319
|
-
if c >= "A" && c <= "F"
|
|
320
|
-
return c.getbyte(0) - "A".getbyte(0) + 10
|
|
321
|
-
end
|
|
322
|
-
-1
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
# Build a single-byte string from an integer 0..255. Spinel doesn't
|
|
326
|
-
# expose `n.chr` for arbitrary bytes uniformly; the table covers the
|
|
327
|
-
# ASCII printable range and falls back to "?" for anything else (the
|
|
328
|
-
# JSON encoder side never produces non-ASCII via \u, so the fallback
|
|
329
|
-
# is reachable only for malformed input).
|
|
330
|
-
def self.byte_to_chr(n)
|
|
331
|
-
printable = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
|
332
|
-
if n >= 32 && n < 127
|
|
333
|
-
return printable[n - 32, 1]
|
|
334
|
-
end
|
|
335
|
-
if n == 9
|
|
336
|
-
return "\t"
|
|
337
|
-
end
|
|
338
|
-
if n == 10
|
|
339
|
-
return "\n"
|
|
340
|
-
end
|
|
341
|
-
if n == 13
|
|
342
|
-
return "\r"
|
|
343
|
-
end
|
|
344
|
-
"?"
|
|
345
|
-
end
|
|
346
|
-
|
|
347
|
-
# Read an integer at `pos`. Accepts an optional leading `-`. Returns 0
|
|
348
|
-
# on no-digit / non-numeric input (caller can use `has_key?` first if
|
|
349
|
-
# 0-vs-absent matters).
|
|
350
|
-
def self.parse_int_value(s, pos)
|
|
351
|
-
pos = Json.skip_ws(s, pos)
|
|
352
|
-
if pos >= s.length
|
|
353
|
-
return 0
|
|
354
|
-
end
|
|
355
|
-
neg = false
|
|
356
|
-
if s[pos] == "-"
|
|
357
|
-
neg = true
|
|
358
|
-
pos += 1
|
|
359
|
-
end
|
|
360
|
-
n = 0
|
|
361
|
-
saw_digit = false
|
|
362
|
-
while pos < s.length
|
|
363
|
-
c = s[pos]
|
|
364
|
-
if c >= "0" && c <= "9"
|
|
365
|
-
n = n * 10 + (c.getbyte(0) - "0".getbyte(0))
|
|
366
|
-
saw_digit = true
|
|
367
|
-
pos += 1
|
|
368
|
-
else
|
|
369
|
-
break
|
|
370
|
-
end
|
|
371
|
-
end
|
|
372
|
-
if !saw_digit
|
|
373
|
-
return 0
|
|
374
|
-
end
|
|
375
|
-
neg ? -n : n
|
|
376
|
-
end
|
|
377
|
-
|
|
378
|
-
# Walk the top-level object looking for the entry whose key matches
|
|
379
|
-
# `target_key`; return the position of the value's first non-ws
|
|
380
|
-
# character. Returns -1 if not found.
|
|
381
|
-
def self.find_value_start(s, target_key)
|
|
382
|
-
pos = Json.skip_ws(s, 0)
|
|
383
|
-
if pos >= s.length || s[pos] != "{"
|
|
384
|
-
return -1
|
|
385
|
-
end
|
|
386
|
-
pos += 1
|
|
387
|
-
while pos < s.length
|
|
388
|
-
pos = Json.skip_ws(s, pos)
|
|
389
|
-
if pos >= s.length
|
|
390
|
-
return -1
|
|
391
|
-
end
|
|
392
|
-
if s[pos] == "}"
|
|
393
|
-
return -1
|
|
394
|
-
end
|
|
395
|
-
# Read a key.
|
|
396
|
-
if s[pos] != "\""
|
|
397
|
-
return -1
|
|
398
|
-
end
|
|
399
|
-
key_start = pos
|
|
400
|
-
pos = Json.skip_str(s, pos)
|
|
401
|
-
if pos < 0
|
|
402
|
-
return -1
|
|
403
|
-
end
|
|
404
|
-
# Decode the key for comparison (handles \" inside keys).
|
|
405
|
-
key = Json.parse_str_value(s, key_start)
|
|
406
|
-
# Skip ws, ":".
|
|
407
|
-
pos = Json.skip_ws(s, pos)
|
|
408
|
-
if pos >= s.length || s[pos] != ":"
|
|
409
|
-
return -1
|
|
410
|
-
end
|
|
411
|
-
pos += 1
|
|
412
|
-
pos = Json.skip_ws(s, pos)
|
|
413
|
-
if key == target_key
|
|
414
|
-
return pos
|
|
415
|
-
end
|
|
416
|
-
# Skip the value, then the comma (if any).
|
|
417
|
-
pos = Json.skip_value(s, pos)
|
|
418
|
-
pos = Json.skip_ws(s, pos)
|
|
419
|
-
if pos < s.length && s[pos] == ","
|
|
420
|
-
pos += 1
|
|
421
|
-
elsif pos < s.length && s[pos] == "}"
|
|
422
|
-
return -1
|
|
423
|
-
end
|
|
424
|
-
end
|
|
425
|
-
-1
|
|
426
|
-
end
|
|
427
|
-
end
|
|
428
|
-
end
|
data/lib/spinel_kit/version.rb
DELETED
data/lib/spinel_kit.rb
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# SpinelKit -- the Spinel stdlib-surface gem.
|
|
2
|
-
#
|
|
3
|
-
# A pure-Ruby, Spinel-safe toolkit holding the generic "stdlib substitute"
|
|
4
|
-
# shims that every Spinel-compiled project would otherwise hand-roll. Spinel
|
|
5
|
-
# (the Ruby->native AOT compiler) cannot lower large chunks of CRuby stdlib
|
|
6
|
-
# -- the `json` gem's C-ext fast path and metaprogrammed pure-Ruby fallback,
|
|
7
|
-
# stdlib `Logger`, C-ext git bindings -- and the spinelgems compatibility
|
|
8
|
-
# catalog confirms there is no verified gem to reuse for any of them. So this
|
|
9
|
-
# gem consolidates the shims toy and tep each grew independently:
|
|
10
|
-
#
|
|
11
|
-
# SpinelKit::Json -- JSON encoders (json.rb) + flat-key decoders
|
|
12
|
-
# (json_decoder.rb).
|
|
13
|
-
# SpinelKit::Json::Builder -- incremental ordered-object builder.
|
|
14
|
-
# SpinelKit::Hex -- hex digit/byte encode + decode.
|
|
15
|
-
# SpinelKit::Url -- percent-encode/decode + form-query + URL split.
|
|
16
|
-
# SpinelKit::Git -- git provenance from .git/HEAD (was Toy::Git).
|
|
17
|
-
# SpinelKit::Log -- minimal levelled logger (was Tep::Logger).
|
|
18
|
-
#
|
|
19
|
-
# MINIMAL-SURFACE REQUIRING. This umbrella requires everything for
|
|
20
|
-
# convenience (and for CRuby use). But because Spinel compiles every loaded
|
|
21
|
-
# method with no tree-shaking, a Spinel-compiled consumer should require ONLY
|
|
22
|
-
# the file(s) it uses, to avoid compiling -- and degrading -- code it never
|
|
23
|
-
# calls:
|
|
24
|
-
#
|
|
25
|
-
# require "spinel_kit/json" # encoders
|
|
26
|
-
# require "spinel_kit/json_decoder" # decoders (require alongside json if you decode)
|
|
27
|
-
# require "spinel_kit/json_builder" # builder
|
|
28
|
-
# require "spinel_kit/hex" # hex digit/byte codec
|
|
29
|
-
# require "spinel_kit/url" # percent-codec + query (pulls in hex)
|
|
30
|
-
# require "spinel_kit/git"
|
|
31
|
-
# require "spinel_kit/log"
|
|
32
|
-
#
|
|
33
|
-
# No native extension (spinel-ext.json is []), no runtime dependencies. See
|
|
34
|
-
# docs/adoption.md and docs/spinel-discipline.md.
|
|
35
|
-
require_relative "spinel_kit/version"
|
|
36
|
-
require_relative "spinel_kit/json"
|
|
37
|
-
require_relative "spinel_kit/json_decoder"
|
|
38
|
-
require_relative "spinel_kit/json_builder"
|
|
39
|
-
require_relative "spinel_kit/hex"
|
|
40
|
-
require_relative "spinel_kit/url"
|
|
41
|
-
require_relative "spinel_kit/git"
|
|
42
|
-
require_relative "spinel_kit/log"
|
|
43
|
-
|
|
44
|
-
module SpinelKit
|
|
45
|
-
end
|
data/sig/spinel_kit/json.rbs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# Advisory RBS for SpinelKit::Json encoders (lib/spinel_kit/json.rb).
|
|
2
|
-
# Decoders are in json_decoder.rbs; the builder in json_builder.rbs.
|
|
3
|
-
module SpinelKit
|
|
4
|
-
class Json
|
|
5
|
-
def self.escape: (String s) -> String
|
|
6
|
-
def self.hex2: (Integer n) -> String
|
|
7
|
-
def self.quote: (String s) -> String
|
|
8
|
-
def self.encode_pair_str: (String k, String v) -> String
|
|
9
|
-
def self.encode_pair_int: (String k, Integer v) -> String
|
|
10
|
-
def self.from_str_hash: (Hash[String, String] h) -> String
|
|
11
|
-
def self.from_int_hash: (Hash[String, Integer] h) -> String
|
|
12
|
-
def self.from_str_array: (Array[String] a) -> String
|
|
13
|
-
def self.from_int_array: (Array[Integer] a) -> String
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
module SpinelKit
|
|
2
|
-
class Json
|
|
3
|
-
class Builder
|
|
4
|
-
def initialize: () -> void
|
|
5
|
-
def add_str: (String key, String value) -> String
|
|
6
|
-
def add_num: (String key, untyped value) -> String
|
|
7
|
-
def add_bool: (String key, bool value) -> String
|
|
8
|
-
def add_raw: (String key, String raw) -> String
|
|
9
|
-
def add_obj: (String key, SpinelKit::Json::Builder child) -> String
|
|
10
|
-
def dump: () -> String
|
|
11
|
-
def comma: () -> void
|
|
12
|
-
|
|
13
|
-
# self-contained escapers (byte-identical to SpinelKit::Json.*)
|
|
14
|
-
def self.quote: (String s) -> String
|
|
15
|
-
def self.escape: (String s) -> String
|
|
16
|
-
def self.hex2: (Integer n) -> String
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# Advisory RBS for SpinelKit::Json decoders (lib/spinel_kit/json_decoder.rb).
|
|
2
|
-
module SpinelKit
|
|
3
|
-
class Json
|
|
4
|
-
def self.get_str: (String s, String key) -> String
|
|
5
|
-
def self.get_int: (String s, String key) -> Integer
|
|
6
|
-
def self.get_float: (String s, String key) -> Float
|
|
7
|
-
def self.get_int_array: (String s, String key) -> Array[Integer]
|
|
8
|
-
def self.has_key?: (String s, String key) -> bool
|
|
9
|
-
|
|
10
|
-
# internal walkers
|
|
11
|
-
def self.skip_ws: (String s, Integer pos) -> Integer
|
|
12
|
-
def self.skip_str: (String s, Integer pos) -> Integer
|
|
13
|
-
def self.skip_value: (String s, Integer pos) -> Integer
|
|
14
|
-
def self.skip_container: (String s, Integer pos) -> Integer
|
|
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
|
|
18
|
-
def self.hex_nibble: (String c) -> Integer
|
|
19
|
-
def self.byte_to_chr: (Integer n) -> String
|
|
20
|
-
def self.parse_int_value: (String s, Integer pos) -> Integer
|
|
21
|
-
def self.find_value_start: (String s, String target_key) -> Integer
|
|
22
|
-
end
|
|
23
|
-
end
|
|
File without changes
|
|
File without changes
|
|
File without changes
|