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). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
6b68c2ffab
commit
8158acf3d5
22 files changed
+152
-311
No files matched your search
@@ -17,6 +17,7 @@
|
||||
-- none / CRC-32 / CRC-64 are verified, SHA-256 is skipped (not verified). xz filters: LZMA2, delta, x86, PowerPC,
|
||||
-- ARM, ARM-Thumb, SPARC, ARM64; IA-64 and RISC-V are Err.UNSUPPORTED. Never throws.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Decode = require("@formats/codecs/Lzma/Decode")
|
||||
local Filters = require("@formats/codecs/Lzma/Filters")
|
||||
@@ -33,43 +34,8 @@ export type Options = {
|
||||
}
|
||||
export type Props = { lc: number, lp: number, pb: number, dictSize: number? }
|
||||
|
||||
local function toBuffer(b: any): buffer?
|
||||
if type(b) == "string" then
|
||||
return buffer.fromstring(b)
|
||||
end
|
||||
if type(b) ~= "buffer" then
|
||||
return nil
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
local function range(b: buffer, off: number?, len: number?): (number?, number)
|
||||
local n = buffer.len(b)
|
||||
local o = off or 0
|
||||
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
|
||||
return nil, 0
|
||||
end
|
||||
local l = len or (n - o)
|
||||
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
|
||||
return nil, 0
|
||||
end
|
||||
if o + l > n then
|
||||
l = n - o
|
||||
end
|
||||
return o, l
|
||||
end
|
||||
|
||||
local function args(what: string, buf: any, offset: number?, length: number?): (buffer?, number, number, Err.Err?)
|
||||
local b = toBuffer(buf)
|
||||
if not b then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": expected a buffer or a string, got " .. typeof(buf))
|
||||
end
|
||||
local o, l = range(b, offset, length)
|
||||
if not o then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": bad offset / length " .. tostring(offset) .. ", " .. tostring(length))
|
||||
end
|
||||
return b, o, l, nil
|
||||
end
|
||||
local toBuffer = Bytes.toBuffer
|
||||
local args = Bytes.span
|
||||
|
||||
local function maxOf(opts: Options?): number
|
||||
local m = opts and opts.maxBytes
|
||||
|
||||
Reference in new issue
Block a user