浏览代码

Avoid boost::format that throws exception on invalid format string

Ivan Savenko 1 年之前
父节点
当前提交
999db2ed78
共有 2 个文件被更改,包括 15 次插入3 次删除
  1. 4 1
      lib/MetaString.cpp
  2. 11 2
      lib/mapObjects/CGTownInstance.cpp

+ 4 - 1
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<int>(elem));

+ 11 - 2
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):