mirror of
https://github.com/OpenMW/openmw.git
synced 2025-10-18 03:16:37 +00:00
Replace StringUtils::format in components/lua
This commit is contained in:
parent
58a232d6c7
commit
26e562490f
4 changed files with 32 additions and 36 deletions
|
@ -3,10 +3,10 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <format>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <components/misc/strings/algorithm.hpp>
|
#include <components/misc/strings/algorithm.hpp>
|
||||||
#include <components/misc/strings/format.hpp>
|
|
||||||
#include <components/misc/strings/lower.hpp>
|
#include <components/misc/strings/lower.hpp>
|
||||||
|
|
||||||
namespace LuaUtil
|
namespace LuaUtil
|
||||||
|
@ -187,13 +187,13 @@ namespace LuaUtil
|
||||||
line = line.substr(0, line.size() - 1);
|
line = line.substr(0, line.size() - 1);
|
||||||
|
|
||||||
if (!Misc::StringUtils::ciEndsWith(line, ".lua"))
|
if (!Misc::StringUtils::ciEndsWith(line, ".lua"))
|
||||||
throw std::runtime_error(Misc::StringUtils::format(
|
throw std::runtime_error(
|
||||||
"Lua script should have suffix '.lua', got: %s", std::string(line.substr(0, 300))));
|
std::format("Lua script should have suffix '.lua', got: {}", line.substr(0, 300)));
|
||||||
|
|
||||||
// Split tags and script path
|
// Split tags and script path
|
||||||
size_t semicolonPos = line.find(':');
|
size_t semicolonPos = line.find(':');
|
||||||
if (semicolonPos == std::string::npos)
|
if (semicolonPos == std::string_view::npos)
|
||||||
throw std::runtime_error(Misc::StringUtils::format("No flags found in: %s", std::string(line)));
|
throw std::runtime_error(std::format("No flags found in: {}", line));
|
||||||
std::string_view tagsStr = line.substr(0, semicolonPos);
|
std::string_view tagsStr = line.substr(0, semicolonPos);
|
||||||
std::string_view scriptPath = line.substr(semicolonPos + 1);
|
std::string_view scriptPath = line.substr(semicolonPos + 1);
|
||||||
while (isSpace(scriptPath[0]))
|
while (isSpace(scriptPath[0]))
|
||||||
|
@ -222,8 +222,7 @@ namespace LuaUtil
|
||||||
else if (typesIt != typeTagsByName.end())
|
else if (typesIt != typeTagsByName.end())
|
||||||
script.mTypes.push_back(typesIt->second);
|
script.mTypes.push_back(typesIt->second);
|
||||||
else
|
else
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(std::format("Unknown tag '{}' in: {}", tagName, line));
|
||||||
Misc::StringUtils::format("Unknown tag '%s' in: %s", std::string(tagName), std::string(line)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,14 +116,14 @@ namespace LuaUtil
|
||||||
void Registry::insert(const Info& info)
|
void Registry::insert(const Info& info)
|
||||||
{
|
{
|
||||||
if (mIds.find(info.mKey) != mIds.end())
|
if (mIds.find(info.mKey) != mIds.end())
|
||||||
throw std::domain_error(Misc::StringUtils::format("Action key \"%s\" is already in use", info.mKey));
|
throw std::domain_error(std::format("Action key \"{}\" is already in use", info.mKey));
|
||||||
if (info.mKey.empty())
|
if (info.mKey.empty())
|
||||||
throw std::domain_error("Action key can't be an empty string");
|
throw std::domain_error("Action key can't be an empty string");
|
||||||
if (info.mL10n.empty())
|
if (info.mL10n.empty())
|
||||||
throw std::domain_error("Localization context can't be empty");
|
throw std::domain_error("Localization context can't be empty");
|
||||||
if (!validateActionValue(info.mDefaultValue, info.mType))
|
if (!validateActionValue(info.mDefaultValue, info.mType))
|
||||||
throw std::logic_error(Misc::StringUtils::format(
|
throw std::logic_error(std::format(
|
||||||
"Invalid value: \"%s\" for action \"%s\"", LuaUtil::toString(info.mDefaultValue), info.mKey));
|
"Invalid value: \"{}\" for action \"{}\"", LuaUtil::toString(info.mDefaultValue), info.mKey));
|
||||||
Id id = mBindingTree.insert();
|
Id id = mBindingTree.insert();
|
||||||
mKeys.push_back(info.mKey);
|
mKeys.push_back(info.mKey);
|
||||||
mIds[std::string(info.mKey)] = id;
|
mIds[std::string(info.mKey)] = id;
|
||||||
|
@ -156,7 +156,7 @@ namespace LuaUtil
|
||||||
{
|
{
|
||||||
auto iter = mIds.find(key);
|
auto iter = mIds.find(key);
|
||||||
if (iter == mIds.end())
|
if (iter == mIds.end())
|
||||||
throw std::logic_error(Misc::StringUtils::format("Unknown action key: \"%s\"", key));
|
throw std::logic_error(std::format("Unknown action key: \"{}\"", key));
|
||||||
return iter->second;
|
return iter->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ namespace LuaUtil
|
||||||
Info info = mInfo[id];
|
Info info = mInfo[id];
|
||||||
if (info.mType != type)
|
if (info.mType != type)
|
||||||
throw std::logic_error(
|
throw std::logic_error(
|
||||||
Misc::StringUtils::format("Attempt to get value of type \"%s\" from action \"%s\" with type \"%s\"",
|
std::format("Attempt to get value of type \"{}\" from action \"{}\" with type \"{}\"",
|
||||||
typeName(type), key, typeName(info.mType)));
|
typeName(type), key, typeName(info.mType)));
|
||||||
return mValues[id];
|
return mValues[id];
|
||||||
}
|
}
|
||||||
|
@ -210,10 +210,9 @@ namespace LuaUtil
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
if (!validateActionValue(newValue, mInfo[node].mType))
|
if (!validateActionValue(newValue, mInfo[node].mType))
|
||||||
Log(Debug::Error) << Misc::StringUtils::format(
|
Log(Debug::Error)
|
||||||
"Error due to invalid value of action \"%s\"(\"%s\"): ", mKeys[node],
|
<< "Error due to invalid value of action \"" << mKeys[node]
|
||||||
LuaUtil::toString(newValue))
|
<< "\"(\"" << LuaUtil::toString(newValue) << "\"): " << e.what();
|
||||||
<< e.what();
|
|
||||||
else
|
else
|
||||||
Log(Debug::Error) << "Error in callback: " << e.what();
|
Log(Debug::Error) << "Error in callback: " << e.what();
|
||||||
}
|
}
|
||||||
|
@ -222,8 +221,8 @@ namespace LuaUtil
|
||||||
bindings.end());
|
bindings.end());
|
||||||
|
|
||||||
if (!validateActionValue(newValue, mInfo[node].mType))
|
if (!validateActionValue(newValue, mInfo[node].mType))
|
||||||
Log(Debug::Error) << Misc::StringUtils::format(
|
Log(Debug::Error) << "Invalid value of action \"" << mKeys[node]
|
||||||
"Invalid value of action \"%s\": %s", mKeys[node], LuaUtil::toString(newValue));
|
<< "\": " << LuaUtil::toString(newValue);
|
||||||
if (mValues[node] != newValue)
|
if (mValues[node] != newValue)
|
||||||
{
|
{
|
||||||
mValues[node] = sol::object(newValue);
|
mValues[node] = sol::object(newValue);
|
||||||
|
@ -270,14 +269,14 @@ namespace LuaUtil
|
||||||
{
|
{
|
||||||
auto it = mIds.find(key);
|
auto it = mIds.find(key);
|
||||||
if (it == mIds.end())
|
if (it == mIds.end())
|
||||||
throw std::domain_error(Misc::StringUtils::format("Unknown trigger key \"%s\"", key));
|
throw std::domain_error(std::format("Unknown trigger key \"{}\"", key));
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Registry::insert(const Info& info)
|
void Registry::insert(const Info& info)
|
||||||
{
|
{
|
||||||
if (mIds.find(info.mKey) != mIds.end())
|
if (mIds.find(info.mKey) != mIds.end())
|
||||||
throw std::domain_error(Misc::StringUtils::format("Trigger key \"%s\" is already in use", info.mKey));
|
throw std::domain_error(std::format("Trigger key \"{}\" is already in use", info.mKey));
|
||||||
if (info.mKey.empty())
|
if (info.mKey.empty())
|
||||||
throw std::domain_error("Trigger key can't be an empty string");
|
throw std::domain_error("Trigger key can't be an empty string");
|
||||||
if (info.mL10n.empty())
|
if (info.mL10n.empty())
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <components/misc/strings/format.hpp>
|
|
||||||
|
|
||||||
#include "utf8.hpp"
|
#include "utf8.hpp"
|
||||||
|
|
||||||
|
#include <format>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
constexpr std::string_view UTF8PATT = "[%z\x01-\x7F\xC2-\xF4][\x80-\xBF]*"; // %z is deprecated in Lua5.2
|
constexpr std::string_view UTF8PATT = "[%z\x01-\x7F\xC2-\xF4][\x80-\xBF]*"; // %z is deprecated in Lua5.2
|
||||||
|
@ -17,12 +17,12 @@ namespace
|
||||||
{
|
{
|
||||||
double integer;
|
double integer;
|
||||||
if (!arg.is<double>())
|
if (!arg.is<double>())
|
||||||
throw std::runtime_error(Misc::StringUtils::format("bad argument #%i to '%s' (number expected, got %s)", n,
|
throw std::runtime_error(std::format("bad argument #{} to '{}' (number expected, got {})", n, name,
|
||||||
name, sol::type_name(arg.lua_state(), arg.get_type())));
|
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(
|
||||||
Misc::StringUtils::format("bad argument #%i to '%s' (number has no integer representation)", n, name));
|
std::format("bad argument #{} to '{}' (number has no integer representation)", n, name));
|
||||||
|
|
||||||
return static_cast<std::int64_t>(integer);
|
return static_cast<std::int64_t>(integer);
|
||||||
}
|
}
|
||||||
|
@ -127,8 +127,7 @@ namespace LuaUtf8
|
||||||
{
|
{
|
||||||
int64_t codepoint = getInteger(args[i], (i + 1), "char");
|
int64_t codepoint = getInteger(args[i], (i + 1), "char");
|
||||||
if (codepoint < 0 || codepoint > MAXUNICODE)
|
if (codepoint < 0 || codepoint > MAXUNICODE)
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(std::format("bad argument #{} to 'char' (value out of range)", i + 1));
|
||||||
"bad argument #" + std::to_string(i + 1) + " to 'char' (value out of range)");
|
|
||||||
|
|
||||||
codepointToUTF8(static_cast<char32_t>(codepoint), result);
|
codepointToUTF8(static_cast<char32_t>(codepoint), result);
|
||||||
}
|
}
|
||||||
|
@ -142,8 +141,7 @@ namespace LuaUtf8
|
||||||
{
|
{
|
||||||
const auto pair = decodeNextUTF8Character(s, posByte);
|
const auto pair = decodeNextUTF8Character(s, posByte);
|
||||||
if (pair.second == -1)
|
if (pair.second == -1)
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(std::format("Invalid UTF-8 code at position {}", posByte.size()));
|
||||||
"Invalid UTF-8 code at position " + std::to_string(posByte.size()));
|
|
||||||
|
|
||||||
return pair;
|
return pair;
|
||||||
}
|
}
|
||||||
|
@ -202,7 +200,7 @@ namespace LuaUtf8
|
||||||
{
|
{
|
||||||
codepoints.push_back(decodeNextUTF8Character(s, posByte).second);
|
codepoints.push_back(decodeNextUTF8Character(s, posByte).second);
|
||||||
if (codepoints.back() == -1)
|
if (codepoints.back() == -1)
|
||||||
throw std::runtime_error("Invalid UTF-8 code at position " + std::to_string(posByte.size()));
|
throw std::runtime_error(std::format("Invalid UTF-8 code at position {}", posByte.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return sol::as_returns(std::move(codepoints));
|
return sol::as_returns(std::move(codepoints));
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <format>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -13,8 +14,7 @@
|
||||||
|
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
#include <components/misc/strings/format.hpp>
|
#include <components/misc/strings/algorithm.hpp>
|
||||||
#include <components/misc/strings/lower.hpp>
|
|
||||||
|
|
||||||
namespace LuaUtil
|
namespace LuaUtil
|
||||||
{
|
{
|
||||||
|
@ -213,10 +213,10 @@ namespace LuaUtil
|
||||||
}
|
}
|
||||||
case ScalarType::Boolean:
|
case ScalarType::Boolean:
|
||||||
{
|
{
|
||||||
if (Misc::StringUtils::lowerCase(value) == "true")
|
if (Misc::StringUtils::ciEqual(value, "true"))
|
||||||
return sol::make_object<bool>(lua, true);
|
return sol::make_object<bool>(lua, true);
|
||||||
|
|
||||||
if (Misc::StringUtils::lowerCase(value) == "false")
|
if (Misc::StringUtils::ciEqual(value, "false"))
|
||||||
return sol::make_object<bool>(lua, false);
|
return sol::make_object<bool>(lua, false);
|
||||||
|
|
||||||
nodeError(node, "Can not read a boolean value '" + value + "'");
|
nodeError(node, "Can not read a boolean value '" + value + "'");
|
||||||
|
@ -272,8 +272,8 @@ namespace LuaUtil
|
||||||
[[noreturn]] void nodeError(const YAML::Node& node, const std::string& message)
|
[[noreturn]] void nodeError(const YAML::Node& node, const std::string& message)
|
||||||
{
|
{
|
||||||
const auto& mark = node.Mark();
|
const auto& mark = node.Mark();
|
||||||
std::string error = Misc::StringUtils::format(
|
std::string error
|
||||||
" at line=%d column=%d position=%d", mark.line + 1, mark.column + 1, mark.pos + 1);
|
= std::format(" at line={} column={} position={}", mark.line + 1, mark.column + 1, mark.pos + 1);
|
||||||
throw std::runtime_error(message + error);
|
throw std::runtime_error(message + error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue