mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-28 12:39:41 +00:00
utf8.char
This commit is contained in:
parent
8622a6da3b
commit
d9c102e14d
2 changed files with 32 additions and 1 deletions
|
@ -1,11 +1,27 @@
|
|||
#include <codecvt>
|
||||
|
||||
#include "utf8.hpp"
|
||||
#include "luastate.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
static constexpr std::string_view UTF8PATT = "[%z\x01-\x7F\xC2-\xF4][\x80-\xBF]*"; // %z is deprecated in Lua5.2
|
||||
static constexpr uint32_t MAXUTF = 0x7FFFFFFFu;
|
||||
static constexpr uint32_t MAXUNICODE = 0x10FFFFu;
|
||||
|
||||
inline static double getInteger(const sol::stack_proxy arg, const size_t& n, const std::string_view& name)
|
||||
{
|
||||
double integer;
|
||||
if (!arg.is<double>())
|
||||
throw std::runtime_error(std::format("bad argument #{} to '{}' (number expected, got {})", n, name,
|
||||
sol::type_name(arg.lua_state(), arg.get_type())));
|
||||
|
||||
if (std::modf(arg, &integer) != 0)
|
||||
throw std::runtime_error(
|
||||
std::format("bad argument #{} to '{}' (number has no integer representation)", n, name));
|
||||
|
||||
return integer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace LuaUtf8
|
||||
|
@ -16,6 +32,19 @@ namespace LuaUtf8
|
|||
|
||||
utf8["charpattern"] = UTF8PATT;
|
||||
|
||||
utf8["char"] = [](const sol::variadic_args args) -> std::string {
|
||||
std::string result{};
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
||||
for (size_t i = 0; i < args.size(); ++i)
|
||||
{
|
||||
int64_t codepoint = getInteger(args[i], (i + 1), "char");
|
||||
if (codepoint < 0 || codepoint > MAXUTF)
|
||||
throw std::runtime_error(std::format("bad argument #{} to 'char' (value out of range)", (i + 1)));
|
||||
|
||||
result += converter.to_bytes(codepoint);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return utf8;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef COMPONENTS_LUA_UTF8_H
|
||||
#define COMPONENTS_LUA_UTF8_H
|
||||
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
namespace LuaUtf8
|
||||
{
|
||||
sol::table initUtf8Package(sol::state_view&);
|
||||
|
|
Loading…
Reference in a new issue