From 6c3d4e4b4ce8a4eb0b5ac012e38ca1f66af4b077 Mon Sep 17 00:00:00 2001 From: Ashkan Kiani Date: Fri, 18 Oct 2019 14:27:02 -0700 Subject: Update color functions (#13) - Add hsl, hsla, rgba, RRGGBBAA - Redo the parser for rgb,rgba,hsl,hsla codes. - Fix the trie implementation. - Bugfix and refactor alpha/error handling --- lua/trie.lua | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'lua/trie.lua') diff --git a/lua/trie.lua b/lua/trie.lua index b0751aa..e74d9eb 100644 --- a/lua/trie.lua +++ b/lua/trie.lua @@ -30,6 +30,7 @@ void free(void *ptr); local Trie_t = ffi.typeof('struct Trie') local Trie_ptr_t = ffi.typeof('$ *', Trie_t) +local Trie_size = ffi.sizeof(Trie_t) local function byte_to_index(b) -- 0-9 starts at string.byte('0') == 0x30 == 48 == 0b0011_0000 @@ -75,8 +76,6 @@ local function verify_byte_to_index() end end -local Trie_size = ffi.sizeof(Trie_t) - local function new_trie() local ptr = ffi.C.malloc(Trie_size) ffi.fill(ptr, Trie_size) @@ -237,6 +236,9 @@ local function print_structure(s) end local function free_trie(trie) + if trie == nil then + return + end for i = 0, 61 do local child = trie.character[i] if child ~= nil then @@ -247,24 +249,24 @@ local function free_trie(trie) end local Trie_mt = { - __index = { - insert = insert; - search = search; - longest_prefix = longest_prefix; - }; - __tostring = function(trie) - local structure = trie_structure(trie) - if structure then - return table.concat(print_structure(structure), '\n') - else - return 'nil' - end - end; - __gc = free_trie; - } -local Trie = ffi.metatype(Trie_t, Trie_mt) + __new = new_trie; + __index = { + insert = insert; + search = search; + longest_prefix = longest_prefix; + }; + __tostring = function(trie) + local structure = trie_structure(trie) + if structure then + return table.concat(print_structure(structure), '\n') + else + return 'nil' + end + end; + __gc = free_trie; +} -return Trie +return ffi.metatype('struct Trie', Trie_mt) -- local tests = { -- "cat"; -- cgit v1.2.3-70-g09d2