From 334412ec3ab539c4c6c5211f2c1ae1286e9e9f55 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sat, 13 Apr 2024 18:02:11 -0500 Subject: Refactor type.character --- type.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/type.lua b/type.lua index 83b29f2..baf9a67 100644 --- a/type.lua +++ b/type.lua @@ -31,20 +31,27 @@ m.character_names = { } function m.character (x) - local s = tostring(x) - local uc = utf8.codepoint(s) + local char, code + if m.luatype(x) == "number" then + code = x + char = utf8_char(x) + elseif m.luatype(x) == "string" then + code = utf8_codepoint(x) + char = utf8_char(code) + end local t = { - v = utf8.char(uc), - u = uc, + char = char, + code = code, } local mt = { __type = "char", -- scheme name -- compare using codepoints since they're just numbers - __eq = function (a, b) return a.u == b.u end, - __lt = function (a, b) return a.u < b.u end, + __eq = function (a, b) return a.code == b.code end, + -- comparing codepoints is probably not the *best* idea + __lt = function (a, b) return a.code < b.code end, __tostring = function (self) - local v = self.v + local v = self.char if m.character_names[v] then v = m.character_names[v] end -- cgit 1.4.1-21-gabe81