add utf8.encode
This commit is contained in:
parent
e6ad3e41a6
commit
52e62bbf95
@ -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
|
||||
|
||||
28
src/logic/scripting/lua/libs/libutf8.cpp
Normal file
28
src/logic/scripting/lua/libs/libutf8.cpp
Normal 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}
|
||||
};
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user