aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2025-06-12 02:08:47 -0500
committerame <[email protected]>2025-06-12 02:08:47 -0500
commitec91aab02442dbbf577fa8d8e68c79a09b55f3f9 (patch)
treee201087adabba53febc0df9a34db4577755f5961
parentc3efe329c55727ca68c4975dccc774b279479fc3 (diff)
rename 'array' lib and small additions
-rw-r--r--library/lullaby.lua2
-rw-r--r--library/lullaby/table.lua (renamed from library/lullaby/array.lua)20
-rw-r--r--src/reg.c4
-rw-r--r--src/table.c39
-rw-r--r--src/table.h5
-rw-r--r--tests/table.lua4
6 files changed, 65 insertions, 9 deletions
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/table.lua
index ebc23af..51414f7 100644
--- a/library/lullaby/array.lua
+++ b/library/lullaby/table.lua
@@ -1,31 +1,47 @@
---@meta
---to be rewritten
----@deprecated
----@class lullaby.array
+---@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
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 <string.h>
#include <stdint.h>
+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))