add utf8.encode(...)
This commit is contained in:
parent
eea97337f5
commit
8e00e73b8e
@ -16,6 +16,9 @@ utf8.length(text: str) -> int
|
||||
-- Returns the code of the first character of the string
|
||||
utf8.codepoint(chars: str) -> int
|
||||
|
||||
-- Encodes codepoint in UTF-8
|
||||
utf8.encode(codepoint: int) -> str
|
||||
|
||||
-- Returns a substring from position startchar to endchar inclusive
|
||||
utf8.sub(text: str, startchar: int, [optional] endchar: int) -> str
|
||||
|
||||
|
||||
@ -16,6 +16,9 @@ utf8.length(text: str) -> int
|
||||
-- Возвращает код первого символа строки
|
||||
utf8.codepoint(chars: str) -> int
|
||||
|
||||
-- Кодирует код в в UTF-8
|
||||
utf8.encode(codepoint: int) -> str
|
||||
|
||||
-- Возвращает подстроку от позиции startchar до endchar включительно
|
||||
utf8.sub(text: str, startchar: int, [опционально] endchar: int) -> str
|
||||
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
#include "../lua_custom_types.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
static int l_encode(lua::State* L) {
|
||||
std::string_view string = lua::require_string(L, 1);
|
||||
static int l_tobytes(lua::State* L) {
|
||||
std::string_view string = lua::require_lstring(L, 1);
|
||||
if (lua::toboolean(L, 2)) {
|
||||
lua::createtable(L, string.length(), 0);
|
||||
for (size_t i = 0; i < string.length(); i++) {
|
||||
@ -23,11 +23,16 @@ static int l_encode(lua::State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_decode(lua::State* L) {
|
||||
static int l_tostring(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));
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
lua::rawgeti(L, i + 1);
|
||||
buffer[i] = lua::tointeger(L, -1);
|
||||
lua::pop(L);
|
||||
}
|
||||
return lua::pushlstring(L, buffer.data(), size);
|
||||
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
|
||||
return lua::pushstring(
|
||||
L,
|
||||
@ -80,13 +85,21 @@ static int l_lower(lua::State* L) {
|
||||
return lua::pushstring(L, util::u32str2str_utf8(string));
|
||||
}
|
||||
|
||||
static int l_encode(lua::State* L) {
|
||||
auto integer = lua::tointeger(L, 1);
|
||||
ubyte bytes[4];
|
||||
size_t count = util::encode_utf8(integer, bytes);
|
||||
return lua::pushlstring(L, bytes, count);
|
||||
}
|
||||
|
||||
const luaL_Reg utf8lib[] = {
|
||||
{"tobytes", lua::wrap<l_encode>},
|
||||
{"tostring", lua::wrap<l_decode>},
|
||||
{"tobytes", lua::wrap<l_tobytes>},
|
||||
{"tostring", lua::wrap<l_tostring>},
|
||||
{"length", lua::wrap<l_length>},
|
||||
{"codepoint", lua::wrap<l_codepoint>},
|
||||
{"sub", lua::wrap<l_sub>},
|
||||
{"upper", lua::wrap<l_upper>},
|
||||
{"lower", lua::wrap<l_lower>},
|
||||
{"encode", lua::wrap<l_encode>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -177,6 +177,11 @@ namespace lua {
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushlstring(lua::State* L, const void* chars, size_t size) {
|
||||
lua_pushlstring(L, reinterpret_cast<const char*>(chars), size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline int pushfstring(lua_State* L, const char* fmt, Args... args) {
|
||||
lua_pushfstring(L, fmt, args...);
|
||||
@ -456,7 +461,16 @@ namespace lua {
|
||||
if (!isstring(L, idx)) {
|
||||
throw luaerror("string expected at " + std::to_string(idx));
|
||||
}
|
||||
return tostring(L, idx);
|
||||
return lua_tostring(L, idx);
|
||||
}
|
||||
|
||||
inline std::string_view require_lstring(lua::State* L, int idx) {
|
||||
if (!isstring(L, idx)) {
|
||||
throw luaerror("string expected at " + std::to_string(idx));
|
||||
}
|
||||
size_t len;
|
||||
const char* chars = lua_tolstring(L, idx, &len);
|
||||
return std::string_view(chars, len);
|
||||
}
|
||||
|
||||
std::wstring require_wstring(lua::State*, int idx);
|
||||
|
||||
@ -94,7 +94,7 @@ static int l_meta_index(lua::State* L) {
|
||||
if (static_cast<size_t>(index) > data.size()) {
|
||||
return 0;
|
||||
}
|
||||
return pushinteger(L, data[index]);
|
||||
return pushinteger(L, data.at(index));
|
||||
}
|
||||
|
||||
static int l_meta_newindex(lua::State* L) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user