diff --git a/src/logic/scripting/lua/libs/libutf8.cpp b/src/logic/scripting/lua/libs/libutf8.cpp index 792f2513..4cfedd03 100644 --- a/src/logic/scripting/lua/libs/libutf8.cpp +++ b/src/logic/scripting/lua/libs/libutf8.cpp @@ -5,7 +5,7 @@ #include "../lua_custom_types.hpp" #include "util/stringutil.hpp" -int l_encode(lua::State* L) { +static int l_encode(lua::State* L) { std::string string = lua::require_string(L, 1); if (lua::toboolean(L, 2)) { lua::createtable(L, string.length(), 0); @@ -22,7 +22,25 @@ int l_encode(lua::State* L) { return 1; } +static int l_decode(lua::State* L) { + if (lua::istable(L, 1)) { + size_t size = lua::objlen(L, 1); + util::Buffer buffer(size); + return lua::pushstring(L, std::string(buffer.data(), size)); + } else if (auto bytes = lua::touserdata(L, 1)) { + return lua::pushstring( + L, + std::string( + reinterpret_cast(bytes->data().data()), + bytes->data().size() + ) + ); + } + return 1; +} + const luaL_Reg utf8lib[] = { {"encode", lua::wrap}, + {"decode", lua::wrap}, {NULL, NULL} };