Files
rosetta/src/Formats/codecs/Text/init.luau
T
4DBug f784b4eb9b 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).
2026-10-31 10:58:31 -05:00

67 lines
2.3 KiB
Luau

--!strict
--!optimize 2
-- codecs/Text: text decoding helpers for the text formats: a UTF-8 BOM is stripped, text that is not valid UTF-8 is
-- read as Windows-1252 (classic .m3u / .pls / .ini / .srt files), lines split on CRLF / CR / LF, trim. Absorbed from
-- playlist-luau 3fc7fd3 (same author, no third-party code). Never throws.
local Text = {}
-- utf8char(codepoint) -> its UTF-8 bytes (no validation: the parsers check ranges first; utf8.char would throw)
function Text.utf8char(c: number): string
local char = string.char
if c < 0x80 then
return char(c)
elseif c < 0x800 then
return char(0xC0 + c // 64, 0x80 + c % 64)
elseif c < 0x10000 then
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
-- CP1252 bytes 0x80..0x9F (undefined ones map to U+FFFD)
Text.CP1252 = {
[0x80] = 0x20AC, [0x82] = 0x201A, [0x83] = 0x0192, [0x84] = 0x201E, [0x85] = 0x2026, [0x86] = 0x2020,
[0x87] = 0x2021, [0x88] = 0x02C6, [0x89] = 0x2030, [0x8A] = 0x0160, [0x8B] = 0x2039, [0x8C] = 0x0152,
[0x8E] = 0x017D, [0x91] = 0x2018, [0x92] = 0x2019, [0x93] = 0x201C, [0x94] = 0x201D, [0x95] = 0x2022,
[0x96] = 0x2013, [0x97] = 0x2014, [0x98] = 0x02DC, [0x99] = 0x2122, [0x9A] = 0x0161, [0x9B] = 0x203A,
[0x9C] = 0x0153, [0x9E] = 0x017E, [0x9F] = 0x0178,
}
function Text.trim(s: string): string
return (string.match(s, "^%s*(.-)%s*$") :: string)
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
text = string.sub(text, 4)
end
if utf8.len(text) then
return text
end
if warnings then
table.insert(warnings, "text is not UTF-8: read as Windows-1252")
end
local out = table.create(#text)
for i = 1, #text do
local b = string.byte(text, i)
if b < 0x80 then
out[i] = string.char(b)
else
out[i] = utf8.char(if b < 0xA0 then Text.CP1252[b] or 0xFFFD else b)
end
end
return table.concat(out)
end
function Text.lines(text: string): { string }
text = string.gsub(text, "\r\n?", "\n")
local out = {}
for line in string.gmatch(text .. "\n", "([^\n]*)\n") do
table.insert(out, line)
end
return out
end
return Text