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:
Oliver Hamlet
2023-08-29 21:32:52 +01:00
committed by Oliver Hamlet
parent 356eb0243f
commit dc95b18805
4 changed files with 22 additions and 21 deletions
+2 -2
View File
@@ -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'
+3 -4
View File
@@ -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); }
}
+11 -9
View File
@@ -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());
}