From 742349dec306d5fc1201254ddf09b223f114b1de Mon Sep 17 00:00:00 2001 From: IonutParau Date: Mon, 30 Jun 2025 18:42:39 +0200 Subject: [PATCH] fixed unicode.sub --- src/neonucleus.h | 2 +- src/sandbox.lua | 2 ++ src/testLuaArch.c | 53 ++++++++++++++++------------------------------- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/src/neonucleus.h b/src/neonucleus.h index d807836..469289e 100644 --- a/src/neonucleus.h +++ b/src/neonucleus.h @@ -53,7 +53,7 @@ #define NN_MAX_METHODS 32 #define NN_MAX_USERS 128 #define NN_MAX_ARCHITECTURES 16 -#define NN_MAX_SIGNALS 32 +#define NN_MAX_SIGNALS 128 #define NN_MAX_SIGNAL_VALS 32 #define NN_MAX_USERDATA 1024 #define NN_MAX_USER_SIZE 128 diff --git a/src/sandbox.lua b/src/sandbox.lua index 7733e83..c03cbaf 100644 --- a/src/sandbox.lua +++ b/src/sandbox.lua @@ -399,6 +399,7 @@ sandbox = { isWide = function(s) return unicode.wlen(s) > unicode.len(s) end, upper = string.upper, lower = string.lower, + --[[ sub = function (str,a,b) if not b then b = utf8.len(str) end if not a then a = 1 end @@ -424,6 +425,7 @@ sandbox = { return str:sub(a,b) -- return str:sub(a, b) end, + ]] }), checkArg = checkArg, component = libcomponent, diff --git a/src/testLuaArch.c b/src/testLuaArch.c index 8b7df6c..0bfd0a7 100644 --- a/src/testLuaArch.c +++ b/src/testLuaArch.c @@ -427,10 +427,6 @@ static int testLuaArch_component_invoke(lua_State *L) { int testLuaArch_unicode_sub(lua_State *L) { const char *s = luaL_checkstring(L, 1); int start = luaL_checkinteger(L, 2); - int stop = -1; - if(lua_isinteger(L, 3)) { - stop = luaL_checkinteger(L, 3); - } if(!nn_unicode_validate(s)) { luaL_error(L, "invalid utf-8"); } @@ -438,58 +434,45 @@ int testLuaArch_unicode_sub(lua_State *L) { if(len < 0) { luaL_error(L, "length overflow"); } + int stop = len; + if(lua_isinteger(L, 3)) { + stop = luaL_checkinteger(L, 3); + } // OpenOS does this... if(len == 0) { lua_pushstring(L, ""); return 1; } - // Lua indexing bullshit + + if(start == 0) start = 1; if(stop == 0) { lua_pushstring(L, ""); return 1; } - if(start > 0) { - start -= 1; - } else if(start < 0) { - start = len + start; + if(start < 0) start = len + start + 1; + if(stop < 0) stop = len + stop + 1; + + if(stop >= len) { + stop = len; } - if(stop > 0) { - stop -= 1; - } else if(stop < 0) { - stop = len + stop; - } - if(start < 0) start = 0; - if(stop < 0) stop = 0; - if(start >= len) { - lua_pushstring(L, ""); - return 1; - } - if(stop >= len) stop = len - 1; - if(stop < start) { - lua_pushstring(L, ""); - return 1; - } - if(start < 0) { - lua_pushstring(L, ""); - return 1; - } - if((stop - start) >= len) { + + if(start > stop) { lua_pushstring(L, ""); return 1; } + // there is a way to do it without an allocation // however, I'm lazy unsigned int *points = nn_unicode_codepoints(s); if(points == NULL) { luaL_error(L, "out of memory"); } - // there are a few cases where memory is leaked due to - // Lua's tendency to longjmp. - // TODO: fix them. - char *sub = nn_unicode_char(points + start, stop - start + 1); - lua_pushstring(L, sub); + + char *sub = nn_unicode_char(points + start - 1, stop - start + 1); + lua_pushstring(L, sub); // TODO: fix OOM here nn_free(sub); nn_free(points); + return 1; }