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:
1 parent
3243404374
commit
763f4a7c04
44 files changed
+374
-370
No files matched your search
@@ -8,6 +8,7 @@
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
@@ -18,7 +19,7 @@ local Csv = {
|
||||
category = "data",
|
||||
extensions = { "csv", "tsv" },
|
||||
mime = { "text/csv", "text/tab-separated-values" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "core/Sniff" },
|
||||
caps = { decode = true, encode = true, header = true },
|
||||
}
|
||||
|
||||
@@ -175,9 +176,7 @@ end
|
||||
|
||||
function Csv.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local s = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
if sub(s, 1, 3) == "\239\187\191" then
|
||||
s = sub(s, 4)
|
||||
end
|
||||
s = Sniff.dropBom(s)
|
||||
local rows, err = Csv.parse(s, opts)
|
||||
if not rows then
|
||||
return nil, err
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
@@ -19,7 +20,7 @@ local Ini = {
|
||||
category = "data",
|
||||
extensions = { "ini", "cfg", "conf", "inf", "desktop" },
|
||||
mime = { "text/plain" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "core/Sniff" },
|
||||
caps = { decode = true, encode = true, ordered = true },
|
||||
}
|
||||
|
||||
@@ -73,9 +74,7 @@ end
|
||||
function Ini.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local s = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
if string.sub(s, 1, 3) == "\239\187\191" then
|
||||
s = string.sub(s, 4)
|
||||
end
|
||||
s = Sniff.dropBom(s)
|
||||
local root = Types.object()
|
||||
local cur = root
|
||||
local lastKey: string? = nil
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
|
||||
@@ -14,7 +15,7 @@ local JsonFormat = {
|
||||
category = "data",
|
||||
extensions = { "json", "jsonl", "ndjson", "geojson" },
|
||||
mime = { "application/json", "application/x-ndjson" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/Json" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/Json", "core/Sniff" },
|
||||
caps = { decode = true, encode = true, ordered = true, streaming = true },
|
||||
}
|
||||
|
||||
@@ -27,10 +28,7 @@ export type EncodeOptions = Json.EncodeOptions & {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function JsonFormat.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(64)
|
||||
if string.sub(head, 1, 3) == "\239\187\191" then
|
||||
head = string.sub(head, 4)
|
||||
end
|
||||
local head = Sniff.text(r, 64)
|
||||
local first = string.match(head, "^[ \t\r\n]*(.)")
|
||||
if first == "{" then
|
||||
local nxt = string.match(head, "^[^{]*{[ \t\r\n]*(.)")
|
||||
|
||||
@@ -40,7 +40,7 @@ function Msgpack.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
while not r:eof() do
|
||||
local v, e, at = Codec.decode(r, o)
|
||||
if e then
|
||||
return nil, Err.new(if string.find(e, "truncated", 1, true) or string.find(e, "past the end", 1, true) then Err.TRUNCATED else Err.CORRUPT, e, at)
|
||||
return nil, Err.from(e, at)
|
||||
end
|
||||
table.insert(out, v)
|
||||
end
|
||||
|
||||
@@ -143,7 +143,7 @@ function Plist.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
if len >= 8 and buffer.readstring(r.buf, r.base, 6) == "bplist" then
|
||||
local v, e = Binary.decode(r.buf, r.base, len, opts)
|
||||
if e then
|
||||
return nil, Err.new(if string.find(e, "truncated", 1, true) or string.find(e, "past the end", 1, true) then Err.TRUNCATED else Err.CORRUPT, e)
|
||||
return nil, Err.from(e)
|
||||
end
|
||||
return v, nil
|
||||
end
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
@@ -15,7 +16,7 @@ local XmlFormat = {
|
||||
category = "data",
|
||||
extensions = { "xml", "xsd", "xsl", "xslt", "rss", "atom", "xhtml" },
|
||||
mime = { "application/xml", "text/xml" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "core/Sniff" },
|
||||
caps = { decode = true, encode = true, namespaces = true, streaming = true },
|
||||
}
|
||||
|
||||
@@ -24,10 +25,7 @@ export type EncodeOptions = Xml.EncodeOptions
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function XmlFormat.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(64)
|
||||
if string.sub(head, 1, 3) == "\239\187\191" then
|
||||
head = string.sub(head, 4)
|
||||
end
|
||||
local head = Sniff.text(r, 64)
|
||||
if string.match(head, "^<%?xml[ \t\r\n]") or string.sub(head, 1, 4) == "\255\254<\0" or string.sub(head, 1, 4) == "\254\255\0<" then
|
||||
return 0.5
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user