1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-02 16:36:41 +00:00

using misc::stringutils::format and simplify relativeposition function

This commit is contained in:
Kindi 2023-09-01 19:26:18 +08:00
parent af58b531da
commit dd61caa96d

View file

@ -1,4 +1,5 @@
#include <codecvt> #include <codecvt>
#include <components/misc/strings/format.hpp>
#include "utf8.hpp" #include "utf8.hpp"
@ -17,24 +18,20 @@ namespace
{ {
double integer; double integer;
if (!arg.is<double>()) if (!arg.is<double>())
throw std::runtime_error(std::format("bad argument #{} to '{}' (number expected, got {})", n, name, throw std::runtime_error(Misc::StringUtils::format("bad argument #%i to '%s' (number expected, got %s)", n,
sol::type_name(arg.lua_state(), arg.get_type()))); name, sol::type_name(arg.lua_state(), arg.get_type())));
if (std::modf(arg, &integer) != 0) if (std::modf(arg, &integer) != 0)
throw std::runtime_error( throw std::runtime_error(
std::format("bad argument #{} to '{}' (number has no integer representation)", n, name)); Misc::StringUtils::format("bad argument #{} to '{}' (number has no integer representation)", n, name));
return integer; return integer;
} }
inline void relativePosition(int64_t& pos, const size_t& len) inline void relativePosition(int64_t& pos, const size_t& len)
{ {
if (pos >= 0) if (pos < 0)
return; pos = std::max<int64_t>(0, pos + len + 1);
else if (0u - pos > static_cast<int64_t>(len))
pos = 0;
else
pos = len + pos + 1;
} }
// returns: first - character pos in bytes, second - character codepoint // returns: first - character pos in bytes, second - character codepoint
@ -101,7 +98,8 @@ namespace LuaUtf8
{ {
int64_t codepoint = getInteger(args[i], (i + 1), "char"); int64_t codepoint = getInteger(args[i], (i + 1), "char");
if (codepoint < 0 || codepoint > MAXUTF) if (codepoint < 0 || codepoint > MAXUTF)
throw std::runtime_error(std::format("bad argument #{} to 'char' (value out of range)", (i + 1))); throw std::runtime_error(
Misc::StringUtils::format("bad argument #{} to 'char' (value out of range)", (i + 1)));
result += converter.to_bytes(codepoint); result += converter.to_bytes(codepoint);
} }