rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,94 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/text/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
|
||||
|
||||
-- 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
|
||||
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
|
||||
Reference in new issue
Block a user