fixed unicode.sub

This commit is contained in:
IonutParau 2025-06-30 18:42:39 +02:00
parent 24b47074eb
commit 742349dec3
3 changed files with 21 additions and 36 deletions

View File

@ -53,7 +53,7 @@
#define NN_MAX_METHODS 32 #define NN_MAX_METHODS 32
#define NN_MAX_USERS 128 #define NN_MAX_USERS 128
#define NN_MAX_ARCHITECTURES 16 #define NN_MAX_ARCHITECTURES 16
#define NN_MAX_SIGNALS 32 #define NN_MAX_SIGNALS 128
#define NN_MAX_SIGNAL_VALS 32 #define NN_MAX_SIGNAL_VALS 32
#define NN_MAX_USERDATA 1024 #define NN_MAX_USERDATA 1024
#define NN_MAX_USER_SIZE 128 #define NN_MAX_USER_SIZE 128

View File

@ -399,6 +399,7 @@ sandbox = {
isWide = function(s) return unicode.wlen(s) > unicode.len(s) end, isWide = function(s) return unicode.wlen(s) > unicode.len(s) end,
upper = string.upper, upper = string.upper,
lower = string.lower, lower = string.lower,
--[[
sub = function (str,a,b) sub = function (str,a,b)
if not b then b = utf8.len(str) end if not b then b = utf8.len(str) end
if not a then a = 1 end if not a then a = 1 end
@ -424,6 +425,7 @@ sandbox = {
return str:sub(a,b) return str:sub(a,b)
-- return str:sub(a, b) -- return str:sub(a, b)
end, end,
]]
}), }),
checkArg = checkArg, checkArg = checkArg,
component = libcomponent, component = libcomponent,

View File

@ -427,10 +427,6 @@ static int testLuaArch_component_invoke(lua_State *L) {
int testLuaArch_unicode_sub(lua_State *L) { int testLuaArch_unicode_sub(lua_State *L) {
const char *s = luaL_checkstring(L, 1); const char *s = luaL_checkstring(L, 1);
int start = luaL_checkinteger(L, 2); int start = luaL_checkinteger(L, 2);
int stop = -1;
if(lua_isinteger(L, 3)) {
stop = luaL_checkinteger(L, 3);
}
if(!nn_unicode_validate(s)) { if(!nn_unicode_validate(s)) {
luaL_error(L, "invalid utf-8"); luaL_error(L, "invalid utf-8");
} }
@ -438,58 +434,45 @@ int testLuaArch_unicode_sub(lua_State *L) {
if(len < 0) { if(len < 0) {
luaL_error(L, "length overflow"); luaL_error(L, "length overflow");
} }
int stop = len;
if(lua_isinteger(L, 3)) {
stop = luaL_checkinteger(L, 3);
}
// OpenOS does this... // OpenOS does this...
if(len == 0) { if(len == 0) {
lua_pushstring(L, ""); lua_pushstring(L, "");
return 1; return 1;
} }
// Lua indexing bullshit
if(start == 0) start = 1;
if(stop == 0) { if(stop == 0) {
lua_pushstring(L, ""); lua_pushstring(L, "");
return 1; return 1;
} }
if(start > 0) { if(start < 0) start = len + start + 1;
start -= 1; if(stop < 0) stop = len + stop + 1;
} else if(start < 0) {
start = len + start; if(stop >= len) {
stop = len;
} }
if(stop > 0) {
stop -= 1; if(start > stop) {
} 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) {
lua_pushstring(L, ""); lua_pushstring(L, "");
return 1; return 1;
} }
// there is a way to do it without an allocation // there is a way to do it without an allocation
// however, I'm lazy // however, I'm lazy
unsigned int *points = nn_unicode_codepoints(s); unsigned int *points = nn_unicode_codepoints(s);
if(points == NULL) { if(points == NULL) {
luaL_error(L, "out of memory"); luaL_error(L, "out of memory");
} }
// there are a few cases where memory is leaked due to
// Lua's tendency to longjmp. char *sub = nn_unicode_char(points + start - 1, stop - start + 1);
// TODO: fix them. lua_pushstring(L, sub); // TODO: fix OOM here
char *sub = nn_unicode_char(points + start, stop - start + 1);
lua_pushstring(L, sub);
nn_free(sub); nn_free(sub);
nn_free(points); nn_free(points);
return 1; return 1;
} }