ws-cleanup: one base64 (core/Base64)

core/Base64 takes over codecs/Base64's API (encode(buf | string, { url, pad, wrap }, offset, length), decode of both
alphabets, decodeDataUri, dataUri) plus line wrapping (opts.wrap) for plist; codecs/Base64 is removed and glTF /
DAE require core/Base64. The private copies in data/plist (Base64.luau), data/yaml (Parse / Emit) and codecs/Brotli
(the static tables) are gone. tests/codecs/base64 merged into tests/core/base64.
This commit is contained in:
4DBug committed 2026-10-31 10:50:40 -05:00
1 parent 52a34f50ed
commit 4f2ceec735
18 files changed
+202 -410

No files matched your search

+2 -17
View File
@@ -5,6 +5,7 @@
-- !!binary; dates as their RFC 3339 text; tables referenced twice get an anchor and aliases. Never throws: bad
-- values (cycles, functions, collection keys) return nil, message.
local Types = require("@formats/Types")
local Base64 = require("@formats/core/Base64")
local Parse = require("@formats/data/yaml/Parse")
local Emit = {}
@@ -119,22 +120,6 @@ local function str(s: string, ind: string, key: boolean): string
return dquote(s)
end
local function b64(b: buffer): string
local A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local out = {}
local n = buffer.len(b)
for i = 0, n - 1, 3 do
local a1 = buffer.readu8(b, i)
local a2 = if i + 1 < n then buffer.readu8(b, i + 1) else 0
local a3 = if i + 2 < n then buffer.readu8(b, i + 2) else 0
local v = a1 * 65536 + a2 * 256 + a3
local c1, c2, c3, c4 = v // 262144, (v // 4096) % 64, (v // 64) % 64, v % 64
table.insert(out, string.sub(A, c1 + 1, c1 + 1) .. string.sub(A, c2 + 1, c2 + 1)
.. (if i + 1 < n then string.sub(A, c3 + 1, c3 + 1) else "=") .. (if i + 2 < n then string.sub(A, c4 + 1, c4 + 1) else "="))
end
return table.concat(out)
end
local function isArray(t: { [any]: any }): boolean
if Types.isObject(t) then
return false
@@ -202,7 +187,7 @@ local function scalar(st: St, v: any, ind: string, key: boolean): string?
elseif tv == "string" then
return str(v, ind, key)
elseif tv == "buffer" then
return "!!binary " .. b64(v)
return "!!binary " .. Base64.encode(v)
elseif tv == "table" and Types.Date.is(v) then
return tostring(v)
end
+2 -32
View File
@@ -7,6 +7,7 @@
-- schema. Not supported: complex keys ("? "), collections as keys, recursive aliases, merge keys "<<" (kept as a
-- normal key). Internally raises { yamlErr } tables caught by run(); never throws past it.
local Types = require("@formats/Types")
local Base64 = require("@formats/core/Base64")
local Parse = {}
@@ -171,39 +172,8 @@ function Parse.resolve(t: string, nullv: any): any
return t
end
local B64: { [number]: number } = {}
do
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for i = 1, 64 do
B64[byte(alphabet, i)] = i - 1
end
end
local function base64(t: string): buffer?
t = string.gsub(t, "[%s]", "")
t = string.gsub(t, "=+$", "")
local n = #t
if n % 4 == 1 then
return nil
end
local out = buffer.create(n * 3 // 4)
local o = 0
local acc, bits = 0, 0
for i = 1, n do
local v = B64[byte(t, i)]
if v == nil then
return nil
end
acc = acc * 64 + v
bits += 6
if bits >= 8 then
bits -= 8
local b = acc // (2 ^ bits)
buffer.writeu8(out, o, b)
o += 1
acc -= b * 2 ^ bits
end
end
return out
return (Base64.decode(t))
end
-- a scalar's value given its tag (nil = untagged, "!" = non-specific)
+1 -1
View File
@@ -20,7 +20,7 @@ local Yaml = {
category = "data",
extensions = { "yaml", "yml" },
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
deps = { "core/Err", "core/Bytes", "Types" },
deps = { "core/Err", "core/Bytes", "Types", "core/Base64" },
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
}