From bf34c92e09e84a5eb2d7823f849eda91effe9729 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 18 Mar 2026 10:33:02 +0100 Subject: [PATCH] =?UTF-8?q?VERSI=C3=93=201.4.5=20-=20[NEW]=20Afegida=20fun?= =?UTF-8?q?ci=C3=B3=20"sub"=20al=20modul=20estandar=20"utf8"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ version.h | 2 +- vscode/library.lua | 10 ++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lua.cpp b/lua.cpp index 5e28d2d..cae9c5c 100644 --- a/lua.cpp +++ b/lua.cpp @@ -993,8 +993,50 @@ extern "C" { return 1; } + static int cpp_utf8_sub(lua_State *L) { + size_t slen; + const char *s = luaL_checklstring(L, 1, &slen); + int i = luaL_checkinteger(L, 2); + int j = luaL_optinteger(L, 3, -1); + + // Get utf8.offset + lua_getglobal(L, "utf8"); + lua_getfield(L, -1, "offset"); + + // Compute start byte index + lua_pushvalue(L, 1); // string + lua_pushinteger(L, i); // start index + lua_call(L, 2, 1); // utf8.offset(s, i) + lua_Integer start = lua_tointeger(L, -1); + lua_pop(L, 1); + + if (start == 0) { + lua_pushliteral(L, ""); + lua_pop(L, 1); // pop utf8 table + return 1; + } + + // Compute end byte index (j+1) + lua_getfield(L, -1, "offset"); + lua_pushvalue(L, 1); + lua_pushinteger(L, j + 1); + lua_call(L, 2, 1); + lua_Integer end = lua_tointeger(L, -1); + lua_pop(L, 2); // pop result + utf8 table + + if (end == 0) { + // until end of string + lua_pushlstring(L, s + start - 1, slen - (start - 1)); + return 1; + } + + lua_pushlstring(L, s + start - 1, (end - 1) - (start - 1)); + return 1; + } + } + lua_State *L; bool is_playing = false; bool init_exists = false; @@ -1293,6 +1335,11 @@ void push_lua_funcs() { lua_setglobal(L, "pad"); + + lua_getglobal(L, "utf8"); // push utf8 table + lua_pushcfunction(L, cpp_utf8_sub); // push C function + lua_setfield(L, -2, "sub"); // utf8.sub = cpp_utf8_sub + lua_pop(L, 1); // pop utf8 table } /* diff --git a/version.h b/version.h index a983b99..2c93adf 100644 --- a/version.h +++ b/version.h @@ -1,3 +1,3 @@ #pragma once -#define MINI_VERSION "1.4.4" +#define MINI_VERSION "1.4.5" diff --git a/vscode/library.lua b/vscode/library.lua index f581325..2e8883d 100644 --- a/vscode/library.lua +++ b/vscode/library.lua @@ -901,3 +901,13 @@ key.RCTRL = 228 key.RSHIFT = 229 key.RALT = 230 key.RGUI = 231 + +---@class utf8 +utf8 = {} + +---@param str string +---@param startchr number +---@param endchr number +---@return string +---Returns whether the specified keyboard key is down +function utf8.sub(str, startchr, endchr) end