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 --- src/reg.c | 4 ++-- src/table.c | 39 +++++++++++++++++++++++++++++++++++++-- src/table.h | 5 +++-- 3 files changed, 42 insertions(+), 6 deletions(-) (limited to 'src') 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} }; -- cgit v1.2.3