dotfiles from arch

This commit is contained in:
2025-09-28 11:39:12 +02:00
parent 75885729cd
commit d1c6923bbb
1358 changed files with 575835 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
local platform = require 'bee.platform'
local windows
if platform.os == 'windows' then
windows = require 'bee.windows'
end
local m = {}
function m.toutf8(text)
if not windows then
return text
end
return windows.a2u(text)
end
function m.fromutf8(text)
if not windows then
return text
end
return windows.u2a(text)
end
return m

View File

@@ -0,0 +1,123 @@
local ansi = require 'encoder.ansi'
local utf16 = require 'encoder.utf16'
local utf16le = utf16('le', utf8.codepoint '<EFBFBD>')
local utf16be = utf16('be', utf8.codepoint '<EFBFBD>')
---@alias encoder.encoding '"utf8"'|'"utf16"'|'"utf16le"'|'"utf16be"'
---@alias encoder.bom '"no"'|'"yes"'|'"auto"'
local m = {}
---@param encoding encoder.encoding
---@param s string
---@param i? integer
---@param j? integer
function m.len(encoding, s, i, j)
i = i or 1
j = j or #s
if encoding == 'utf16'
or encoding == 'utf16' then
local us = utf16le.fromutf8(s:sub(i, j))
return #us // 2
end
if encoding == 'utf16be' then
local us = utf16be.fromutf8(s:sub(i, j))
return #us // 2
end
if encoding == 'utf8' then
return utf8.len(s, i, j, true)
end
log.error('Unsupport len encoding:', encoding)
return j - i + 1
end
---@param encoding encoder.encoding
---@param s string
---@param n integer
---@param i? integer
function m.offset(encoding, s, n, i)
i = i or 1
if encoding == 'utf16'
or encoding == 'utf16le' then
local line = s:match('[^\r\n]*', i)
if not line:find '[\x80-\xff]' then
return n + i - 1
end
local us = utf16le.fromutf8(line)
local os = utf16le.toutf8(us:sub(1, n * 2 - 2))
return #os + i
end
if encoding == 'utf16be' then
local line = s:match('[^\r\n]*', i)
if not line:find '[\x80-\xff]' then
return n + i - 1
end
local us = utf16be.fromutf8(line)
local os = utf16be.toutf8(us:sub(1, n * 2 - 2))
return #os + i
end
if encoding == 'utf8' then
return utf8.offset(s, n, i)
end
log.error('Unsupport offset encoding:', encoding)
return n + i - 1
end
---@param encoding encoder.encoding
---@param text string
---@param bom encoder.bom
---@return string
function m.encode(encoding, text, bom)
if encoding == 'utf8' then
if bom == 'yes' then
text = '\xEF\xBB\xBF' .. text
end
return text
end
if encoding == 'ansi' then
return ansi.fromutf8(text)
end
if encoding == 'utf16'
or encoding == 'utf16le' then
text = utf16le.fromutf8(text)
if bom == 'yes'
or bom == 'auto' then
text = '\xFF\xFE' .. text
end
return text
end
if encoding == 'utf16be' then
text = utf16be.fromutf8(text)
if bom == 'yes'
or bom == 'auto' then
text = '\xFE\xFF' .. text
end
return text
end
log.error('Unsupport encode encoding:', encoding)
return text
end
---@param encoding encoder.encoding
---@param text string
---@return string
function m.decode(encoding, text)
if encoding == 'utf8' then
return text
end
if encoding == 'ansi' then
return ansi.toutf8(text)
end
if encoding == 'utf16'
or encoding == 'utf16le' then
return utf16le.toutf8(text)
end
if encoding == 'utf16be' then
return utf16be.toutf8(text)
end
log.error('Unsupport encode encoding:', encoding)
return text
end
return m

View File

