From 999db2ed78c339000ec340cc21401792958cca76 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sun, 10 Dec 2023 16:33:22 +0200 Subject: [PATCH] Avoid boost::format that throws exception on invalid format string --- lib/MetaString.cpp | 5 ++++- lib/mapObjects/CGTownInstance.cpp | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/MetaString.cpp b/lib/MetaString.cpp index 8fdb6d3a0..2845a6f38 100644 --- a/lib/MetaString.cpp +++ b/lib/MetaString.cpp @@ -168,7 +168,10 @@ DLL_LINKAGE std::string MetaString::toString() const boost::replace_first(dst, "%d", std::to_string(numbers[nums++])); break; case EMessage::REPLACE_POSITIVE_NUMBER: - boost::replace_first(dst, "%+d", '+' + std::to_string(numbers[nums++])); + if (dst.find("%+d") != std::string::npos) + boost::replace_first(dst, "%+d", '+' + std::to_string(numbers[nums++])); + else + boost::replace_first(dst, "%d", std::to_string(numbers[nums++])); break; default: logGlobal->error("MetaString processing error! Received message of type %d", static_cast(elem)); diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index ab3fe7cd7..0f7c49bcf 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -1224,12 +1224,21 @@ TerrainId CGTownInstance::getNativeTerrain() const GrowthInfo::Entry::Entry(const std::string &format, int _count) : count(_count) { - description = boost::str(boost::format(format) % count); + MetaString formatter; + formatter.appendRawString(format); + formatter.replacePositiveNumber(count); + + description = formatter.toString(); } GrowthInfo::Entry::Entry(int subID, const BuildingID & building, int _count): count(_count) { - description = boost::str(boost::format("%s %+d") % (*VLC->townh)[subID]->town->buildings.at(building)->getNameTranslated() % count); + MetaString formatter; + formatter.appendRawString("%s %+d"); + formatter.replaceRawString((*VLC->townh)[subID]->town->buildings.at(building)->getNameTranslated()); + formatter.replacePositiveNumber(count); + + description = formatter.toString(); } GrowthInfo::Entry::Entry(int _count, std::string fullDescription):