add utf8.decode
This commit is contained in:
parent
52e62bbf95
commit
52ed1abf1c
@ -5,7 +5,7 @@
|
|||||||
#include "../lua_custom_types.hpp"
|
#include "../lua_custom_types.hpp"
|
||||||
#include "util/stringutil.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);
|
std::string string = lua::require_string(L, 1);
|
||||||
if (lua::toboolean(L, 2)) {
|
if (lua::toboolean(L, 2)) {
|
||||||
lua::createtable(L, string.length(), 0);
|
lua::createtable(L, string.length(), 0);
|
||||||
@ -22,7 +22,25 @@ int l_encode(lua::State* L) {
|
|||||||
return 1;
|
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[] = {
|
const luaL_Reg utf8lib[] = {
|
||||||
{"encode", lua::wrap<l_encode>},
|
{"encode", lua::wrap<l_encode>},
|
||||||
|
{"decode", lua::wrap<l_decode>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user