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

+10 -5
View File
@@ -41,6 +41,15 @@ local function crc16(b: buffer, from: number, to: number): number
end
Frame.crc16 = crc16
local function crc8(b: buffer, from: number, to: number): number
local c = 0
for i = from, to - 1 do
c = CRC8[bxor(c, readu8(b, i)) + 1]
end
return c
end
Frame.crc8 = crc8
local POW2 = table.create(64, 0)
for i = 0, 63 do
POW2[i + 1] = 2 ^ i
@@ -147,11 +156,7 @@ function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
return nil
end
-- CRC-8 over the header
local c = 0
for i = pos, p - 1 do
c = CRC8[bxor(c, readu8(b, i)) + 1]
end
if c ~= readu8(b, p) then
if crc8(b, pos, p) ~= readu8(b, p) then
return nil
end
return {