From 8ecd0b82a4604d7bc5e34b663c218c9c005dd34b Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Sat, 23 Feb 2019 00:14:07 +0300 Subject: [PATCH 1/2] Replace Boost format and replace_all where possible --- apps/openmw/mwgui/enchantingdialog.cpp | 3 +-- apps/openmw/mwgui/formatting.cpp | 4 +-- apps/openmw/mwgui/recharge.cpp | 5 ++-- apps/openmw/mwgui/settingswindow.cpp | 3 ++- apps/openmw/mwgui/spellwindow.cpp | 5 ++-- apps/openmw/mwgui/tradewindow.cpp | 3 +-- apps/openmw/mwmechanics/disease.hpp | 3 +-- apps/openmw/mwmechanics/npcstats.cpp | 14 ++++------- apps/openmw/mwmechanics/repair.cpp | 5 ++-- apps/openmw/mwmechanics/spellcasting.cpp | 4 +-- apps/openmw/mwscript/containerextensions.cpp | 12 ++++----- components/misc/stringops.hpp | 26 ++++++++++++++++++++ 12 files changed, 49 insertions(+), 38 deletions(-) diff --git a/apps/openmw/mwgui/enchantingdialog.cpp b/apps/openmw/mwgui/enchantingdialog.cpp index af6567afb7..fbdc1bf806 100644 --- a/apps/openmw/mwgui/enchantingdialog.cpp +++ b/apps/openmw/mwgui/enchantingdialog.cpp @@ -348,8 +348,7 @@ namespace MWGui if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(item.getCellRef().getRefId(), mPtr)) { std::string msg = MWBase::Environment::get().getWorld()->getStore().get().find("sNotifyMessage49")->mValue.getString(); - if (msg.find("%s") != std::string::npos) - msg.replace(msg.find("%s"), 2, item.getClass().getName(item)); + Misc::StringUtils::replace(msg, "%s", item.getClass().getName(item).c_str(), 2); MWBase::Environment::get().getWindowManager()->messageBox(msg); MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, item, mPtr, 1); diff --git a/apps/openmw/mwgui/formatting.cpp b/apps/openmw/mwgui/formatting.cpp index d4d6a0af56..fca05b05cd 100644 --- a/apps/openmw/mwgui/formatting.cpp +++ b/apps/openmw/mwgui/formatting.cpp @@ -9,8 +9,6 @@ #include "../mwbase/environment.hpp" #include "../mwbase/windowmanager.hpp" -#include - #include #include #include @@ -28,7 +26,7 @@ namespace MWGui MWScript::InterpreterContext interpreterContext(nullptr, MWWorld::Ptr()); // empty arguments, because there is no locals or actor mText = Interpreter::fixDefinesBook(mText, interpreterContext); - boost::algorithm::replace_all(mText, "\r", ""); + Misc::StringUtils::replaceAll(mText, "\r", ""); // vanilla game does not show any text after the last EOL tag. const std::string lowerText = Misc::StringUtils::lowerCase(mText); diff --git a/apps/openmw/mwgui/recharge.cpp b/apps/openmw/mwgui/recharge.cpp index 56bd97d5e3..e42db715bc 100644 --- a/apps/openmw/mwgui/recharge.cpp +++ b/apps/openmw/mwgui/recharge.cpp @@ -1,7 +1,5 @@ #include "recharge.hpp" -#include - #include #include @@ -179,7 +177,8 @@ void Recharge::onItemClicked(MyGUI::Widget *sender, const MWWorld::Ptr& item) if (gem.getRefData().getCount() == 0) { std::string message = MWBase::Environment::get().getWorld()->getStore().get().find("sNotifyMessage51")->mValue.getString(); - message = boost::str(boost::format(message) % gem.getClass().getName(gem)); + Misc::StringUtils::replace(message, "%s", gem.getClass().getName(gem).c_str(), 2); + MWBase::Environment::get().getWindowManager()->messageBox(message); // special case: readd Azura's Star diff --git a/apps/openmw/mwgui/settingswindow.cpp b/apps/openmw/mwgui/settingswindow.cpp index 6e6924f28e..c41d43b315 100644 --- a/apps/openmw/mwgui/settingswindow.cpp +++ b/apps/openmw/mwgui/settingswindow.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -159,7 +160,7 @@ namespace MWGui MyGUI::TextBox* textBox; getWidget(textBox, labelWidgetName); std::string labelCaption = scroller->getUserString("SettingLabelCaption"); - boost::algorithm::replace_all(labelCaption, "%s", value); + Misc::StringUtils::replaceAll(labelCaption, "%s", value.c_str(), 2); textBox->setCaptionWithReplacing(labelCaption); } } diff --git a/apps/openmw/mwgui/spellwindow.cpp b/apps/openmw/mwgui/spellwindow.cpp index 2177e9e2af..254e731d0e 100644 --- a/apps/openmw/mwgui/spellwindow.cpp +++ b/apps/openmw/mwgui/spellwindow.cpp @@ -1,10 +1,9 @@ #include "spellwindow.hpp" -#include - #include #include +#include #include #include "../mwbase/windowmanager.hpp" @@ -157,7 +156,7 @@ namespace MWGui mSpellToDelete = spellId; ConfirmationDialog* dialog = MWBase::Environment::get().getWindowManager()->getConfirmationDialog(); std::string question = MWBase::Environment::get().getWindowManager()->getGameSettingString("sQuestionDeleteSpell", "Delete %s?"); - question = boost::str(boost::format(question) % spell->mName); + Misc::StringUtils::replace(question, "%s", spell->mName.c_str(), 2); dialog->askForConfirmation(question); dialog->eventOkClicked.clear(); dialog->eventOkClicked += MyGUI::newDelegate(this, &SpellWindow::onDeleteSpellAccept); diff --git a/apps/openmw/mwgui/tradewindow.cpp b/apps/openmw/mwgui/tradewindow.cpp index 90698dfc4f..b21257eaf7 100644 --- a/apps/openmw/mwgui/tradewindow.cpp +++ b/apps/openmw/mwgui/tradewindow.cpp @@ -311,8 +311,7 @@ namespace MWGui if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(it->mBase.getCellRef().getRefId(), mPtr)) { std::string msg = gmst.find("sNotifyMessage49")->mValue.getString(); - if (msg.find("%s") != std::string::npos) - msg.replace(msg.find("%s"), 2, it->mBase.getClass().getName(it->mBase)); + Misc::StringUtils::replace(msg, "%s", it->mBase.getClass().getName(it->mBase).c_str(), 2); MWBase::Environment::get().getWindowManager()->messageBox(msg); MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, it->mBase, mPtr, it->mCount); diff --git a/apps/openmw/mwmechanics/disease.hpp b/apps/openmw/mwmechanics/disease.hpp index fec3bdd376..8bca9f9b5b 100644 --- a/apps/openmw/mwmechanics/disease.hpp +++ b/apps/openmw/mwmechanics/disease.hpp @@ -60,8 +60,7 @@ namespace MWMechanics std::string msg = "sMagicContractDisease"; msg = MWBase::Environment::get().getWorld()->getStore().get().find(msg)->mValue.getString(); - if (msg.find("%s") != std::string::npos) - msg.replace(msg.find("%s"), 2, spell->mName); + Misc::StringUtils::replace(msg, "%s", spell->mName.c_str(), 2); MWBase::Environment::get().getWindowManager()->messageBox(msg); } } diff --git a/apps/openmw/mwmechanics/npcstats.cpp b/apps/openmw/mwmechanics/npcstats.cpp index 70c2004f02..df61ee93b4 100644 --- a/apps/openmw/mwmechanics/npcstats.cpp +++ b/apps/openmw/mwmechanics/npcstats.cpp @@ -2,8 +2,6 @@ #include -#include - #include #include #include @@ -251,16 +249,14 @@ void MWMechanics::NpcStats::increaseSkill(int skillIndex, const ESM::Class &clas /// \todo check if character is the player, if levelling is ever implemented for NPCs MWBase::Environment::get().getWindowManager()->playSound("skillraise"); - std::stringstream message; + std::string message = MWBase::Environment::get().getWindowManager ()->getGameSettingString ("sNotifyMessage39", ""); + Misc::StringUtils::replace(message, "%s", ("#{" + ESM::Skill::sSkillNameIds[skillIndex] + "}").c_str(), 2); + Misc::StringUtils::replace(message, "%d", std::to_string(base).c_str(), 2); if (readBook) - message << std::string("#{sBookSkillMessage}\n"); - - message << boost::format(MWBase::Environment::get().getWindowManager ()->getGameSettingString ("sNotifyMessage39", "")) - % std::string("#{" + ESM::Skill::sSkillNameIds[skillIndex] + "}") - % static_cast (base); + message = "#{sBookSkillMessage}\n" + message; - MWBase::Environment::get().getWindowManager ()->messageBox(message.str(), MWGui::ShowInDialogueMode_Never); + MWBase::Environment::get().getWindowManager ()->messageBox(message, MWGui::ShowInDialogueMode_Never); if (mLevelProgress >= gmst.find("iLevelUpTotal")->mValue.getInteger()) { diff --git a/apps/openmw/mwmechanics/repair.cpp b/apps/openmw/mwmechanics/repair.cpp index 8ab3c605d2..28dfab0cdc 100644 --- a/apps/openmw/mwmechanics/repair.cpp +++ b/apps/openmw/mwmechanics/repair.cpp @@ -1,7 +1,5 @@ #include "repair.hpp" -#include - #include #include "../mwbase/world.hpp" @@ -85,8 +83,9 @@ void Repair::repair(const MWWorld::Ptr &itemToRepair) std::string message = MWBase::Environment::get().getWorld()->getStore().get() .find("sNotifyMessage51")->mValue.getString(); + Misc::StringUtils::replace(message, "%s", mTool.getClass().getName(mTool).c_str(), 2); - MWBase::Environment::get().getWindowManager()->messageBox((boost::format(message) % mTool.getClass().getName(mTool)).str()); + MWBase::Environment::get().getWindowManager()->messageBox(message); // try to find a new tool of the same ID for (MWWorld::ContainerStoreIterator iter (store.begin()); diff --git a/apps/openmw/mwmechanics/spellcasting.cpp b/apps/openmw/mwmechanics/spellcasting.cpp index 2a359cb52c..7b406cf9ae 100644 --- a/apps/openmw/mwmechanics/spellcasting.cpp +++ b/apps/openmw/mwmechanics/spellcasting.cpp @@ -3,8 +3,6 @@ #include #include -#include - #include #include @@ -1015,7 +1013,7 @@ namespace MWMechanics { // "X has no effect on you" std::string message = MWBase::Environment::get().getWorld()->getStore().get().find("sNotifyMessage50")->mValue.getString(); - message = boost::str(boost::format(message) % ingredient->mName); + Misc::StringUtils::replace(message, "%s", ingredient->mName.c_str(), 2); MWBase::Environment::get().getWindowManager()->messageBox(message); return false; } diff --git a/apps/openmw/mwscript/containerextensions.cpp b/apps/openmw/mwscript/containerextensions.cpp index 7d555e632a..47c46d65e9 100644 --- a/apps/openmw/mwscript/containerextensions.cpp +++ b/apps/openmw/mwscript/containerextensions.cpp @@ -2,8 +2,6 @@ #include -#include - #include #include @@ -85,13 +83,13 @@ namespace MWScript if (count == 1) { msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}"); - msgBox = boost::str(boost::format(msgBox) % itemName); } else { msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}"); - msgBox = boost::str(boost::format(msgBox) % count % itemName); + Misc::StringUtils::replace(msgBox, "%d", std::to_string(count).c_str(), 2); } + Misc::StringUtils::replace(msgBox, "%s", itemName.c_str(), 2); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, MWGui::ShowInDialogueMode_Only); } } @@ -170,16 +168,16 @@ namespace MWScript // The two GMST entries below expand to strings informing the player of what, and how many of it has been removed from their inventory std::string msgBox; - if(numRemoved > 1) + if (numRemoved > 1) { msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}"); - msgBox = boost::str (boost::format(msgBox) % numRemoved % itemName); + Misc::StringUtils::replace(msgBox, "%d", std::to_string(numRemoved).c_str(), 2); } else { msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}"); - msgBox = boost::str (boost::format(msgBox) % itemName); } + Misc::StringUtils::replace(msgBox, "%s", itemName.c_str(), 2); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, MWGui::ShowInDialogueMode_Only); } } diff --git a/components/misc/stringops.hpp b/components/misc/stringops.hpp index 76a585d61d..58e1651b8e 100644 --- a/components/misc/stringops.hpp +++ b/components/misc/stringops.hpp @@ -240,6 +240,32 @@ public: } return str; } + + /** @brief Replaces the first occurrence of a string in another string. + * + * @param str The string to operate on. + * @param what The string to replace. + * @param with The replacement string. + * @param whatLen The length of the string to replace. + * @param withLen The length of the replacement string. + * + * @return A reference to the string passed in @p str. + */ + static std::string &replace(std::string &str, const char *what, const char *with, + std::size_t whatLen=std::string::npos, std::size_t withLen=std::string::npos) + { + if (whatLen == std::string::npos) + whatLen = strlen(what); + + if (withLen == std::string::npos) + withLen = strlen(with); + + std::size_t found; + if ((found = str.find(what)) != std::string::npos) + str.replace(found, whatLen, with, withLen); + + return str; + } }; } From e70cc1030512d7b8bc260ec973c764d5f63e1caf Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Sat, 23 Feb 2019 16:55:49 +0300 Subject: [PATCH 2/2] Some more de-boosting --- apps/opencs/view/doc/adjusterwidget.cpp | 6 +++--- apps/openmw/mwrender/terrainstorage.cpp | 2 -- components/files/configurationmanager.cpp | 2 -- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/apps/opencs/view/doc/adjusterwidget.cpp b/apps/opencs/view/doc/adjusterwidget.cpp index 06da68ed80..509e656c33 100644 --- a/apps/opencs/view/doc/adjusterwidget.cpp +++ b/apps/opencs/view/doc/adjusterwidget.cpp @@ -1,7 +1,8 @@ #include "adjusterwidget.hpp" +#include + #include -#include #include #include @@ -70,8 +71,7 @@ void CSVDoc::AdjusterWidget::setName (const QString& name, bool addon) { boost::filesystem::path path (name.toUtf8().data()); - std::string extension = path.extension().string(); - boost::algorithm::to_lower(extension); + std::string extension = Misc::StringUtils::lowerCase(path.extension().string()); bool isLegacyPath = (extension == ".esm" || extension == ".esp"); diff --git a/apps/openmw/mwrender/terrainstorage.cpp b/apps/openmw/mwrender/terrainstorage.cpp index ff2a8bfc78..7894a8393f 100644 --- a/apps/openmw/mwrender/terrainstorage.cpp +++ b/apps/openmw/mwrender/terrainstorage.cpp @@ -1,7 +1,5 @@ #include "terrainstorage.hpp" -#include - #include "../mwbase/world.hpp" #include "../mwbase/environment.hpp" #include "../mwworld/esmstore.hpp" diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index c58130f963..3df6faf623 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -3,8 +3,6 @@ #include #include -#include -#include #include /** * \namespace Files