add utf8.sub

This commit is contained in:
MihailRis 2024-10-26 11:24:58 +03:00
parent bd60d05c3a
commit 137e6fc767
3 changed files with 17 additions and 0 deletions

View File

@ -15,4 +15,7 @@ utf8.length(text: str) -> int
-- Returns the code of the first character of the string
utf8.codepoint(chars: str) -> int
-- Returns a substring from position startchar to endchar inclusive
utf8.sub(text: str, startchar: int, [optional] endchar: int) -> str
```

View File

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

View File

@ -53,10 +53,21 @@ static int l_codepoint(lua::State* L) {
return lua::pushinteger(L, util::decode_utf8(size, string.data()));
}
static int l_sub(lua::State* L) {
auto string = util::str2u32str_utf8(lua::require_string(L, 1));
int start = std::max(0, static_cast<int>(lua::tointeger(L, 2) - 1));
int end = string.length();
if (lua::gettop(L) >= 3) {
end = std::max(0, static_cast<int>(lua::tointeger(L, 3) - 1));
}
return lua::pushstring(L, util::u32str2str_utf8(string.substr(start, end)));
}
const luaL_Reg utf8lib[] = {
{"tobytes", lua::wrap<l_encode>},
{"tostring", lua::wrap<l_decode>},
{"length", lua::wrap<l_length>},
{"codepoint", lua::wrap<l_codepoint>},
{"sub", lua::wrap<l_sub>},
{NULL, NULL}
};