add utf8.decode

This commit is contained in:
MihailRis 2024-10-25 22:56:11 +03:00
parent 52e62bbf95
commit 52ed1abf1c

View File

@ -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<char> buffer(size);
return lua::pushstring(L, std::string(buffer.data(), size));
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
return lua::pushstring(
L,
std::string(
reinterpret_cast<char*>(bytes->data().data()),
bytes->data().size()
)
);
}
return 1;
}
const luaL_Reg utf8lib[] = {
{"encode", lua::wrap<l_encode>},
{"decode", lua::wrap<l_decode>},
{NULL, NULL}
};