1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-10-17 14:16:37 +00:00

Replace StringUtils::format in components/lua

This commit is contained in:
Evil Eye 2025-08-24 14:28:17 +02:00
parent 58a232d6c7
commit 26e562490f
4 changed files with 32 additions and 36 deletions

View file

@ -3,10 +3,10 @@
#include <algorithm>
#include <bitset>
#include <cassert>
#include <format>
#include <sstream>
#include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/format.hpp>
#include <components/misc/strings/lower.hpp>
namespace LuaUtil
@ -187,13 +187,13 @@ namespace LuaUtil
line = line.substr(0, line.size() - 1);
if (!Misc::StringUtils::ciEndsWith(line, ".lua"))
throw std::runtime_error(Misc::StringUtils::format(
"Lua script should have suffix '.lua', got: %s", std::string(line.substr(0, 300))));
throw std::runtime_error(
std::format("Lua script should have suffix '.lua', got: {}", line.substr(0, 300)));
// Split tags and script path
size_t semicolonPos = line.find(':');
if (semicolonPos == std::string::npos)
throw std::runtime_error(Misc::StringUtils::format("No flags found in: %s", std::string(line)));
if (semicolonPos == std::string_view::npos)
throw std::runtime_error(std::format("No flags found in: {}", line));
std::string_view tagsStr = line.substr(0, semicolonPos);
std::string_view scriptPath = line.substr(semicolonPos + 1);
while (isSpace(scriptPath[0]))
@ -222,8 +222,7 @@ namespace LuaUtil
else if (typesIt != typeTagsByName.end())
script.mTypes.push_back(typesIt->second);
else
throw std::runtime_error(
Misc::StringUtils::format("Unknown tag '%s' in: %s", std::string(tagName), std::string(line)));
throw std::runtime_error(std::format("Unknown tag '{}' in: {}", tagName, line));
}
}
}

View file

@ -116,14 +116,14 @@ namespace LuaUtil
void Registry::insert(const Info& info)
{
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())
throw std::domain_error("Action key can't be an empty string");
if (info.mL10n.empty())
throw std::domain_error("Localization context can't be empty");
if (!validateActionValue(info.mDefaultValue, info.mType))
throw std::logic_error(Misc::StringUtils::format(
"Invalid value: \"%s\" for action \"%s\"", LuaUtil::toString(info.mDefaultValue), info.mKey));
throw std::logic_error(std::format(
"Invalid value: \"{}\" for action \"{}\"", LuaUtil::toString(info.mDefaultValue), info.mKey));
Id id = mBindingTree.insert();
mKeys.push_back(info.mKey);
mIds[std::string(info.mKey)] = id;
@ -156,7 +156,7 @@ namespace LuaUtil
{
auto iter = mIds.find(key);
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;
}
@ -183,7 +183,7 @@ namespace LuaUtil
Info info = mInfo[id];
if (info.mType != type)
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)));
return mValues[id];
}
@ -210,10 +210,9 @@ namespace LuaUtil
catch (std::exception& e)
{
if (!validateActionValue(newValue, mInfo[node].mType))
Log(Debug::Error) << Misc::StringUtils::format(
"Error due to invalid value of action \"%s\"(\"%s\"): ", mKeys[node],
LuaUtil::toString(newValue))
<< e.what();
Log(Debug::Error)
<< "Error due to invalid value of action \"" << mKeys[node]
<< "\"(\"" << LuaUtil::toString(newValue) << "\"): " << e.what();
else
Log(Debug::Error) << "Error in callback: " << e.what();
}
@ -222,8 +221,8 @@ namespace LuaUtil
bindings.end());
if (!validateActionValue(newValue, mInfo[node].mType))
Log(Debug::Error) << Misc::StringUtils::format(
"Invalid value of action \"%s\": %s", mKeys[node], LuaUtil::toString(newValue));
Log(Debug::Error) << "Invalid value of action \"" << mKeys[node]
<< "\": " << LuaUtil::toString(newValue);
if (mValues[node] != newValue)
{
mValues[node] = sol::object(newValue);
@ -270,14 +269,14 @@ namespace LuaUtil
{
auto it = mIds.find(key);
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;
}
void Registry::insert(const Info& info)
{
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())
throw std::domain_error("Trigger key can't be an empty string");
if (info.mL10n.empty())

View file

@ -1,7 +1,7 @@
#include <components/misc/strings/format.hpp>
#include "utf8.hpp"
#include <format>
namespace
{
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;
if (!arg.is<double>())
throw std::runtime_error(Misc::StringUtils::format("bad argument #%i to '%s' (number expected, got %s)", n,
name, sol::type_name(arg.lua_state(), arg.get_type())));
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(
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);
}
@ -127,8 +127,7 @@ namespace LuaUtf8
{
int64_t codepoint = getInteger(args[i], (i + 1), "char");
if (codepoint < 0 || codepoint > MAXUNICODE)
throw std::runtime_error(
"bad argument #" + std::to_string(i + 1) + " to 'char' (value out of range)");
throw std::runtime_error(std::format("bad argument #{} to 'char' (value out of range)", i + 1));
codepointToUTF8(static_cast<char32_t>(codepoint), result);
}
@ -142,8 +141,7 @@ namespace LuaUtf8
{
const auto pair = decodeNextUTF8Character(s, posByte);
if (pair.second == -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 pair;
}
@ -202,7 +200,7 @@ namespace LuaUtf8
{
codepoints.push_back(decodeNextUTF8Character(s, posByte).second);
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));

View file

@ -2,6 +2,7 @@
#include <charconv>
#include <cmath>
#include <format>
#include <limits>
#include <regex>
#include <stdexcept>
@ -13,8 +14,7 @@
#include <yaml-cpp/yaml.h>
#include <components/misc/strings/format.hpp>
#include <components/misc/strings/lower.hpp>
#include <components/misc/strings/algorithm.hpp>
namespace LuaUtil
{
@ -213,10 +213,10 @@ namespace LuaUtil
}
case ScalarType::Boolean:
{
if (Misc::StringUtils::lowerCase(value) == "true")
if (Misc::StringUtils::ciEqual(value, "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);
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)
{
const auto& mark = node.Mark();
std::string error = Misc::StringUtils::format(
" at line=%d column=%d position=%d", mark.line + 1, mark.column + 1, mark.pos + 1);
std::string error
= std::format(" at line={} column={} position={}", mark.line + 1, mark.column + 1, mark.pos + 1);
throw std::runtime_error(message + error);
}
}