@@ -0,0 +1,157 @@
local error = error
local strchar = string.char
local strbyte = string.byte
local strmatch = string.match
local utf8char = utf8.char
local tconcat = table.concat
local function be_tochar(code)
return strchar((code >> 8) & 0xFF, code & 0xFF)
end
local function be_tobyte(s, i)
local h, l = strbyte(s, i, i+1)
return (h << 8) | l
end
local function le_tochar(code)
return strchar(code & 0xFF, (code >> 8) & 0xFF)
end
local function le_tobyte(s, i)
local l, h = strbyte(s, i, i+1)
return (h << 8) | l
end
local function utf16char(tochar, code)
if code < 0x10000 then
return tochar(code)
else
code = code - 0x10000
return tochar(0xD800 + (code >> 10))..tochar(0xDC00 + (code & 0x3FF))
end
end
local function utf16next(s, n, tobyte)
if n > #s then
return
end
local code1 = tobyte(s, n)
if code1 < 0xD800 or code1 >= 0xE000 then
return n+2, code1
elseif code1 >= 0xD800 and code1 < 0xDC00 then
n = n + 2
if n > #s then
return n --invaild
end
local code2 = tobyte(s, n)
if code2 < 0xDC00 or code2 >= 0xE000 then
return n --invaild
end
local code = 0x10000 + ((code1 - 0xD800) << 10) + ((code2 - 0xDC00) & 0x3FF)
return n+2, code
else
return n+2 --invaild
end
end
local function utf16codes(s, tobyte)
return function (_, n)
return utf16next(s, n, tobyte)
end, s, 1
end
local _utf8byte = utf8.codes ""
local function utf8byte(s, n)
local _, code = _utf8byte(s, n-1)
return code
end
--[[
U+0000.. U+007F 00..7F
U+0080.. U+07FF C2..DF 80..BF
U+0800.. U+0FFF E0 A0..BF 80..BF
U+1000.. U+CFFF E1..EC 80..BF 80..BF
U+D000.. U+D7FF ED 80..9F 80..BF
U+E000.. U+FFFF EE..EF 80..BF 80..BF
U+10000.. U+3FFFF F0 90..BF 80..BF 80..BF
U+40000.. U+FFFFF F1..F3 80..BF 80..BF 80..BF
U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
]]
local function utf8next(s, n)
if n > #s then
return
end
if strmatch(s, "^[\0-\x7F]", n) then
return n+1, utf8byte(s, n)
elseif strmatch(s, "^[\xC2-\xDF][\x80-\xBF]", n) then
return n+2, utf8byte(s, n)
elseif strmatch(s, "^[\xE0][\xA0-\xBF][\x80-\xBF]", n) then
return n+3, utf8byte(s, n)
elseif strmatch(s, "^[\xE1-\xEC][\x80-\xBF][\x80-\xBF]", n) then
return n+3, utf8byte(s, n)
elseif strmatch(s, "^[\xED][\x80-\x9F][\x80-\xBF]", n) then
return n+3, utf8byte(s, n)
elseif strmatch(s, "^[\xEE-\xEF][\x80-\xBF][\x80-\xBF]", n) then
return n+3, utf8byte(s, n)
elseif strmatch(s, "^[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF]", n) then
return n+4, utf8byte(s, n)
elseif strmatch(s, "^[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]", n) then
return n+4, utf8byte(s, n)
elseif strmatch(s, "^[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF]", n) then
return n+4, utf8byte(s, n)
else
return n+1 --invaild
end
end
local function utf8codes(s)
return utf8next, s, 1
end
return function (what, replace)
local tobyte, tochar
if what == "be" then
tobyte = be_tobyte
tochar = be_tochar
else
tobyte = le_tobyte
tochar = le_tochar
end
local utf8replace = replace and utf8char(replace)
local utf16replace = replace and utf16char(tochar, replace)
local function toutf8(s)
local r = {}
for _, code in utf16codes(s, tobyte) do
if code == nil then
if replace then
r[#r+1] = utf8replace
else
error "invalid UTF-16 code"
end
else
r[#r+1] = utf8char(code)
end
end
return tconcat(r)
end
local function fromutf8(s)
local r = {}
for _, code in utf8codes(s) do
if code == nil then
if replace then
r[#r+1] = utf16replace
else
error "invalid UTF-8 code"
end
else
r[#r+1] = utf16char(tochar, code)
end
end
return tconcat(r)
end
return {
toutf8 = toutf8,
fromutf8 = fromutf8,
}
end