fast-sfv 0.1.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 +7 -0
- data/CHANGELOG.md +71 -0
- data/README.md +97 -0
- data/lib/fast/sfv/errors.rb +44 -0
- data/lib/fast/sfv/parser.rb +641 -0
- data/lib/fast/sfv/serializer.rb +581 -0
- data/lib/fast/sfv/types.rb +134 -0
- data/lib/fast/sfv/version.rb +7 -0
- data/lib/fast/sfv.rb +49 -0
- metadata +55 -0
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module SFV
|
|
5
|
+
# The RFC 9651 §4.1 emit path: module functions appending bytes into
|
|
6
|
+
# one output String, which is the only object an emission allocates —
|
|
7
|
+
# append-only, byte-index validation, no pattern machinery (the parse
|
|
8
|
+
# path's own discipline). The separator loops carry a first-member
|
|
9
|
+
# flag because #each_with_index allocates (two objects per Array
|
|
10
|
+
# call, a pair Array per Hash entry) and the budgets are zero-waste. The Ruby type of a value IS its wire-type
|
|
11
|
+
# tag: an Integer is an integer, a Float a decimal, a Token a token,
|
|
12
|
+
# a String an sf-string unless it is tagged ASCII-8BIT, which is a
|
|
13
|
+
# byte sequence — the content never decides the type, so "foo" the
|
|
14
|
+
# String stays a quoted sf-string and Fast::SFV::Token.new("foo") a
|
|
15
|
+
# bare token (RFC 9213 §2.1 forbids inferring a type from content),
|
|
16
|
+
# and the Token arm is matched before the String arm in the dispatch
|
|
17
|
+
# because a Token IS a String — a binary-tagged Token still emits
|
|
18
|
+
# bare. Everything non-serializable raises SerializeError carrying
|
|
19
|
+
# the output's byte offset where the emit stopped; the grammar byte
|
|
20
|
+
# classes are the parse path's (parser.rb owns them, the emit path
|
|
21
|
+
# reads the same sets from the value side).
|
|
22
|
+
module Serializer
|
|
23
|
+
# The wire grammar classes the emit path validates against: a key's
|
|
24
|
+
# first byte and continuation (§4.1.1.3), a token's first byte and
|
|
25
|
+
# continuation (§4.1.7) — the same sets the parser reads.
|
|
26
|
+
KEY_FIRST = Parser::KEY_FIRST
|
|
27
|
+
KEY_BODY = Parser::KEY_BODY
|
|
28
|
+
TOKEN_FIRST = Parser::TOKEN_FIRST
|
|
29
|
+
TOKEN_BODY = Parser::TOKEN_BODY
|
|
30
|
+
# One DIGIT of a decimal's Float#to_s digits (§4.1.5).
|
|
31
|
+
DIGIT = Parser::DIGIT
|
|
32
|
+
# The byte a display string always percent-escapes, and the DQUOTE
|
|
33
|
+
# both wire forms share (§4.1.11 step 4.1.1).
|
|
34
|
+
PERCENT = Parser::PERCENT
|
|
35
|
+
DQUOTE = Parser::DQUOTE
|
|
36
|
+
# The decimal walk's sign and point, and the SP floor a string and
|
|
37
|
+
# display string validate against — the parse path's own byte
|
|
38
|
+
# constants, aliased so the emit path reads them by name.
|
|
39
|
+
HYPHEN = Parser::HYPHEN
|
|
40
|
+
PERIOD = Parser::PERIOD
|
|
41
|
+
SP = Parser::SP
|
|
42
|
+
# The two bytes an sf-string escape carries a backslash for: DQUOTE
|
|
43
|
+
# and backslash (§4.1.6 step 4.1) — the parse path's own class.
|
|
44
|
+
STRING_ESCAPE = Parser::STRING_ESCAPE
|
|
45
|
+
# The bytes of the decimal walk's two non-digit characters: the
|
|
46
|
+
# exponent marker and its "+" sign (a "-" is Parser::HYPHEN, the
|
|
47
|
+
# point Parser::PERIOD).
|
|
48
|
+
EXPONENT = 0x65
|
|
49
|
+
PLUS = 0x2B
|
|
50
|
+
# The base64 alphabet of a byte sequence (§4.1.8, RFC 4648 §4):
|
|
51
|
+
# 6-bit groups index it, getbyte hands back the byte to append.
|
|
52
|
+
BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
53
|
+
# The lowercase hex digits of a display-string percent escape
|
|
54
|
+
# (§4.1.11 step 4.1.2).
|
|
55
|
+
HEX = "0123456789abcdef"
|
|
56
|
+
# 10 to the 0th..18th power: the places an integer's base-10 digits
|
|
57
|
+
# are spelled against, and the digit-count bound a Float#to_s
|
|
58
|
+
# mantissa (at most 17 significant digits) can never pass.
|
|
59
|
+
POWERS = (0..18).map { |power| 10**power }.freeze
|
|
60
|
+
# The decimal range of §4.1.4 step 1.
|
|
61
|
+
INTEGER_MAX = 999_999_999_999_999
|
|
62
|
+
|
|
63
|
+
module_function
|
|
64
|
+
|
|
65
|
+
# The wire form of one Item (§4.1.3) — serialize_item's answer and
|
|
66
|
+
# Item#to_s: the bare item, then its parameters.
|
|
67
|
+
def item_string(item)
|
|
68
|
+
output = "".b
|
|
69
|
+
emit_item(output, item)
|
|
70
|
+
output.force_encoding(Encoding::US_ASCII)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The wire form of one Inner List (§4.1.1.1) — InnerList#to_s.
|
|
74
|
+
def inner_list_string(inner_list)
|
|
75
|
+
output = "".b
|
|
76
|
+
emit_inner_list(output, inner_list)
|
|
77
|
+
output.force_encoding(Encoding::US_ASCII)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The wire form of one List (§4.1.1) — serialize_list's answer. The
|
|
81
|
+
# empty case never reaches here: the entry point answers nil (§4.1
|
|
82
|
+
# step 1, the omitted field).
|
|
83
|
+
def list_string(list)
|
|
84
|
+
output = "".b
|
|
85
|
+
first = true
|
|
86
|
+
list.each do |member|
|
|
87
|
+
output << ", " unless first
|
|
88
|
+
first = false
|
|
89
|
+
case member
|
|
90
|
+
when Item
|
|
91
|
+
emit_item(output, member)
|
|
92
|
+
when InnerList
|
|
93
|
+
emit_inner_list(output, member)
|
|
94
|
+
else
|
|
95
|
+
fail_serialize(output, "a list member must be an Item or an InnerList (got #{shape(member)})")
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
output.force_encoding(Encoding::US_ASCII)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# The wire form of one Dictionary (§4.1.2) — serialize_dict's
|
|
102
|
+
# answer, with the empty case omitted by the entry point.
|
|
103
|
+
def dictionary_string(dictionary)
|
|
104
|
+
output = "".b
|
|
105
|
+
first = true
|
|
106
|
+
dictionary.each do |key, member|
|
|
107
|
+
output << ", " unless first
|
|
108
|
+
first = false
|
|
109
|
+
emit_dictionary_member(output, key, member)
|
|
110
|
+
end
|
|
111
|
+
output.force_encoding(Encoding::US_ASCII)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# One dictionary entry: the key, then "=" and the member — or the
|
|
115
|
+
# valueless short form when the Item's value is Boolean true.
|
|
116
|
+
def emit_dictionary_member(output, key, member)
|
|
117
|
+
emit_key(output, key)
|
|
118
|
+
case member
|
|
119
|
+
when InnerList
|
|
120
|
+
output << "="
|
|
121
|
+
emit_inner_list(output, member)
|
|
122
|
+
when Item
|
|
123
|
+
if member.value.equal?(true)
|
|
124
|
+
emit_parameters(output, member.params)
|
|
125
|
+
else
|
|
126
|
+
output << "="
|
|
127
|
+
emit_item(output, member)
|
|
128
|
+
end
|
|
129
|
+
else
|
|
130
|
+
fail_serialize(output, "a dictionary value must be an Item or an InnerList (got #{shape(member)})")
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The wire form of one Date (§4.1.10) — Date#to_s.
|
|
135
|
+
def date_string(date)
|
|
136
|
+
output = "".b
|
|
137
|
+
emit_date(output, date)
|
|
138
|
+
output.force_encoding(Encoding::US_ASCII)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The wire form of one Display String (§4.1.11) —
|
|
142
|
+
# DisplayString#to_s.
|
|
143
|
+
def display_string_string(display_string)
|
|
144
|
+
output = "".b
|
|
145
|
+
emit_display_string(output, display_string)
|
|
146
|
+
output.force_encoding(Encoding::US_ASCII)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# §4.1.3 Serializing an Item: the bare item, then its parameters.
|
|
150
|
+
def emit_item(output, item)
|
|
151
|
+
emit_bare_item(output, item.value)
|
|
152
|
+
emit_parameters(output, item.params)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# §4.1.1.1 Serializing an Inner List: "(", the items joined by
|
|
156
|
+
# single SPs, ")", then the list's own parameters after the close.
|
|
157
|
+
# An empty inner list is "()" — only the top level omits a field.
|
|
158
|
+
def emit_inner_list(output, inner_list)
|
|
159
|
+
output << "("
|
|
160
|
+
items = inner_list.items
|
|
161
|
+
fail_serialize(output, "an inner list's items must be an Array (got #{shape(items)})") unless items.is_a?(Array)
|
|
162
|
+
|
|
163
|
+
first = true
|
|
164
|
+
items.each do |item|
|
|
165
|
+
output << " " unless first
|
|
166
|
+
first = false
|
|
167
|
+
fail_serialize(output, "an inner list member must be an Item (got #{shape(item)})") unless item.is_a?(Item)
|
|
168
|
+
|
|
169
|
+
emit_item(output, item)
|
|
170
|
+
end
|
|
171
|
+
output << ")"
|
|
172
|
+
emit_parameters(output, inner_list.params)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# §4.1.1.2 Serializing Parameters: ";" then the key, then "= value"
|
|
176
|
+
# unless the value is Boolean true — the ";key" short form. The
|
|
177
|
+
# Hash's iteration order is the wire order.
|
|
178
|
+
def emit_parameters(output, params)
|
|
179
|
+
fail_serialize(output, "parameters must be a Hash (got #{shape(params)})") unless params.is_a?(Hash)
|
|
180
|
+
|
|
181
|
+
params.each do |key, value|
|
|
182
|
+
output << ";"
|
|
183
|
+
emit_key(output, key)
|
|
184
|
+
unless value.equal?(true)
|
|
185
|
+
output << "="
|
|
186
|
+
emit_bare_item(output, value)
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# §4.1.1.3 Serializing a Key: the first byte lcalpha or "*", the
|
|
192
|
+
# continuation KEY_BODY — the class the parse path reads; then the
|
|
193
|
+
# key's bytes are appended whole.
|
|
194
|
+
def emit_key(output, key)
|
|
195
|
+
fail_serialize(output, "a key must be a String (got #{shape(key)})") unless key.is_a?(String)
|
|
196
|
+
|
|
197
|
+
size = key.bytesize
|
|
198
|
+
fail_serialize(output, "a key must not be empty") if size.zero?
|
|
199
|
+
fail_serialize(output, "a key must start with a lowercase letter or '*'") unless KEY_FIRST[key.getbyte(0)]
|
|
200
|
+
|
|
201
|
+
index = 1
|
|
202
|
+
while index < size
|
|
203
|
+
unless KEY_BODY[key.getbyte(index)]
|
|
204
|
+
fail_serialize(output, "a key may only carry lcalpha, DIGIT, '_', '-', '.' and '*'")
|
|
205
|
+
end
|
|
206
|
+
index += 1
|
|
207
|
+
end
|
|
208
|
+
output << key
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# §4.1.3.1 Serializing a Bare Item: the Ruby type is the dispatch —
|
|
212
|
+
# Integer, Float, Token (a token: the arm is matched before the
|
|
213
|
+
# String arm, because a Token IS a String), String (an sf-string,
|
|
214
|
+
# or a byte sequence when tagged ASCII-8BIT), true, false, Date
|
|
215
|
+
# and DisplayString. Anything else fails — a Symbol is wrong-typed
|
|
216
|
+
# input now that tokens are Tokens; there is no coercion and no
|
|
217
|
+
# content inference.
|
|
218
|
+
def emit_bare_item(output, value)
|
|
219
|
+
case value
|
|
220
|
+
when Integer
|
|
221
|
+
emit_integer(output, value)
|
|
222
|
+
when Float
|
|
223
|
+
emit_decimal(output, value)
|
|
224
|
+
when Token
|
|
225
|
+
emit_token(output, value)
|
|
226
|
+
when String
|
|
227
|
+
if value.encoding == Encoding::ASCII_8BIT
|
|
228
|
+
emit_byte_sequence(output, value)
|
|
229
|
+
else
|
|
230
|
+
emit_string(output, value)
|
|
231
|
+
end
|
|
232
|
+
when true
|
|
233
|
+
output << "?1"
|
|
234
|
+
when false
|
|
235
|
+
output << "?0"
|
|
236
|
+
when Date
|
|
237
|
+
emit_date(output, value)
|
|
238
|
+
when DisplayString
|
|
239
|
+
emit_display_string(output, value)
|
|
240
|
+
else
|
|
241
|
+
fail_serialize(output, "a bare item must be one of the eight bare types (got #{shape(value)})")
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# §4.1.4 Serializing an Integer: "-" when negative, then the
|
|
246
|
+
# base-10 digits spelled by division against the POWERS table — no
|
|
247
|
+
# String fragment per value. Zero is "0"; the range is the 15-digit
|
|
248
|
+
# one.
|
|
249
|
+
def emit_integer(output, value)
|
|
250
|
+
magnitude = value.abs
|
|
251
|
+
fail_serialize(output, "an integer is at most 999999999999999") if magnitude > INTEGER_MAX
|
|
252
|
+
output << "-" if value.negative?
|
|
253
|
+
emit_digits(output, magnitude)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Spells +magnitude+ in base 10, most significant digit first: the
|
|
257
|
+
# shared tail of integer, decimal and date emission.
|
|
258
|
+
def emit_digits(output, magnitude)
|
|
259
|
+
power = 15
|
|
260
|
+
power -= 1 while power.positive? && POWERS[power] > magnitude
|
|
261
|
+
while power >= 0
|
|
262
|
+
output << (0x30 + magnitude / POWERS[power])
|
|
263
|
+
magnitude %= POWERS[power]
|
|
264
|
+
power -= 1
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# §4.1.5 Serializing a Decimal: the digits AS WRITTEN — the
|
|
269
|
+
# shortest round-trip decimal Float#to_s spells (the corpus's tie
|
|
270
|
+
# records pin this reading over the binary double's rounding:
|
|
271
|
+
# 0.5015 writes "0.502") — rounded to three fraction places, ties
|
|
272
|
+
# to even; at most 12 integer digits after the rounding; the
|
|
273
|
+
# fraction keeps its leading zeros and drops trailing zeros, "0"
|
|
274
|
+
# when it is zero. The one unavoidable temporary is the Float#to_s
|
|
275
|
+
# text the digits are read from; the scan accumulates the mantissa
|
|
276
|
+
# in place, exponent form included.
|
|
277
|
+
def emit_decimal(output, value)
|
|
278
|
+
fail_serialize(output, "a decimal must be finite") unless value.finite?
|
|
279
|
+
|
|
280
|
+
text = value.to_s
|
|
281
|
+
size = text.bytesize
|
|
282
|
+
index = 0
|
|
283
|
+
negative = text.getbyte(0) == HYPHEN
|
|
284
|
+
index += 1 if negative
|
|
285
|
+
mantissa = 0
|
|
286
|
+
while index < size && DIGIT[text.getbyte(index)]
|
|
287
|
+
mantissa = mantissa * 10 + (text.getbyte(index) - 0x30)
|
|
288
|
+
index += 1
|
|
289
|
+
end
|
|
290
|
+
fraction = 0
|
|
291
|
+
if text.getbyte(index) == PERIOD
|
|
292
|
+
index += 1
|
|
293
|
+
while index < size && DIGIT[text.getbyte(index)]
|
|
294
|
+
mantissa = mantissa * 10 + (text.getbyte(index) - 0x30)
|
|
295
|
+
fraction += 1
|
|
296
|
+
index += 1
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
exponent = 0
|
|
300
|
+
exponent = exponent_of(text, index + 1) if text.getbyte(index) == EXPONENT
|
|
301
|
+
|
|
302
|
+
scale = fraction - exponent
|
|
303
|
+
mantissa = rounded_mantissa(mantissa, scale)
|
|
304
|
+
scale = 3 if scale > 3
|
|
305
|
+
|
|
306
|
+
# §4.1.5 step 3: at most 12 significant digits left of the point,
|
|
307
|
+
# after the rounding — a carry may push the count over.
|
|
308
|
+
integer_digits = digit_count(mantissa) - scale
|
|
309
|
+
integer_digits = 1 if integer_digits < 1
|
|
310
|
+
fail_serialize(output, "a decimal is at most 12 integer digits") if integer_digits > 12
|
|
311
|
+
|
|
312
|
+
emit_decimal_components(output, negative, mantissa, scale)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# The exponent of a Float#to_s text whose "e" sits at +index+ —
|
|
316
|
+
# the sign, then the DIGITs; the text always ends there.
|
|
317
|
+
def exponent_of(text, index)
|
|
318
|
+
exponent = 0
|
|
319
|
+
negative = text.getbyte(index) == HYPHEN
|
|
320
|
+
index += 1 if negative || text.getbyte(index) == PLUS
|
|
321
|
+
while index < text.bytesize && DIGIT[text.getbyte(index)]
|
|
322
|
+
exponent = exponent * 10 + (text.getbyte(index) - 0x30)
|
|
323
|
+
index += 1
|
|
324
|
+
end
|
|
325
|
+
negative ? -exponent : exponent
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# The mantissa rounded to three fraction places, ties to even on
|
|
329
|
+
# the as-written digits (§4.1.5 step 2): the digits past the third
|
|
330
|
+
# fraction place are dropped, a remainder past half carries, an
|
|
331
|
+
# exact half carries only onto an even digit. A scale past 21
|
|
332
|
+
# rounds to zero — the factor is past 10^18, and a mantissa of at
|
|
333
|
+
# most 18 digits is under half of it.
|
|
334
|
+
def rounded_mantissa(mantissa, scale)
|
|
335
|
+
return 0 if scale > 21
|
|
336
|
+
return mantissa if scale <= 3
|
|
337
|
+
|
|
338
|
+
factor = 10**(scale - 3)
|
|
339
|
+
rounded = mantissa / factor
|
|
340
|
+
remainder = mantissa - rounded * factor
|
|
341
|
+
half = factor / 2
|
|
342
|
+
rounded += 1 if remainder > half || (remainder == half && rounded.odd?)
|
|
343
|
+
rounded
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# §4.1.5 steps 5-9: the sign of the rounded value, the integer
|
|
347
|
+
# component ("0" at zero), the point, then the fraction — leading
|
|
348
|
+
# zeros kept, trailing zeros dropped, "0" when it is zero.
|
|
349
|
+
def emit_decimal_components(output, negative, mantissa, scale)
|
|
350
|
+
output << "-" if negative && mantissa.positive?
|
|
351
|
+
if scale.negative?
|
|
352
|
+
emit_digits(output, mantissa)
|
|
353
|
+
zeros = -scale
|
|
354
|
+
while zeros.positive?
|
|
355
|
+
output << "0"
|
|
356
|
+
zeros -= 1
|
|
357
|
+
end
|
|
358
|
+
else
|
|
359
|
+
emit_digits(output, mantissa / POWERS[scale])
|
|
360
|
+
end
|
|
361
|
+
output << "."
|
|
362
|
+
fraction = scale.negative? ? 0 : mantissa % POWERS[scale]
|
|
363
|
+
if fraction.zero?
|
|
364
|
+
output << "0"
|
|
365
|
+
else
|
|
366
|
+
places = scale
|
|
367
|
+
places -= 1 while places.positive? && (fraction % 10).zero?
|
|
368
|
+
power = places - 1
|
|
369
|
+
while power >= 0
|
|
370
|
+
output << (0x30 + fraction / POWERS[power] % 10)
|
|
371
|
+
power -= 1
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
# §4.1.6 Serializing a String: DQUOTE-delimited, a backslash
|
|
377
|
+
# escapes only DQUOTE and backslash, every byte SP or VCHAR —
|
|
378
|
+
# 0x20-0x7e. The content is appended whole when nothing needs
|
|
379
|
+
# escaping, in runs between escapes when something does.
|
|
380
|
+
def emit_string(output, value)
|
|
381
|
+
output << "\""
|
|
382
|
+
size = value.bytesize
|
|
383
|
+
start = 0
|
|
384
|
+
escaped = false
|
|
385
|
+
index = 0
|
|
386
|
+
while index < size
|
|
387
|
+
byte = value.getbyte(index)
|
|
388
|
+
if STRING_ESCAPE[byte]
|
|
389
|
+
output << value.byteslice(start, index - start) if index > start
|
|
390
|
+
output << "\\" << byte
|
|
391
|
+
start = index + 1
|
|
392
|
+
escaped = true
|
|
393
|
+
elsif byte < SP || byte > 0x7E
|
|
394
|
+
fail_serialize(output, "a string may only carry SP and printable ASCII")
|
|
395
|
+
end
|
|
396
|
+
index += 1
|
|
397
|
+
end
|
|
398
|
+
output << (escaped ? value.byteslice(start, size - start) : value)
|
|
399
|
+
output << "\""
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
# §4.1.7 Serializing a Token: ALPHA or "*" first, the continuation
|
|
403
|
+
# TOKEN_BODY — validated on the Token's own bytes, then appended
|
|
404
|
+
# whole: the constructor already guaranteed this grammar at the
|
|
405
|
+
# boundary, so the walk is a strict sender's defensive re-check.
|
|
406
|
+
def emit_token(output, value)
|
|
407
|
+
size = value.bytesize
|
|
408
|
+
fail_serialize(output, "a token must not be empty") if size.zero?
|
|
409
|
+
fail_serialize(output, "a token must start with ALPHA or '*'") unless TOKEN_FIRST[value.getbyte(0)]
|
|
410
|
+
|
|
411
|
+
index = 1
|
|
412
|
+
while index < size
|
|
413
|
+
fail_serialize(output, "a token may only carry tchar, ':' and '/'") unless TOKEN_BODY[value.getbyte(index)]
|
|
414
|
+
|
|
415
|
+
index += 1
|
|
416
|
+
end
|
|
417
|
+
output << value
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# §4.1.8 Serializing a Byte Sequence: ":" + the RFC 4648 §4 base64
|
|
421
|
+
# of the bytes, padded with "=" (the pad bits are zero by
|
|
422
|
+
# construction, the shifts below fill with zeros) + ":". Three
|
|
423
|
+
# bytes spell four alphabet bytes, the 2-byte tail spells three and
|
|
424
|
+
# one pad, the 1-byte tail two and two.
|
|
425
|
+
def emit_byte_sequence(output, value)
|
|
426
|
+
output << ":"
|
|
427
|
+
size = value.bytesize
|
|
428
|
+
index = 0
|
|
429
|
+
while index + 3 <= size
|
|
430
|
+
group = (value.getbyte(index) << 16) | (value.getbyte(index + 1) << 8) | value.getbyte(index + 2)
|
|
431
|
+
output << BASE64.getbyte(group >> 18) << BASE64.getbyte((group >> 12) & 0x3F)
|
|
432
|
+
output << BASE64.getbyte((group >> 6) & 0x3F) << BASE64.getbyte(group & 0x3F)
|
|
433
|
+
index += 3
|
|
434
|
+
end
|
|
435
|
+
remaining = size - index
|
|
436
|
+
if remaining == 2
|
|
437
|
+
group = (value.getbyte(index) << 16) | (value.getbyte(index + 1) << 8)
|
|
438
|
+
output << BASE64.getbyte(group >> 18) << BASE64.getbyte((group >> 12) & 0x3F)
|
|
439
|
+
output << BASE64.getbyte((group >> 6) & 0x3F) << "="
|
|
440
|
+
elsif remaining == 1
|
|
441
|
+
group = value.getbyte(index) << 16
|
|
442
|
+
output << BASE64.getbyte(group >> 18) << BASE64.getbyte((group >> 12) & 0x3F) << "=="
|
|
443
|
+
end
|
|
444
|
+
output << ":"
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# §4.1.10 Serializing a Date: "@" + the integer form of #value —
|
|
448
|
+
# the Integer range applies; @0 at zero.
|
|
449
|
+
def emit_date(output, value)
|
|
450
|
+
output << "@"
|
|
451
|
+
number = value.value
|
|
452
|
+
fail_serialize(output, "a date's value must be an Integer (got #{shape(number)})") unless number.is_a?(Integer)
|
|
453
|
+
|
|
454
|
+
emit_integer(output, number)
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
# §4.1.11 Serializing a Display String: #value UTF-8-encoded (a
|
|
458
|
+
# String that cannot be read as Unicode text fails), then `%"`,
|
|
459
|
+
# per byte: "%", DQUOTE and the control/high bytes as "%" plus two
|
|
460
|
+
# lowercase hex digits, every other byte literal, then `"`. The
|
|
461
|
+
# literal runs are appended whole when nothing needs escaping.
|
|
462
|
+
def emit_display_string(output, value)
|
|
463
|
+
text = display_text(output, value)
|
|
464
|
+
output << "%\""
|
|
465
|
+
size = text.bytesize
|
|
466
|
+
start = 0
|
|
467
|
+
escaped = false
|
|
468
|
+
index = 0
|
|
469
|
+
while index < size
|
|
470
|
+
byte = text.getbyte(index)
|
|
471
|
+
if byte == PERCENT || byte == DQUOTE || byte < SP || byte > 0x7E
|
|
472
|
+
output << text.byteslice(start, index - start) if index > start
|
|
473
|
+
output << "%" << HEX.getbyte(byte >> 4) << HEX.getbyte(byte & 0xF)
|
|
474
|
+
start = index + 1
|
|
475
|
+
escaped = true
|
|
476
|
+
end
|
|
477
|
+
index += 1
|
|
478
|
+
end
|
|
479
|
+
output << (escaped ? text.byteslice(start, size - start) : text)
|
|
480
|
+
output << "\""
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
# The display string's value as UTF-8 text — encoded when it
|
|
484
|
+
# arrives in another encoding, failed loudly when it cannot be.
|
|
485
|
+
def display_text(output, value)
|
|
486
|
+
text = value.value
|
|
487
|
+
unless text.is_a?(String)
|
|
488
|
+
fail_serialize(output, "a display string's value must be a String (got #{shape(text)})")
|
|
489
|
+
end
|
|
490
|
+
if text.encoding != Encoding::UTF_8
|
|
491
|
+
begin
|
|
492
|
+
text = text.encode(Encoding::UTF_8)
|
|
493
|
+
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError, Encoding::ConverterNotFoundError
|
|
494
|
+
fail_serialize(output, "a display string must be Unicode text")
|
|
495
|
+
end
|
|
496
|
+
end
|
|
497
|
+
fail_serialize(output, "a display string must be Unicode text") unless text.valid_encoding?
|
|
498
|
+
|
|
499
|
+
text
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
# The number of base-10 digits of +magnitude+ (1 at zero): the
|
|
503
|
+
# POWERS place it reaches, bounded by the 18 a Float#to_s mantissa
|
|
504
|
+
# can never pass.
|
|
505
|
+
def digit_count(magnitude)
|
|
506
|
+
count = 1
|
|
507
|
+
count += 1 while count < 19 && POWERS[count] <= magnitude
|
|
508
|
+
count
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
# The class of +value+ for an error message, or a placeholder when
|
|
512
|
+
# the value has none (a BasicObject descendant).
|
|
513
|
+
def shape(value)
|
|
514
|
+
defined?(value.class) ? value.class : "a classless object"
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
# Raises the positioned SerializeError: the reason, and the byte
|
|
518
|
+
# offset into the output built so far where the emit stopped.
|
|
519
|
+
def fail_serialize(output, reason)
|
|
520
|
+
offset = output.bytesize
|
|
521
|
+
raise SerializeError.new("#{reason} at offset #{offset}", offset)
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
# Raises at offset 0: the top-level input shape is wrong before
|
|
525
|
+
# anything was emitted.
|
|
526
|
+
def fail_shape(expected, got)
|
|
527
|
+
raise SerializeError.new("#{expected} is required (got #{shape(got)})", 0)
|
|
528
|
+
end
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
private_constant :Serializer
|
|
532
|
+
|
|
533
|
+
module_function
|
|
534
|
+
|
|
535
|
+
# Serializes one RFC 9651 Item (§4.1, field_type "item") — the
|
|
536
|
+
# canonical §4.1.3 form, the bare item then its parameters, as a
|
|
537
|
+
# fresh US-ASCII String the caller owns.
|
|
538
|
+
#
|
|
539
|
+
# Strict: a non-Item input, or any non-serializable value inside it,
|
|
540
|
+
# raises SerializeError with the byte offset into the output built
|
|
541
|
+
# so far. There is no coercion and no content inference: a String
|
|
542
|
+
# value is an sf-string whatever it spells, a Fast::SFV::Token a
|
|
543
|
+
# token (a Symbol is wrong-typed input and raises), an ASCII-8BIT
|
|
544
|
+
# String a byte sequence.
|
|
545
|
+
def serialize_item(item)
|
|
546
|
+
Serializer.fail_shape("an Item", item) unless item.is_a?(Item)
|
|
547
|
+
|
|
548
|
+
Serializer.item_string(item)
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
# Serializes an RFC 9651 List (§4.1, field_type "list") — the
|
|
552
|
+
# canonical §4.1.1 form of an Array of Items and InnerLists, as a
|
|
553
|
+
# fresh US-ASCII String, or nil for the empty List (§4.1 step 1: an
|
|
554
|
+
# empty container omits the field entirely).
|
|
555
|
+
#
|
|
556
|
+
# Strict: a non-Array input or an unwrapped member raises
|
|
557
|
+
# SerializeError; an Item is never empty, so only the containers
|
|
558
|
+
# answer nil.
|
|
559
|
+
def serialize_list(list)
|
|
560
|
+
Serializer.fail_shape("an Array of Items and InnerLists", list) unless list.is_a?(Array)
|
|
561
|
+
return nil if list.empty?
|
|
562
|
+
|
|
563
|
+
Serializer.list_string(list)
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
# Serializes an RFC 9651 Dictionary (§4.1, field_type "dictionary") —
|
|
567
|
+
# the canonical §4.1.2 form of an ordered Hash of String keys to
|
|
568
|
+
# Items and InnerLists, as a fresh US-ASCII String, or nil for the
|
|
569
|
+
# empty Dictionary (the omitted field). A member whose Item value is
|
|
570
|
+
# Boolean true uses the valueless short form.
|
|
571
|
+
#
|
|
572
|
+
# Strict: a non-Hash input, a Symbol key or an unwrapped value
|
|
573
|
+
# raises SerializeError.
|
|
574
|
+
def serialize_dict(dict)
|
|
575
|
+
Serializer.fail_shape("a Hash of String keys to Items and InnerLists", dict) unless dict.is_a?(Hash)
|
|
576
|
+
return nil if dict.empty?
|
|
577
|
+
|
|
578
|
+
Serializer.dictionary_string(dict)
|
|
579
|
+
end
|
|
580
|
+
end
|
|
581
|
+
end
|