add utf8.encode

This commit is contained in:
MihailRis 2024-10-25 17:22:54 +03:00
parent e6ad3e41a6
commit 52e62bbf95
3 changed files with 30 additions and 0 deletions

View File

@ -35,6 +35,7 @@ extern const luaL_Reg playerlib[];
extern const luaL_Reg quatlib[]; // quat.cpp
extern const luaL_Reg timelib[];
extern const luaL_Reg tomllib[];
extern const luaL_Reg utf8lib[];
extern const luaL_Reg vec2lib[]; // vecn.cpp
extern const luaL_Reg vec3lib[]; // vecn.cpp
extern const luaL_Reg vec4lib[]; // vecn.cpp

View File

@ -0,0 +1,28 @@
#include "api_lua.hpp"
#include <vector>
#include "../lua_custom_types.hpp"
#include "util/stringutil.hpp"
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);
for (size_t i = 0; i < string.length(); i++) {
lua::pushinteger(L, string[i]);
lua::rawseti(L, i+1);
}
} else {
lua::newuserdata<lua::LuaBytearray>(L, string.length());
auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1);
bytearray->data().reserve(string.length());
std::memcpy(bytearray->data().data(), string.data(), string.length());
}
return 1;
}
const luaL_Reg utf8lib[] = {
{"encode", lua::wrap<l_encode>},
{NULL, NULL}
};

View File

@ -51,6 +51,7 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "quat", quatlib);
openlib(L, "time", timelib);
openlib(L, "toml", tomllib);
openlib(L, "utf8", utf8lib);
openlib(L, "vec2", vec2lib);
openlib(L, "vec3", vec3lib);
openlib(L, "vec4", vec4lib);