diff --git a/components/lua/utf8.cpp b/components/lua/utf8.cpp index 69160fdd53..eac3954230 100644 --- a/components/lua/utf8.cpp +++ b/components/lua/utf8.cpp @@ -27,10 +27,10 @@ namespace return integer; } - inline void posrelat(int64_t& pos, const size_t& len) + inline void relativePosition(int64_t& pos, const size_t& len) { if (pos >= 0) - /* no change */; + return; else if (0u - pos > static_cast(len)) pos = 0; else @@ -38,7 +38,7 @@ namespace } // returns: first - character pos in bytes, second - character codepoint - std::pair poscodes(const std::string_view& s, std::vector& pos_byte) + std::pair decodeNextUTF8Character(const std::string_view& s, std::vector& pos_byte) { const int64_t pos = pos_byte.back() - 1; const unsigned char ch = static_cast(s[pos]); @@ -113,7 +113,7 @@ namespace LuaUtf8 return sol::as_function([s, pos_byte]() mutable -> sol::optional> { if (pos_byte.back() <= static_cast(s.size())) { - const auto pair = poscodes(s, pos_byte); + const auto pair = decodeNextUTF8Character(s, pos_byte); if (pair.second == -1) throw std::runtime_error("Invalid UTF-8 code at position " + std::to_string(pos_byte.size())); @@ -129,8 +129,8 @@ namespace LuaUtf8 int64_t iv = isNilOrNone(args[0]) ? 1 : getInteger(args[0], 2, "len"); int64_t fv = isNilOrNone(args[1]) ? -1 : getInteger(args[1], 3, "len"); - posrelat(iv, len); - posrelat(fv, len); + relativePosition(iv, len); + relativePosition(fv, len); if (iv <= 0) throw std::runtime_error("bad argument #2 to 'len' (initial position out of bounds)"); @@ -144,7 +144,7 @@ namespace LuaUtf8 while (pos_byte.back() <= fv) { - if (poscodes(s, pos_byte).second == -1) + if (decodeNextUTF8Character(s, pos_byte).second == -1) return std::pair(sol::lua_nil, pos_byte.back()); } return pos_byte.size() - 1; @@ -156,8 +156,8 @@ namespace LuaUtf8 int64_t iv = isNilOrNone(args[0]) ? 1 : getInteger(args[0], 2, "codepoint"); int64_t fv = isNilOrNone(args[1]) ? iv : getInteger(args[1], 3, "codepoint"); - posrelat(iv, len); - posrelat(fv, len); + relativePosition(iv, len); + relativePosition(fv, len); if (iv <= 0) throw std::runtime_error("bad argument #2 to 'codepoint' (initial position out of bounds)"); @@ -172,7 +172,7 @@ namespace LuaUtf8 while (pos_byte.back() <= fv) { - codepoints.push_back(poscodes(s, pos_byte).second); + codepoints.push_back(decodeNextUTF8Character(s, pos_byte).second); if (codepoints.back() == -1) throw std::runtime_error("Invalid UTF-8 code at position " + std::to_string(pos_byte.size())); } @@ -186,13 +186,13 @@ namespace LuaUtf8 int64_t iv = isNilOrNone(args[0]) ? ((n >= 0) ? 1 : s.size() + 1) : getInteger(args[0], 3, "offset"); std::vector pos_byte = { 1 }; - posrelat(iv, len); + relativePosition(iv, len); if (iv > static_cast(len) + 1) throw std::runtime_error("bad argument #3 to 'offset' (position out of bounds)"); while (pos_byte.back() <= static_cast(len)) - poscodes(s, pos_byte); + decodeNextUTF8Character(s, pos_byte); for (auto it = pos_byte.begin(); it != pos_byte.end(); ++it) if (*it == iv) diff --git a/docs/source/reference/lua-scripting/overview.rst b/docs/source/reference/lua-scripting/overview.rst index 283664b2c4..6e25f321f3 100644 --- a/docs/source/reference/lua-scripting/overview.rst +++ b/docs/source/reference/lua-scripting/overview.rst @@ -4,7 +4,7 @@ Overview of Lua scripting Language and sandboxing ======================= -OpenMW supports scripts written in Lua 5.1 with some extensions (see below) from Lua 5.2. +OpenMW supports scripts written in Lua 5.1 with some extensions (see below) from Lua 5.2 and Lua 5.3. There are no plans to switch to any newer version of the language, because newer versions are not supported by LuaJIT. .. note:: @@ -38,6 +38,10 @@ Supported Lua 5.2 features: - ``__pairs`` and ``__ipairs`` metamethods; - Function ``table.unpack`` (alias to Lua 5.1 ``unpack``). +Supported Lua 5.3 features: + +- All functions in the `UTF-8 Library `__ + Loading libraries with ``require('library_name')`` is allowed, but limited. It works this way: 1. If `library_name` is one of the standard libraries, then return the library.