From ec91aab02442dbbf577fa8d8e68c79a09b55f3f9 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 12 Jun 2025 02:08:47 -0500 Subject: rename 'array' lib and small additions --- library/lullaby.lua | 2 +- library/lullaby/array.lua | 56 ------------------------------------ library/lullaby/table.lua | 72 +++++++++++++++++++++++++++++++++++++++++++++++ src/reg.c | 4 +-- src/table.c | 39 +++++++++++++++++++++++-- src/table.h | 5 ++-- tests/table.lua | 4 +++ 7 files changed, 119 insertions(+), 63 deletions(-) delete mode 100644 library/lullaby/array.lua create mode 100644 library/lullaby/table.lua create mode 100644 tests/table.lua diff --git a/library/lullaby.lua b/library/lullaby.lua index 9f66530..db04f5e 100644 --- a/library/lullaby.lua +++ b/library/lullaby.lua @@ -9,7 +9,7 @@ lullaby.error = require("library.lullaby.error") lullaby.io = require("library.lullaby.io") lullaby.math = require("library.lullaby.math") lullaby.net = require("library.lullaby.net") -lullaby.array = require("library.lullaby.array") +lullaby.table = require("library.lullaby.table") lullaby.test = require("library.lullaby.test") lullaby.thread = require("library.lullaby.thread") diff --git a/library/lullaby/array.lua b/library/lullaby/array.lua deleted file mode 100644 index ebc23af..0000000 --- a/library/lullaby/array.lua +++ /dev/null @@ -1,56 +0,0 @@ ----@meta - ----to be rewritten ----@deprecated ----@class lullaby.array -local table = {} - ----greatest, least ----@param array number[] -function table.quicksort(array) end - ----greatest, least ----@param array number[] -function table.mergesort(array) end - ----greatest, least ----@param array number[] -function table.shellsort(array) end - ----greatest, least ----@param array number[] -function table.bubblesort(array) end - ----greatest, least ----@param array number[] -function table.heapsort(array) end - ----least, greatest ----@param array integer[] -function table.countintsort(array) end - ----dont use this lol ----@deprecated ----greatest, least ----@param array number[] -function table.miraclesort(array) end - ----dont use this lol ----@deprecated ----greatest, least ----@param array number[] -function table.stalinsort(array) end - ----dont use this lol ----@deprecated ----greatest, least ----@param array number[] -function table.slowsort(array) end - ----dont use this lol ----@deprecated ----greatest, least ----@param array number[] -function table.bogosort(array) end - -return table diff --git a/library/lullaby/table.lua b/library/lullaby/table.lua new file mode 100644 index 0000000..51414f7 --- /dev/null +++ b/library/lullaby/table.lua @@ -0,0 +1,72 @@ +---@meta + +---to be rewritten +---@class lullaby.table +local table = {} + +---splits string into smaller strings, removing the delimiter +---```lua +---llby.table.split("/hello/world//test///", "/") -- {"hello", "world", "test"} +---llby.table.split("/hello/world//test///", "/", false) -- {"hello", "world", "", "test", "", "", ""} +---``` +---@param haystack string +---@param search string +---@param skip boolean? whether or not to skip empty chunks +---@return string[] +function table.split(haystack, search, skip) end + +---greatest, least +---@deprecated +---@param array number[] +function table.quicksort(array) end + +---greatest, least +---@deprecated +---@param array number[] +function table.mergesort(array) end + +---greatest, least +---@deprecated +---@param array number[] +function table.shellsort(array) end + +---greatest, least +---@deprecated +---@param array number[] +function table.bubblesort(array) end + +---greatest, least +---@deprecated +---@param array number[] +function table.heapsort(array) end + +---least, greatest +---@deprecated +---@param array integer[] +function table.countintsort(array) end + +---dont use this lol +---@deprecated +---greatest, least +---@param array number[] +function table.miraclesort(array) end + +---dont use this lol +---@deprecated +---greatest, least +---@param array number[] +function table.stalinsort(array) end + +---dont use this lol +---@deprecated +---greatest, least +---@param array number[] +function table.slowsort(array) end + +---dont use this lol +---@deprecated +---greatest, least +---@param array number[] +function table.bogosort(array) end + +return table diff --git a/src/reg.c b/src/reg.c index e6715c3..9393aa4 100644 --- a/src/reg.c +++ b/src/reg.c @@ -17,7 +17,7 @@ return 1;\ } -open_common(array); +open_common(table); open_common(crypto); open_common(io); open_common(math); @@ -35,7 +35,7 @@ int luaopen_lullaby(lua_State* L) { lua_newtable(L); int top = lua_gettop(L); - push(top, array); + push(top, table); push(top, crypto); push(top, io); push(top, math); diff --git a/src/table.c b/src/table.c index 593c31b..ea57b2c 100644 --- a/src/table.c +++ b/src/table.c @@ -3,6 +3,41 @@ #include #include +int l_split(lua_State* L){ + size_t str_len = 0; + size_t search_len = 0; + + const char* str = luaL_checklstring(L, 1, &str_len); + const char* search = luaL_checklstring(L, 2, &search_len); + int skip = 1; + if(lua_gettop(L) >= 3) skip = lua_toboolean(L, 3); + + lua_newtable(L); + int idx = lua_gettop(L); + + const char* last = str; + + for(;;){ + char* end = (char*)memmem(last, str_len - (last - str), search, search_len); + if(end == NULL) break; + + if(!(skip && (end - last) == 0)){ + lua_pushinteger(L, lua_objlen(L, idx) + 1); + lua_pushlstring(L, last, end - last); + lua_settable(L, idx); + } + + last = end + 1; + } + + lua_pushinteger(L, lua_objlen(L, idx) + 1); + lua_pushlstring(L, last, str_len - (last - str)); + lua_settable(L, idx); + + lua_pushvalue(L, idx); + return 1; +} + int l_unpack(lua_State* L){ int top = lua_gettop(L); lua_pushnil(L); @@ -195,7 +230,7 @@ int l_sindexof(lua_State* L) { return 1; } -int l_split(lua_State* L){ +/*int l_split(lua_State* L){ size_t input_len = 0; size_t split_len = 0; char* input = (char*)luaL_checklstring(L, 1, &input_len); @@ -229,7 +264,7 @@ int l_split(lua_State* L){ lua_settable(L, -3); return 1; -} +}*/ int l_to_char_array(lua_State* L){ size_t input_len = 0; diff --git a/src/table.h b/src/table.h index e962e3c..988eb45 100644 --- a/src/table.h +++ b/src/table.h @@ -20,8 +20,9 @@ int l_split(lua_State*); int l_to_char_array(lua_State*); int l_unpack(lua_State*); +int l_split(lua_State*); -static const luaL_Reg array_function_list [] = { +static const luaL_Reg table_function_list [] = { {"len",l_len}, {"reverse",l_reverse}, {"greatest",l_greatest}, @@ -52,6 +53,6 @@ static const luaL_Reg array_function_list [] = { {NULL,NULL} }; -static struct config array_config[] = { +static struct config table_config[] = { {.type = c_none} }; diff --git a/tests/table.lua b/tests/table.lua new file mode 100644 index 0000000..c63bfdf --- /dev/null +++ b/tests/table.lua @@ -0,0 +1,4 @@ +local llby = require"lullaby" +llby.io.pprint(llby.table.split("/hello/world//test///", "/", false)) -- {"hello", "world", "", "test", "", "", ""} + +llby.io.pprint(llby.table.split("/hi/uwu/meow", "/", false)) -- cgit v1.2.3