add utf8.encode(...)

This commit is contained in:
MihailRis 2024-11-06 16:26:35 +03:00
parent eea97337f5
commit 8e00e73b8e
5 changed files with 41 additions and 8 deletions

View File

@ -16,6 +16,9 @@ utf8.length(text: str) -> int
-- Returns the code of the first character of the string -- Returns the code of the first character of the string
utf8.codepoint(chars: str) -> int utf8.codepoint(chars: str) -> int
-- Encodes codepoint in UTF-8
utf8.encode(codepoint: int) -> str
-- Returns a substring from position startchar to endchar inclusive -- Returns a substring from position startchar to endchar inclusive
utf8.sub(text: str, startchar: int, [optional] endchar: int) -> str utf8.sub(text: str, startchar: int, [optional] endchar: int) -> str

View File

@ -16,6 +16,9 @@ utf8.length(text: str) -> int
-- Возвращает код первого символа строки -- Возвращает код первого символа строки
utf8.codepoint(chars: str) -> int utf8.codepoint(chars: str) -> int
-- Кодирует код в в UTF-8
utf8.encode(codepoint: int) -> str
-- Возвращает подстроку от позиции startchar до endchar включительно -- Возвращает подстроку от позиции startchar до endchar включительно
utf8.sub(text: str, startchar: int, [опционально] endchar: int) -> str utf8.sub(text: str, startchar: int, [опционально] endchar: int) -> str

View File

@ -6,8 +6,8 @@
#include "../lua_custom_types.hpp" #include "../lua_custom_types.hpp"
#include "util/stringutil.hpp" #include "util/stringutil.hpp"
static int l_encode(lua::State* L) { static int l_tobytes(lua::State* L) {
std::string_view string = lua::require_string(L, 1); std::string_view string = lua::require_lstring(L, 1);
if (lua::toboolean(L, 2)) { if (lua::toboolean(L, 2)) {
lua::createtable(L, string.length(), 0); lua::createtable(L, string.length(), 0);
for (size_t i = 0; i < string.length(); i++) { for (size_t i = 0; i < string.length(); i++) {
@ -23,11 +23,16 @@ static int l_encode(lua::State* L) {
return 1; return 1;
} }
static int l_decode(lua::State* L) { static int l_tostring(lua::State* L) {
if (lua::istable(L, 1)) { if (lua::istable(L, 1)) {
size_t size = lua::objlen(L, 1); size_t size = lua::objlen(L, 1);
util::Buffer<char> buffer(size); 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)) { } else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
return lua::pushstring( return lua::pushstring(
L, L,
@ -80,13 +85,21 @@ static int l_lower(lua::State* L) {
return lua::pushstring(L, util::u32str2str_utf8(string)); 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[] = { const luaL_Reg utf8lib[] = {
{"tobytes", lua::wrap<l_encode>}, {"tobytes", lua::wrap<l_tobytes>},
{"tostring", lua::wrap<l_decode>}, {"tostring", lua::wrap<l_tostring>},
{"length", lua::wrap<l_length>}, {"length", lua::wrap<l_length>},
{"codepoint", lua::wrap<l_codepoint>}, {"codepoint", lua::wrap<l_codepoint>},
{"sub", lua::wrap<l_sub>}, {"sub", lua::wrap<l_sub>},
{"upper", lua::wrap<l_upper>}, {"upper", lua::wrap<l_upper>},
{"lower", lua::wrap<l_lower>}, {"lower", lua::wrap<l_lower>},
{"encode", lua::wrap<l_encode>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -177,6 +177,11 @@ namespace lua {
return 1; 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> template <typename... Args>
inline int pushfstring(lua_State* L, const char* fmt, Args... args) { inline int pushfstring(lua_State* L, const char* fmt, Args... args) {
lua_pushfstring(L, fmt, args...); lua_pushfstring(L, fmt, args...);
@ -456,7 +461,16 @@ namespace lua {
if (!isstring(L, idx)) { if (!isstring(L, idx)) {
throw luaerror("string expected at " + std::to_string(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); std::wstring require_wstring(lua::State*, int idx);

View File

@ -94,7 +94,7 @@ static int l_meta_index(lua::State* L) {
if (static_cast<size_t>(index) > data.size()) { if (static_cast<size_t>(index) > data.size()) {
return 0; return 0;
} }
return pushinteger(L, data[index]); return pushinteger(L, data.at(index));
} }
static int l_meta_newindex(lua::State* L) { static int l_meta_newindex(lua::State* L) {