ws-cleanup: shared helpers instead of copies (CRCs, codec arguments, utf8char, keyLess / isArray)

A sweep for duplicated function bodies, CRC tables and base64 across src/Formats:
- core/Crc32.msb / MSB_TABLE: the MSB-first 0x04C11DB7 CRC-32 that containers/Ogg (Page + Mux), containers/MpegTs
  and codecs/Bzip2 each built (test vectors CRC-32/MPEG-2 and /BZIP2 in tests/core/bytes).
- audio/flac/Encode uses audio/flac/Frame's CRC-8 / CRC-16 tables (Frame.crc8 added) instead of its own copy.
- core/Bytes.toBuffer / Bytes.span: the (buffer | string, offset, length) argument checking codecs/Deflate,
  codecs/Lzma and codecs/Brotli each carried.
- codecs/Text.utf8char: one copy instead of four (codecs/Json, codecs/Xml, data/yaml, data/toml).
- Types.keyLess / Types.isArray (small additions to Types, backward compatible): data/msgpack, data/cbor and
  data/yaml/Emit had identical copies.
- playlist/pls: dead copies of m3u's attributes / extinf removed.
Left on purpose: per-codec bit readers / writers in hot loops (STYLE.md: inner loops keep their own bit buffers),
same-folder helpers (Alac signExtend, Vorbis ilog), Zip's DOS-date civil() (a different epoch helper).
This commit is contained in:
4DBug committed 2026-10-31 10:58:31 -05:00
1 parent 7169f2cc2c
commit f784b4eb9b
22 files changed
+152 -311

No files matched your search

+2 -24
View File
@@ -453,31 +453,9 @@ local function toHalf(x: number): number?
end
Codec.toHalf = toHalf
local function keyLess(a: any, b: any): boolean
local ta, tb = type(a), type(b)
if ta ~= tb then
return ta < tb
end
if ta == "number" or ta == "string" then
return a < b
end
return tostring(a) < tostring(b)
end
local keyLess = Types.keyLess
local function isArray(t: any, emptyMap: boolean): boolean
if Types.isObject(t) then
return false
end
local n = #t
if n == 0 then
return next(t) == nil and not emptyMap
end
local c = 0
for _ in next, t do
c += 1
end
return c == n
end
local isArray = Types.isArray
local scratch = buffer.create(8)
+2 -24
View File
@@ -322,31 +322,9 @@ local function lenHeader(w: Bytes.Writer, n: number, fix: number?, fixMax: numbe
end
end
local function keyLess(a: any, b: any): boolean
local ta, tb = type(a), type(b)
if ta ~= tb then
return ta < tb
end
if ta == "number" or ta == "string" then
return a < b
end
return tostring(a) < tostring(b)
end
local keyLess = Types.keyLess
local function isArray(t: any, emptyMap: boolean): boolean
if Types.isObject(t) then
return false
end
local n = #t
if n == 0 then
return next(t) == nil and not emptyMap
end
local c = 0
for _ in next, t do
c += 1
end
return c == n
end
local isArray = Types.isArray
Codec.isArray = isArray
local function enc(st: ESt, v: any, depth: number): boolean
+2 -10
View File
@@ -3,6 +3,7 @@
-- their key order; arrays are arrays; datetimes are Types.Date values. Checks the 1.0 validity rules: UTF-8, control
-- characters, table / key redefinition (explicit, implicit, dotted, inline and array-of-tables), number grammar and
-- 64-bit range, escapes, datetimes. Never throws: failures return nil, message, offset (0-based).
local Text = require("@formats/codecs/Text")
local Types = require("@formats/Types")
local Decode = {}
@@ -13,16 +14,7 @@ export type Options = {
bigInts: ("number" | "string")?, -- integers beyond +-2^53: "string" keeps their digits (default "number")
}
local function utf8char(c: number): string
if c < 0x80 then
return char(c)
elseif c < 0x800 then
return char(0xC0 + c // 64, 0x80 + c % 64)
elseif c < 0x10000 then
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
local utf8char = Text.utf8char
local ESC: { [number]: string } = { [98] = "\b", [116] = "\t", [110] = "\n", [102] = "\f", [114] = "\r", [34] = '"', [92] = "\\" }
+1 -1
View File
@@ -19,7 +19,7 @@ local Toml = {
category = "data",
extensions = { "toml" },
mime = { "application/toml" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "codecs/Text" },
caps = { decode = true, encode = true, ordered = true, dates = true },
}
+2 -27
View File
@@ -120,34 +120,9 @@ local function str(s: string, ind: string, key: boolean): string
return dquote(s)
end
local function isArray(t: { [any]: any }): boolean
if Types.isObject(t) then
return false
end
local n = #t
if n == 0 then
return next(t) == nil
end
if next(t, n) ~= nil then
return false
end
local c = 0
for _ in next, t do
c += 1
end
return c == n
end
local isArray = Types.isArray
local function keyLess(a: any, b: any): boolean
local ta, tb = type(a), type(b)
if ta ~= tb then
return ta < tb
end
if ta == "number" or ta == "string" then
return a < b
end
return tostring(a) < tostring(b)
end
local keyLess = Types.keyLess
type St = { out: { string }, step: number, sort: boolean, null: any, refs: { [any]: number }, anchors: { [any]: string }, nextId: number, err: string? }
+2 -10
View File
@@ -6,6 +6,7 @@
-- opts.tags = "ignore"), %YAML / %TAG directives, multi-document streams, comments. Scalars resolve with the core
-- schema. Not supported: complex keys ("? "), collections as keys, recursive aliases, merge keys "<<" (kept as a
-- normal key). Internally raises { yamlErr } tables caught by run(); never throws past it.
local Text = require("@formats/codecs/Text")
local Types = require("@formats/Types")
local Base64 = require("@formats/core/Base64")
@@ -116,16 +117,7 @@ local function endOfLine(s: string, p: number): boolean
return c == nil or c == 10 or (c == 35 and q > p)
end
local function utf8char(c: number): string
if c < 0x80 then
return char(c)
elseif c < 0x800 then
return char(0xC0 + c // 64, 0x80 + c % 64)
elseif c < 0x10000 then
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
local utf8char = Text.utf8char
-- ------------------------------------------------------------------------------------------------ resolution
-- the core schema: a plain scalar's value
+1 -1
View File
@@ -20,7 +20,7 @@ local Yaml = {
category = "data",
extensions = { "yaml", "yml" },
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
deps = { "core/Err", "core/Bytes", "Types", "core/Base64" },
deps = { "core/Err", "core/Bytes", "Types", "core/Base64", "codecs/Text" },
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
}