mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Replace use of Boost Format with fmt
Using the version of fmt that's bundled with spdlog. This breaks compatibility with existing metadata messages that use substitutions, as the placeholder syntax has changed. The new syntax is compatible with C++20's std::format.
This commit is contained in:
@@ -37,7 +37,7 @@ Messages are given as key-value maps.
|
||||
|
||||
``string list``
|
||||
|
||||
A list of CommonMark strings to be substituted into the message content string. The content string must use numbered specifiers (``%1%``, ``%2%``, etc.), where the numbers correspond to the position of the substitution string in this list to use, to denote where these strings are to be substituted.
|
||||
A list of CommonMark strings to be substituted into the message content string. The content string must use numbered specifiers (``{0}``, ``{1}``, etc.), where the numbers correspond to the position of the substitution string in this list to use, to denote where these strings are to be substituted.
|
||||
|
||||
.. _languages:
|
||||
|
||||
@@ -102,7 +102,7 @@ In English,
|
||||
.. code-block:: yaml
|
||||
|
||||
type: say
|
||||
content: 'A newer version of %1% [is available](%2%).'
|
||||
content: 'A newer version of {0} [is available]({1}).'
|
||||
subs:
|
||||
- 'this plugin'
|
||||
- 'http://www.example.com'
|
||||
|
||||
@@ -24,8 +24,9 @@
|
||||
|
||||
#include "api/helpers/crc.h"
|
||||
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
|
||||
#include <boost/crc.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <fstream>
|
||||
|
||||
#include "api/helpers/logging.h"
|
||||
@@ -80,7 +81,5 @@ uint32_t GetCrc32(const std::filesystem::path& filename) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string CrcToString(uint32_t crc) {
|
||||
return (boost::format("%08X") % crc).str();
|
||||
}
|
||||
std::string CrcToString(uint32_t crc) { return fmt::format("{:08X}", crc); }
|
||||
}
|
||||
|
||||
@@ -26,9 +26,10 @@
|
||||
|
||||
#define YAML_CPP_SUPPORT_MERGE_KEYS
|
||||
|
||||
#include <spdlog/fmt/bundled/args.h>
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <boost/format.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -103,16 +104,17 @@ struct convert<loot::Message> {
|
||||
if (node["subs"]) {
|
||||
std::vector<std::string> subs =
|
||||
node["subs"].as<std::vector<std::string>>();
|
||||
|
||||
fmt::dynamic_format_arg_store<fmt::format_context> formatArgStore;
|
||||
for (const auto& sub : subs) {
|
||||
formatArgStore.push_back(sub);
|
||||
}
|
||||
|
||||
for (auto& mc : content) {
|
||||
boost::format f(mc.GetText());
|
||||
|
||||
for (const auto& sub : subs) {
|
||||
f = f % sub;
|
||||
}
|
||||
|
||||
try {
|
||||
mc = loot::MessageContent(f.str(), mc.GetLanguage());
|
||||
} catch (const boost::io::format_error& e) {
|
||||
const auto text = fmt::vformat(mc.GetText(), formatArgStore);
|
||||
mc = loot::MessageContent(text, mc.GetLanguage());
|
||||
} catch (const fmt::format_error& e) {
|
||||
throw RepresentationException(
|
||||
node.Mark(),
|
||||
std::string("bad conversion: content substitution error: ") +
|
||||
|
||||
@@ -615,7 +615,7 @@ TEST_P(
|
||||
decodingFromYamlShouldApplySubstitutionsWhenThereIsOnlyOneContentString) {
|
||||
YAML::Node node = YAML::Load(
|
||||
"type: say\n"
|
||||
"content: con%1%tent1\n"
|
||||
"content: con{0}tent1\n"
|
||||
"subs:\n"
|
||||
" - sub1");
|
||||
Message message = node.as<Message>();
|
||||
@@ -630,9 +630,9 @@ TEST_P(MessageTest,
|
||||
"type: say\n"
|
||||
"content:\n"
|
||||
" - lang: en\n"
|
||||
" text: content1 %1%\n"
|
||||
" text: content1 {0}\n"
|
||||
" - lang: fr\n"
|
||||
" text: content2 %1%\n"
|
||||
" text: content2 {0}\n"
|
||||
"subs:\n"
|
||||
" - sub");
|
||||
Message message = node.as<Message>();
|
||||
@@ -649,7 +649,7 @@ TEST_P(
|
||||
decodingFromYamlShouldThrowIfTheContentStringExpectsMoreSubstitutionsThanExist) {
|
||||
YAML::Node node = YAML::Load(
|
||||
"type: say\n"
|
||||
"content: '%1% %2%'\n"
|
||||
"content: '{0} {1}'\n"
|
||||
"subs:\n"
|
||||
" - sub1");
|
||||
|
||||
@@ -662,10 +662,10 @@ TEST_P(MessageTest,
|
||||
decodingFromYamlShouldIgnoreSubstitutionSyntaxIfNoSubstitutionsExist) {
|
||||
YAML::Node node = YAML::Load(
|
||||
"type: say\n"
|
||||
"content: con%1%tent1\n");
|
||||
"content: con{0}tent1\n");
|
||||
Message message = node.as<Message>();
|
||||
|
||||
EXPECT_EQ(MessageContents({MessageContent("con%1%tent1")}),
|
||||
EXPECT_EQ(MessageContents({MessageContent("con{0}tent1")}),
|
||||
message.GetContent());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user