ws-cleanup: shared plumbing: Err.from, core/Sniff, Pcm.drain, Bits.msbPut; DUP lint; tools/move.py

- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix /
  'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and
  bmfont each wrote.
- core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s),
  Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it.
- codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder
  loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each.
- core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied.
- tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails.
- tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs).
- tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is
  now under 14.3 MB).
- tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default
  limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
This commit is contained in:
4DBug committed 2026-10-31 12:00:59 -05:00
1 parent 3243404374
commit 763f4a7c04
44 files changed
+374 -370

No files matched your search

+23
View File
@@ -12,6 +12,8 @@
-- local br = Bits.lsbReader(buf, offset?, length?) -- or Bits.msbReader
-- local v = br:read(5); if br.err then return nil, br.err end
-- local bw = Bits.msbWriter(); bw:write(0b101, 3); local out = bw:finish()
-- local w = { buf = buffer.create(n), pos = 0, acc = 0, cnt = 0 }; Bits.msbPut(w, v, nbits) -- encoders' fast path
-- (a plain record the caller sizes / grows itself: MP3, MP2 and FLAC frames)
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
@@ -276,6 +278,27 @@ function MsbW.finish(self: any): buffer
return self.out:finish()
end
-- msbPut(w, v, n): append the low n bits (n <= 32) of the non-negative integer v to the record w = { buf, pos, acc,
-- cnt }, MSB first. No growth: the caller keeps buf large enough (whole bytes go to buf[pos]; acc holds cnt < 8 bits).
export type RawWriter = { buf: buffer, pos: number, acc: number, cnt: number }
function Bits.msbPut(w: RawWriter, v: number, n: number)
if n == 0 then
return
end
local acc = w.acc * POW[n + 1] + v
local cnt = w.cnt + n
local b, p = w.buf, w.pos
while cnt >= 8 do
cnt -= 8
local m = POW[cnt + 1]
local byte = floor(acc / m)
buffer.writeu8(b, p, byte)
p += 1
acc -= byte * m
end
w.acc, w.cnt, w.pos = acc, cnt, p
end
-- lsbWriter(capacity?) / msbWriter(capacity?) -> BitWriter
function Bits.lsbWriter(capacity: number?): BitWriter
return setmetatable({ out = Bytes.writer(capacity), acc = 0, cnt = 0 }, LsbW) :: any
+36
View File
@@ -38,6 +38,42 @@ function Err.new(code: string, message: string, offset: number?): Err
return setmetatable({ code = code, message = message, offset = offset, format = nil :: string? }, Meta) :: any
end
-- the kinds a private submodule may report: Err codes in either case ("truncated", "CORRUPT", ...)
local KIND: { [string]: string } = {}
for _, c in { "TRUNCATED", "CORRUPT", "CHECKSUM", "UNSUPPORTED", "LIMIT", "ARG", "IO", "UNKNOWN_FORMAT" } do
KIND[c] = c
KIND[string.lower(c)] = c
end
-- from(message, offset?, kind?) -> Err: the error plumbing between a private submodule (which returns
-- nil / false, "message"[, kind]) and the contract. kind names the code ("truncated", "LIMIT", ...); without it the
-- message decides: a "<code>: ..." prefix ("limit: ...", "unsupported: ...") names the code and is dropped;
-- "...truncated..." / "...past the end" / "...runs past..." -> TRUNCATED; "unsupported..." -> UNSUPPORTED;
-- "checksum..." -> CHECKSUM; anything else CORRUPT. An Err passes
-- through unchanged.
function Err.from(message: any, offset: number?, kind: string?): Err
if Err.is(message) then
return message
end
local msg = if message == nil then "failed" else tostring(message)
local code = kind and KIND[kind]
if not code then
local head, rest = string.match(msg, "^(%a+):%s*(.*)$")
if head and KIND[head] then
code, msg = KIND[head], rest :: string
elseif string.find(msg, "truncated", 1, true) or string.find(msg, "past the end", 1, true) or string.find(msg, "runs past", 1, true) then
code = Err.TRUNCATED
elseif string.find(msg, "^unsupported") then
code = Err.UNSUPPORTED
elseif string.find(msg, "^checksum") then
code = Err.CHECKSUM
else
code = Err.CORRUPT
end
end
return Err.new(code, msg, offset)
end
-- is(v) -> true when v is an Err
function Err.is(v: any): boolean
return type(v) == "table" and getmetatable(v) == Meta
+36
View File
@@ -0,0 +1,36 @@
--!strict
-- core/Sniff: helpers for the formats' sniff functions (which return 0..1 from the first bytes, never consuming and
-- never throwing): magic comparison and the start of a text file without its UTF-8 byte order mark.
--
-- function Qoi.sniff(r) return Sniff.magic(r, "qoif") end -- 1 or 0
-- function Gz.sniff(r) return Sniff.magic(r, "\31\139\8", 0, 0.9) end -- a custom score
-- local head = Sniff.text(r, 64) -- the first 64 bytes, BOM dropped
-- local head = Sniff.text(r, 512, true) -- ... and leading whitespace too
-- local text = Sniff.dropBom(whole) -- decoders: the BOM off a whole file
local Sniff = {}
type Peeker = { peek: (any, number, number?) -> string }
-- magic(r, s, at?, score?) -> score (default 1) when the bytes at offset at (default 0) are s, else 0
function Sniff.magic(r: Peeker, s: string, at: number?, score: number?): number
return if r:peek(#s, at) == s then score or 1 else 0
end
-- dropBom(s) -> s without a leading UTF-8 byte order mark (for decoders reading a whole text file)
function Sniff.dropBom(s: string): string
return if string.sub(s, 1, 3) == "\239\187\191" then string.sub(s, 4) else s
end
-- text(r, n, trim?) -> up to n bytes from the start without a UTF-8 BOM (and leading whitespace when trim)
function Sniff.text(r: Peeker, n: number, trim: boolean?): string
local s = r:peek(n)
if string.sub(s, 1, 3) == "\239\187\191" then
s = string.sub(s, 4)
end
if trim then
s = string.match(s, "^%s*(.*)$") :: string
end
return s
end
return Sniff