From 26e562490ff1c4a2d61cd0d22f086518caa032a1 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 24 Aug 2025 14:28:17 +0200 Subject: [PATCH] Replace StringUtils::format in components/lua --- components/lua/configuration.cpp | 13 ++++++------- components/lua/inputactions.cpp | 25 ++++++++++++------------- components/lua/utf8.cpp | 18 ++++++++---------- components/lua/yamlloader.cpp | 12 ++++++------ 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/components/lua/configuration.cpp b/components/lua/configuration.cpp index 858137ab73..3ca350e39b 100644 --- a/components/lua/configuration.cpp +++ b/components/lua/configuration.cpp @@ -3,10 +3,10 @@ #include #include #include +#include #include #include -#include #include 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)); } } } diff --git a/components/lua/inputactions.cpp b/components/lua/inputactions.cpp index b0c7fe2e9f..5570824394 100644 --- a/components/lua/inputactions.cpp +++ b/components/lua/inputactions.cpp @@ -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()) diff --git a/components/lua/utf8.cpp b/components/lua/utf8.cpp index d8aa7cf295..4c52c75255 100644 --- a/components/lua/utf8.cpp +++ b/components/lua/utf8.cpp @@ -1,7 +1,7 @@ -#include - #include "utf8.hpp" +#include + 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()) - 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(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(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)); diff --git a/components/lua/yamlloader.cpp b/components/lua/yamlloader.cpp index df83af6253..e271f7c428 100644 --- a/components/lua/yamlloader.cpp +++ b/components/lua/yamlloader.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -13,8 +14,7 @@ #include -#include -#include +#include 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(lua, true); - if (Misc::StringUtils::lowerCase(value) == "false") + if (Misc::StringUtils::ciEqual(value, "false")) return sol::make_object(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); } }