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
@@ -73,56 +73,6 @@ local function duration(s: string?): number?
|
||||
return d
|
||||
end
|
||||
|
||||
-- RFC 8216 attribute list: KEY=value pairs, quoted strings may hold commas
|
||||
local function attributes(s: string): { [string]: string }
|
||||
local out = {}
|
||||
local i, n = 1, #s
|
||||
while i <= n do
|
||||
local key, eq = string.match(s, "^%s*([%w%-_]+)%s*()=", i)
|
||||
if not key then
|
||||
break
|
||||
end
|
||||
i = (eq :: number) + 1
|
||||
local value
|
||||
if string.sub(s, i, i) == '"' then
|
||||
local j = string.find(s, '"', i + 1, true) or (n + 1)
|
||||
value = string.sub(s, i + 1, j - 1)
|
||||
i = j + 1
|
||||
else
|
||||
local j = string.find(s, ",", i, true) or (n + 1)
|
||||
value = Text.trim(string.sub(s, i, j - 1))
|
||||
i = j
|
||||
end
|
||||
out[string.upper(key)] = value
|
||||
local c = string.find(s, ",", i, true)
|
||||
if not c then
|
||||
break
|
||||
end
|
||||
i = c + 1
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- #EXTINF:<duration>[ attr="v" ...],<title>
|
||||
local function extinf(rest: string): (number?, string?)
|
||||
-- the first comma outside quoted attribute values
|
||||
local comma, quoted = nil, false
|
||||
for i = 1, #rest do
|
||||
local c = string.byte(rest, i)
|
||||
if c == 34 then
|
||||
quoted = not quoted
|
||||
elseif c == 44 and not quoted then
|
||||
comma = i
|
||||
break
|
||||
end
|
||||
end
|
||||
local head = if comma then string.sub(rest, 1, comma - 1) else rest
|
||||
local title = if comma then Text.trim(string.sub(rest, comma + 1)) else nil
|
||||
local d = string.match(head, "^%s*([%-%+]?[%d%.]+)")
|
||||
return duration(d), if title and title ~= "" then title else nil
|
||||
end
|
||||
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Pls.sniff(r: Bytes.Reader): number
|
||||
local s = r:peek(64)
|
||||
|
||||
Reference in new issue
Block a user