Add utils from who-er

This commit is contained in:
2024-12-12 13:58:41 +01:00
parent 02146b78c8
commit 7580dd153a
2 changed files with 51 additions and 0 deletions

View File

@@ -8,7 +8,10 @@ if not Heimdall_Data then Heimdall_Data = {} end
---@field raceMap table<string, string>
---@field stinkies table<string, boolean>
---@field messenger HeimdallMessengerData
---@field who HeimdallWhoData
---@field dumpTable fun(table: any, depth?: number): nil
---@field utf8len fun(input: string): number
---@field padString fun(input: string, targetLength: number, left?: boolean): string
--- Config ---
---@class HeimdallConfig
@@ -37,6 +40,9 @@ if not Heimdall_Data then Heimdall_Data = {} end
---@field queue table<string, Message>
---@field ticker number?
---@class HeimdallWhoData
---@field ticker number?
data.messenger = {
queue = {}
}
@@ -85,3 +91,46 @@ data.raceMap = {
}
data.stinkies = {}
---@param input string
---@return number
data.utf8len = function(input)
if not input then
return 0
end
local len = 0
local i = 1
local n = #input
while i <= n do
local c = input:byte(i)
if c >= 0 and c <= 127 then
i = i + 1
elseif c >= 194 and c <= 223 then
i = i + 2
elseif c >= 224 and c <= 239 then
i = i + 3
elseif c >= 240 and c <= 244 then
i = i + 4
else
i = i + 1
end
len = len + 1
end
return len
end
---@param input string
---@param targetLength number
---@param left boolean
---@return string
data.padString = function(input, targetLength, left)
left = left or false
local len = data.utf8len(input)
if len < targetLength then
if left then
input = input .. string.rep(" ", targetLength - len)
else
input = string.rep(" ", targetLength - len) .. input
end
end
return input
end