mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-19 12:09:42 +00:00
refactoring for readability
This commit is contained in:
parent
8798217b51
commit
7eb456a169
1 changed files with 21 additions and 7 deletions
|
@ -28,6 +28,9 @@ namespace
|
||||||
return integer;
|
return integer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the input 'pos' is negative, it is treated as counting from the end of the string,
|
||||||
|
// where -1 represents the last character position, -2 represents the second-to-last position,
|
||||||
|
// and so on. If 'pos' is non-negative, it is used as-is.
|
||||||
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)
|
||||||
|
@ -181,7 +184,18 @@ namespace LuaUtf8
|
||||||
utf8["offset"]
|
utf8["offset"]
|
||||||
= [](std::string_view s, const int64_t n, const sol::variadic_args args) -> sol::optional<int64_t> {
|
= [](std::string_view s, const int64_t n, const sol::variadic_args args) -> sol::optional<int64_t> {
|
||||||
size_t len = s.size();
|
size_t len = s.size();
|
||||||
int64_t iv = isNilOrNone(args[0]) ? ((n >= 0) ? 1 : s.size() + 1) : getInteger(args[0], 3, "offset");
|
int64_t iv;
|
||||||
|
|
||||||
|
if (isNilOrNone(args[0]))
|
||||||
|
{
|
||||||
|
if (n >= 0)
|
||||||
|
iv = 1;
|
||||||
|
else
|
||||||
|
iv = s.size() + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
iv = getInteger(args[0], 3, "offset");
|
||||||
|
|
||||||
std::vector<int64_t> pos_byte = { 1 };
|
std::vector<int64_t> pos_byte = { 1 };
|
||||||
|
|
||||||
relativePosition(iv, len);
|
relativePosition(iv, len);
|
||||||
|
@ -193,14 +207,13 @@ namespace LuaUtf8
|
||||||
decodeNextUTF8Character(s, pos_byte);
|
decodeNextUTF8Character(s, pos_byte);
|
||||||
|
|
||||||
for (auto it = pos_byte.begin(); it != pos_byte.end(); ++it)
|
for (auto it = pos_byte.begin(); it != pos_byte.end(); ++it)
|
||||||
|
{
|
||||||
if (*it == iv)
|
if (*it == iv)
|
||||||
{
|
{
|
||||||
if (n <= 0)
|
if (n <= 0 && it + n >= pos_byte.begin())
|
||||||
if ((it + n) >= pos_byte.begin())
|
return *(it + n);
|
||||||
return *(it + n);
|
if (n > 0 && it + n - 1 < pos_byte.end())
|
||||||
if (n > 0)
|
return *(it + n - 1);
|
||||||
if ((it + n - 1) < pos_byte.end())
|
|
||||||
return *(it + n - 1);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (*it > iv) /* a continuation byte */
|
else if (*it > iv) /* a continuation byte */
|
||||||
|
@ -210,6 +223,7 @@ namespace LuaUtf8
|
||||||
else
|
else
|
||||||
throw std::runtime_error("initial position is a continuation byte");
|
throw std::runtime_error("initial position is a continuation byte");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sol::nullopt;
|
return sol::nullopt;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue