2017-04-03 09:24:51 +00:00
|
|
|
|
2017-07-17 19:33:11 +00:00
|
|
|
-- TODO: I don't like to have such tiny modules. Either merge into another
|
2017-10-09 11:54:56 +00:00
|
|
|
-- module or provide the functionality with another approach.
|
2017-07-17 19:33:11 +00:00
|
|
|
|
2017-04-03 09:24:51 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Enum
|
|
|
|
|
|
|
|
local enum_metatable = {
|
|
|
|
__tostring = function (e) return 'Enum:' .. e.name or 'Enum: no name' end,
|
|
|
|
__concat = function (a, b) return tostring(a) .. tostring(b) end,
|
|
|
|
}
|
|
|
|
|
|
|
|
local function enum (t)
|
|
|
|
local e = {}
|
|
|
|
for _, v in ipairs(t) do
|
|
|
|
e[v] = setmetatable({ name = v }, enum_metatable)
|
|
|
|
end
|
|
|
|
return e
|
|
|
|
end
|
|
|
|
|
2017-10-22 12:26:19 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Unicode
|
|
|
|
|
|
|
|
local UNICODE_CHAR_PATTERN = '[\01-\127\192-\255][\128-\191]*'
|
|
|
|
|
|
|
|
local UNICODE_ZERO_WIDTH_CHARACTERS = {}
|
|
|
|
for i = 128, 191 do UNICODE_ZERO_WIDTH_CHARACTERS['\204'..string.char(i)] = true end
|
|
|
|
for i = 128, 175 do UNICODE_ZERO_WIDTH_CHARACTERS['\205'..string.char(i)] = true end
|
|
|
|
|
|
|
|
|
|
|
|
local function iterate_utf8_chars (str)
|
|
|
|
-- TODO: Detect invalid codepoints.
|
|
|
|
return str:gmatch(UNICODE_CHAR_PATTERN)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function utf8_string_length (str)
|
|
|
|
local len = 0
|
|
|
|
for char in iterate_utf8_chars(str) do
|
|
|
|
if not UNICODE_ZERO_WIDTH_CHARACTERS[char] then
|
|
|
|
len = len + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return len
|
|
|
|
end
|
|
|
|
|
2017-04-03 09:24:51 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-10-09 11:54:56 +00:00
|
|
|
return {
|
|
|
|
TABLE_TYPE = enum { 'EMPTY', 'SEQUENCE', 'STRING_MAP', 'PURE_MAP', 'MIXED', 'SET' },
|
|
|
|
DISPLAY = { HIDE = 1, SMALL = 2, INLINE = 3, EXPAND = 4 },
|
2017-10-22 12:26:19 +00:00
|
|
|
utf8_string_length = utf8_string_length,
|
2017-10-09 11:54:56 +00:00
|
|
|
}
|