data formats: comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
0452f26eef
18 files changed
+110
-224
No files matched your search
@@ -1,7 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- data/cbor/Codec: the CBOR (RFC 8949) decoder and encoder behind data/cbor. Never throws: errors return nil,
|
||||
-- message, offset.
|
||||
-- data/cbor/Codec: CBOR (RFC 8949) decoder and encoder; never throws (errors return nil, message, offset).
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
@@ -16,14 +15,13 @@ end }
|
||||
local SimpleMeta = { __tostring = function(s: any): string
|
||||
return "cbor.simple(" .. s.value .. ")"
|
||||
end }
|
||||
-- tagged(tag, value) -> { tag, value }: a tag this library does not interpret
|
||||
function Codec.tagged(tag: number, value: any): { tag: number, value: any }
|
||||
return setmetatable({ tag = tag, value = value }, TaggedMeta) :: any
|
||||
end
|
||||
function Codec.isTagged(v: any): boolean
|
||||
return type(v) == "table" and getmetatable(v) == TaggedMeta
|
||||
end
|
||||
-- simple(n) -> an unassigned simple value (0..19, 32..255)
|
||||
-- unassigned simple values: 0..19, 32..255
|
||||
function Codec.simple(n: number): { value: number }
|
||||
return setmetatable({ value = n }, SimpleMeta) :: any
|
||||
end
|
||||
@@ -34,7 +32,6 @@ Codec.undefined = table.freeze(setmetatable({}, { __tostring = function()
|
||||
return "undefined"
|
||||
end }))
|
||||
|
||||
-- decimal text of a big-endian unsigned byte string (any length)
|
||||
local function bigText(s: string): string
|
||||
local limbs = {} -- base 1e7, little-endian
|
||||
for i = 1, #s do
|
||||
@@ -58,7 +55,6 @@ local function bigText(s: string): string
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
-- text + 1 for a decimal string
|
||||
local function incText(s: string): string
|
||||
local digits: { number } = table.create(#s) :: { number }
|
||||
for i = 1, #s do
|
||||
@@ -81,18 +77,18 @@ local function incText(s: string): string
|
||||
end
|
||||
|
||||
export type DecodeOptions = {
|
||||
bigInts: ("number" | "string")?, -- integers / bignums past 2^53: "string" keeps exact decimal text
|
||||
null: any?, -- the value for null (default Types.null)
|
||||
undefined: any?, -- the value for undefined (default Codec.undefined)
|
||||
bigInts: ("number" | "string")?,
|
||||
null: any?, -- default Types.null
|
||||
undefined: any?, -- default Codec.undefined
|
||||
maxDepth: number?, -- default 512
|
||||
dates: boolean?, -- tags 0 / 1 as Types.Date (default true)
|
||||
dates: boolean?, -- default true
|
||||
}
|
||||
|
||||
type St = { r: Bytes.Reader, big: boolean, null: any, undef: any, maxDepth: number, dates: boolean }
|
||||
|
||||
local BREAK = {} -- the 0xFF stop code, inside indefinite items
|
||||
local BREAK = {} -- 0xFF break stop code
|
||||
|
||||
-- the argument of a head: number (or nil, "text" for u64 past 2^53 when big), nil means indefinite (ai 31)
|
||||
-- additional information 31: indefinite length
|
||||
local function arg(st: St, ai: number): (number?, string?, string?)
|
||||
local r = st.r
|
||||
if ai < 24 then
|
||||
@@ -169,7 +165,7 @@ local function tag(st: St, t: number, v: any): (any, string?)
|
||||
n = n * 256 + string.byte(s, i)
|
||||
end
|
||||
if n >= 9007199254740992 then
|
||||
-- exact text; as a number, read back from that text (correctly rounded, unlike the running sum)
|
||||
-- read back from the exact text: correctly rounded, unlike the running sum
|
||||
local text = bigText(s)
|
||||
if t == 3 then
|
||||
text = incText(text)
|
||||
@@ -347,7 +343,6 @@ function item(st: St, depth: number): (any, string?)
|
||||
if v == BREAK then
|
||||
return nil, "unexpected break"
|
||||
end
|
||||
-- bignum content arrives as a buffer: hand the tag its bytes
|
||||
if (n == 2 or n == 3) and type(v) == "buffer" then
|
||||
return tag(st, n, buffer.tostring(v))
|
||||
end
|
||||
@@ -355,7 +350,6 @@ function item(st: St, depth: number): (any, string?)
|
||||
end
|
||||
end
|
||||
|
||||
-- decode(r, opts) -> value | nil, message, offset (one item; r is left after it)
|
||||
function Codec.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, string?, number?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local st: St = {
|
||||
@@ -378,10 +372,10 @@ end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
export type EncodeOptions = {
|
||||
compactFloats: boolean?, -- floats in the shortest of half / single / double that keeps the value (default false)
|
||||
sortKeys: boolean?, -- plain tables' keys sorted (default true; Types.object keeps its order)
|
||||
emptyTable: ("array" | "map")?, -- a plain empty table (default "array")
|
||||
dates: ("string" | "epoch")?, -- Types.Date as tag 0 text (default) or tag 1 seconds
|
||||
compactFloats: boolean?, -- default false
|
||||
sortKeys: boolean?, -- default true
|
||||
emptyTable: ("array" | "map")?, -- default "array"
|
||||
dates: ("string" | "epoch")?, -- tag 0 text (default) or tag 1 epoch seconds
|
||||
maxDepth: number?, -- default 512 (also catches cycles)
|
||||
}
|
||||
|
||||
@@ -410,7 +404,6 @@ local function head(w: Bytes.Writer, major: number, n: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- the half-float bits of x when x is exactly representable, else nil
|
||||
local function toHalf(x: number): number?
|
||||
if x ~= x then
|
||||
return 0x7E00
|
||||
@@ -423,7 +416,7 @@ local function toHalf(x: number): number?
|
||||
if a == 0 then
|
||||
return sign
|
||||
end
|
||||
local m, e = math.frexp(a) -- a = m * 2^e, 0.5 <= m < 1
|
||||
local m, e = math.frexp(a)
|
||||
local exp = e - 1 + 15
|
||||
if exp >= 31 then
|
||||
return nil
|
||||
@@ -554,7 +547,6 @@ local function enc(st: ESt, v: any, depth: number): boolean
|
||||
return true
|
||||
end
|
||||
|
||||
-- encode(v, opts) -> buffer | nil, message
|
||||
function Codec.encode(v: any, opts: EncodeOptions?): (buffer?, string?)
|
||||
local o: EncodeOptions = opts or {}
|
||||
local st: ESt = {
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
--!strict
|
||||
-- data/cbor: CBOR (RFC 8949). New code. Supports: every major type, indefinite-length strings / arrays / maps, half /
|
||||
-- single / double floats, tags (0 / 1 date-times as Types.Date, 2 / 3 bignums as numbers or exact decimal text,
|
||||
-- 55799 self-describe stripped, others kept as Cbor.tagged(tag, value)), simple values (Cbor.simple(n),
|
||||
-- Cbor.undefined), byte strings as buffers, maps as Types.object (key order kept; keys of any type), sequences
|
||||
-- (RFC 8742, opts.all). Encode: preferred (shortest) integer and length heads, float64 unless opts.compactFloats,
|
||||
-- definite lengths. Not supported: other semantic tags (decimal fractions, bigfloats, ...) stay tagged values.
|
||||
-- data/cbor: CBOR (RFC 8949), new code. Supports: every major type, indefinite lengths, half / single / double
|
||||
-- floats, tags 0 / 1 (Types.Date), 2 / 3 (bignums), 55799 (stripped), others as Cbor.tagged; simple values; sequences
|
||||
-- (RFC 8742). Encode: shortest heads, definite lengths. Not supported: other semantic tags (kept as tagged values).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -22,11 +19,11 @@ local Cbor = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = Codec.DecodeOptions & {
|
||||
all: boolean?, -- a CBOR sequence: every item into an array (default false: exactly one item)
|
||||
all: boolean?, -- default false
|
||||
}
|
||||
export type EncodeOptions = Codec.EncodeOptions & {
|
||||
all: boolean?, -- the value is an array of items, written as a sequence
|
||||
selfDescribe: boolean?, -- prefix the 55799 self-describe tag (d9 d9 f7)
|
||||
all: boolean?,
|
||||
selfDescribe: boolean?, -- 55799 tag prefix (d9 d9 f7)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -91,9 +88,9 @@ function Cbor.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
Cbor.tagged = Codec.tagged -- tagged(tag, value) -> a value written as that tag
|
||||
Cbor.tagged = Codec.tagged
|
||||
Cbor.isTagged = Codec.isTagged
|
||||
Cbor.simple = Codec.simple -- simple(n) -> a simple value
|
||||
Cbor.simple = Codec.simple
|
||||
Cbor.isSimple = Codec.isSimple
|
||||
Cbor.undefined = Codec.undefined
|
||||
|
||||
|
||||
Reference in new issue
Block a user