ws-cleanup: codecs/compress/PackBits; UTF-16 and half floats from one place

- codecs/compress/PackBits (new, from image/tiff/Pixels): decode (lenient count + a clean flag for strict callers)
  and encode; image/tiff and layered/psd (which carried its own decoder) use it. tests/codecs/packbits.
- codecs/text/Text.utf16(buf, offset, length, bigEndian?): the UTF-16 -> UTF-8 conversion codecs/font/TrueType,
  archive/7z and layered/psd each wrote (lone surrogates -> U+FFFD everywhere now).
- image/tiff/Pixels and data/cbor use core/Bytes.half instead of their own float16 decoders.
This commit is contained in:
4DBug committed 2026-10-31 12:28:33 -05:00
1 parent 51b08575c4
commit 44a599f00c
13 files changed
+191 -178

No files matched your search

+28
View File
@@ -31,6 +31,34 @@ function Text.trim(s: string): string
return (string.match(s, "^%s*(.-)%s*$") :: string)
end
-- utf16(buf, offset, length, bigEndian?) -> UTF-8 text of UTF-16 code units (surrogate pairs joined, lone surrogates
-- become U+FFFD; an odd last byte is ignored). Font names, 7z / PSD / Windows strings, XML in UTF-16.
function Text.utf16(b: buffer, o: number, n: number, bigEndian: boolean?): string
local out = {}
local k = 0
local readu16 = buffer.readu16
local function unit(at: number): number
local v = readu16(b, at)
return if bigEndian then (v % 256) * 256 + v // 256 else v
end
while k + 1 < n do
local c = unit(o + k)
k += 2
if c >= 0xD800 and c < 0xDC00 and k + 1 < n then
local lo = unit(o + k)
if lo >= 0xDC00 and lo < 0xE000 then
c = 0x10000 + (c - 0xD800) * 1024 + (lo - 0xDC00)
k += 2
end
end
if c >= 0xD800 and c < 0xE000 then
c = 0xFFFD
end
table.insert(out, Text.utf8char(c))
end
return table.concat(out)
end
-- toUtf8(text, warnings?) -> text as UTF-8: strips a BOM; text that is not valid UTF-8 is read as Windows-1252
function Text.toUtf8(text: string, warnings: { string }?): string
if string.sub(text, 1, 3) == "\239\187\191" then