1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-04-01 12:36:42 +00:00

Merge pull request #2389 from akortunov/fmt

Introduce a custom format() function
This commit is contained in:
Alexei Dobrohotov 2019-05-21 21:45:52 +03:00 committed by GitHub
commit 7c46102f0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 80 additions and 76 deletions

View file

@ -12,7 +12,7 @@
#include <components/esm/loadspel.hpp> #include <components/esm/loadspel.hpp>
#include <components/esm/loadweap.hpp> #include <components/esm/loadweap.hpp>
#include <boost/format.hpp> #include <components/misc/stringops.hpp>
std::string bodyPartLabel(int idx) std::string bodyPartLabel(int idx)
{ {
@ -659,7 +659,7 @@ std::string bodyPartFlags(int flags)
(ESM::BodyPart::BPF_Female| (ESM::BodyPart::BPF_Female|
ESM::BodyPart::BPF_NotPlayable)); ESM::BodyPart::BPF_NotPlayable));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -680,7 +680,7 @@ std::string cellFlags(int flags)
ESM::Cell::QuasiEx| ESM::Cell::QuasiEx|
0x00000040)); 0x00000040));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -696,7 +696,7 @@ std::string containerFlags(int flags)
ESM::Container::Organic| ESM::Container::Organic|
ESM::Container::Respawn)); ESM::Container::Respawn));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -722,7 +722,7 @@ std::string creatureFlags(int flags)
ESM::Creature::Weapon| ESM::Creature::Weapon|
ESM::Creature::Essential)); ESM::Creature::Essential));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%02X)") % flags); properties += Misc::StringUtils::format("(0x%02X)", flags);
return properties; return properties;
} }
@ -737,7 +737,7 @@ std::string landFlags(int flags)
if (flags & 0x00000004) properties += "Unknown3 "; if (flags & 0x00000004) properties += "Unknown3 ";
if (flags & 0x00000002) properties += "Unknown2 "; if (flags & 0x00000002) properties += "Unknown2 ";
if (flags & 0xFFFFFFF8) properties += "Invalid "; if (flags & 0xFFFFFFF8) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -751,7 +751,7 @@ std::string itemListFlags(int flags)
(ESM::ItemLevList::AllLevels| (ESM::ItemLevList::AllLevels|
ESM::ItemLevList::Each)); ESM::ItemLevList::Each));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -762,7 +762,7 @@ std::string creatureListFlags(int flags)
if (flags & ESM::CreatureLevList::AllLevels) properties += "AllLevels "; if (flags & ESM::CreatureLevList::AllLevels) properties += "AllLevels ";
int unused = (0xFFFFFFFF ^ ESM::CreatureLevList::AllLevels); int unused = (0xFFFFFFFF ^ ESM::CreatureLevList::AllLevels);
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -790,7 +790,7 @@ std::string lightFlags(int flags)
ESM::Light::Negative| ESM::Light::Negative|
ESM::Light::OffDefault)); ESM::Light::OffDefault));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -816,7 +816,7 @@ std::string magicEffectFlags(int flags)
if (flags & ESM::MagicEffect::NegativeLight) properties += "NegativeLight "; if (flags & ESM::MagicEffect::NegativeLight) properties += "NegativeLight ";
if (flags & 0xFFFC0000) properties += "Invalid "; if (flags & 0xFFFC0000) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -838,7 +838,7 @@ std::string npcFlags(int flags)
ESM::NPC::Respawn| ESM::NPC::Respawn|
ESM::NPC::Essential)); ESM::NPC::Essential));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%02X)") % flags); properties += Misc::StringUtils::format("(0x%02X)", flags);
return properties; return properties;
} }
@ -853,7 +853,7 @@ std::string raceFlags(int flags)
(ESM::Race::Playable| (ESM::Race::Playable|
ESM::Race::Beast)); ESM::Race::Beast));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -869,7 +869,7 @@ std::string spellFlags(int flags)
ESM::Spell::F_PCStart| ESM::Spell::F_PCStart|
ESM::Spell::F_Always)); ESM::Spell::F_Always));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }
@ -886,6 +886,6 @@ std::string weaponFlags(int flags)
(ESM::Weapon::Magical| (ESM::Weapon::Magical|
ESM::Weapon::Silver)); ESM::Weapon::Silver));
if (flags & unused) properties += "Invalid "; if (flags & unused) properties += "Invalid ";
properties += str(boost::format("(0x%08X)") % flags); properties += Misc::StringUtils::format("(0x%08X)", flags);
return properties; return properties;
} }

View file

@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <boost/format.hpp> #include <components/misc/stringops.hpp>
namespace namespace
{ {
@ -12,7 +12,7 @@ namespace
void printAIPackage(ESM::AIPackage p) void printAIPackage(ESM::AIPackage p)
{ {
std::cout << " AI Type: " << aiTypeLabel(p.mType) std::cout << " AI Type: " << aiTypeLabel(p.mType)
<< " (" << boost::format("0x%08X") % p.mType << ")" << std::endl; << " (" << Misc::StringUtils::format("0x%08X", p.mType) << ")" << std::endl;
if (p.mType == ESM::AI_Wander) if (p.mType == ESM::AI_Wander)
{ {
std::cout << " Distance: " << p.mWander.mDistance << std::endl; std::cout << " Distance: " << p.mWander.mDistance << std::endl;
@ -46,7 +46,7 @@ void printAIPackage(ESM::AIPackage p)
std::cout << " Activate Unknown: " << p.mActivate.mUnk << std::endl; std::cout << " Activate Unknown: " << p.mActivate.mUnk << std::endl;
} }
else { else {
std::cout << " BadPackage: " << boost::format("0x%08x") % p.mType << std::endl; std::cout << " BadPackage: " << Misc::StringUtils::format("0x%08X", p.mType) << std::endl;
} }
if (!p.mCellName.empty()) if (!p.mCellName.empty())
@ -64,7 +64,7 @@ std::string ruleString(ESM::DialInfo::SelectStruct ss)
char indicator = rule[2]; char indicator = rule[2];
std::string type_str = "INVALID"; std::string type_str = "INVALID";
std::string func_str = str(boost::format("INVALID=%s") % rule.substr(1,3)); std::string func_str = Misc::StringUtils::format("INVALID=%s", rule.substr(1,3));
int func; int func;
std::istringstream iss(rule.substr(2,2)); std::istringstream iss(rule.substr(2,2));
iss >> func; iss >> func;
@ -104,7 +104,7 @@ std::string ruleString(ESM::DialInfo::SelectStruct ss)
// for all types not qual to one. If this wasn't true, go back to // for all types not qual to one. If this wasn't true, go back to
// the error message. // the error message.
if (type != '1' && rule[3] != 'X') if (type != '1' && rule[3] != 'X')
func_str = str(boost::format("INVALID=%s") % rule.substr(1,3)); func_str = Misc::StringUtils::format("INVALID=%s", rule.substr(1,3));
char oper = rule[4]; char oper = rule[4];
std::string oper_str = "??"; std::string oper_str = "??";
@ -122,8 +122,7 @@ std::string ruleString(ESM::DialInfo::SelectStruct ss)
std::ostringstream stream; std::ostringstream stream;
stream << ss.mValue; stream << ss.mValue;
std::string result = str(boost::format("%-12s %-32s %2s %s") std::string result = Misc::StringUtils::format("%-12s %-32s %2s %s", type_str, func_str, oper_str, stream.str());
% type_str % func_str % oper_str % stream.str());
return result; return result;
} }
@ -156,13 +155,13 @@ void printTransport(const std::vector<ESM::Transport::Dest>& transport)
for (const ESM::Transport::Dest& dest : transport) for (const ESM::Transport::Dest& dest : transport)
{ {
std::cout << " Destination Position: " std::cout << " Destination Position: "
<< boost::format("%12.3f") % dest.mPos.pos[0] << "," << Misc::StringUtils::format("%12.3f", dest.mPos.pos[0]) << ","
<< boost::format("%12.3f") % dest.mPos.pos[1] << "," << Misc::StringUtils::format("%12.3f", dest.mPos.pos[1]) << ","
<< boost::format("%12.3f") % dest.mPos.pos[2] << ")" << std::endl; << Misc::StringUtils::format("%12.3f", dest.mPos.pos[2]) << ")" << std::endl;
std::cout << " Destination Rotation: " std::cout << " Destination Rotation: "
<< boost::format("%9.6f") % dest.mPos.rot[0] << "," << Misc::StringUtils::format("%9.6f", dest.mPos.rot[0]) << ","
<< boost::format("%9.6f") % dest.mPos.rot[1] << "," << Misc::StringUtils::format("%9.6f", dest.mPos.rot[1]) << ","
<< boost::format("%9.6f") % dest.mPos.rot[2] << ")" << std::endl; << Misc::StringUtils::format("%9.6f", dest.mPos.rot[2]) << ")" << std::endl;
if (!dest.mCellName.empty()) if (!dest.mCellName.empty())
std::cout << " Destination Cell: " << dest.mCellName << std::endl; std::cout << " Destination Cell: " << dest.mCellName << std::endl;
} }
@ -542,7 +541,7 @@ void Record<ESM::Cell>::print()
std::cout << " Water Level: " << mData.mWater << std::endl; std::cout << " Water Level: " << mData.mWater << std::endl;
} }
else else
std::cout << " Map Color: " << boost::format("0x%08X") % mData.mMapColor << std::endl; std::cout << " Map Color: " << Misc::StringUtils::format("0x%08X", mData.mMapColor) << std::endl;
std::cout << " Water Level Int: " << mData.mWaterInt << std::endl; std::cout << " Water Level Int: " << mData.mWaterInt << std::endl;
std::cout << " RefId counter: " << mData.mRefNumCounter << std::endl; std::cout << " RefId counter: " << mData.mRefNumCounter << std::endl;
std::cout << " Deleted: " << mIsDeleted << std::endl; std::cout << " Deleted: " << mIsDeleted << std::endl;
@ -607,7 +606,7 @@ void Record<ESM::Container>::print()
std::cout << " Flags: " << containerFlags(mData.mFlags) << std::endl; std::cout << " Flags: " << containerFlags(mData.mFlags) << std::endl;
std::cout << " Weight: " << mData.mWeight << std::endl; std::cout << " Weight: " << mData.mWeight << std::endl;
for (const ESM::ContItem &item : mData.mInventory.mList) for (const ESM::ContItem &item : mData.mInventory.mList)
std::cout << " Inventory: Count: " << boost::format("%4d") % item.mCount std::cout << " Inventory: Count: " << Misc::StringUtils::format("%4d", item.mCount)
<< " Item: " << item.mItem.toString() << std::endl; << " Item: " << item.mItem.toString() << std::endl;
std::cout << " Deleted: " << mIsDeleted << std::endl; std::cout << " Deleted: " << mIsDeleted << std::endl;
} }
@ -653,7 +652,7 @@ void Record<ESM::Creature>::print()
std::cout << " Gold: " << mData.mData.mGold << std::endl; std::cout << " Gold: " << mData.mData.mGold << std::endl;
for (const ESM::ContItem &item : mData.mInventory.mList) for (const ESM::ContItem &item : mData.mInventory.mList)
std::cout << " Inventory: Count: " << boost::format("%4d") % item.mCount std::cout << " Inventory: Count: " << Misc::StringUtils::format("%4d", item.mCount)
<< " Item: " << item.mItem.toString() << std::endl; << " Item: " << item.mItem.toString() << std::endl;
for (const std::string &spell : mData.mSpells.mList) for (const std::string &spell : mData.mSpells.mList)
@ -669,7 +668,7 @@ void Record<ESM::Creature>::print()
std::cout << " AI U1:" << (int)mData.mAiData.mU1 << std::endl; std::cout << " AI U1:" << (int)mData.mAiData.mU1 << std::endl;
std::cout << " AI U2:" << (int)mData.mAiData.mU2 << std::endl; std::cout << " AI U2:" << (int)mData.mAiData.mU2 << std::endl;
std::cout << " AI U3:" << (int)mData.mAiData.mU3 << std::endl; std::cout << " AI U3:" << (int)mData.mAiData.mU3 << std::endl;
std::cout << " AI Services:" << boost::format("0x%08X") % mData.mAiData.mServices << std::endl; std::cout << " AI Services:" << Misc::StringUtils::format("0x%08X", mData.mAiData.mServices) << std::endl;
for (const ESM::AIPackage &package : mData.mAiPackage.mList) for (const ESM::AIPackage &package : mData.mAiPackage.mList)
printAIPackage(package); printAIPackage(package);
@ -1073,7 +1072,7 @@ void Record<ESM::NPC>::print()
} }
for (const ESM::ContItem &item : mData.mInventory.mList) for (const ESM::ContItem &item : mData.mInventory.mList)
std::cout << " Inventory: Count: " << boost::format("%4d") % item.mCount std::cout << " Inventory: Count: " << Misc::StringUtils::format("%4d", item.mCount)
<< " Item: " << item.mItem.toString() << std::endl; << " Item: " << item.mItem.toString() << std::endl;
for (const std::string &spell : mData.mSpells.mList) for (const std::string &spell : mData.mSpells.mList)
@ -1089,7 +1088,7 @@ void Record<ESM::NPC>::print()
std::cout << " AI U1:" << (int)mData.mAiData.mU1 << std::endl; std::cout << " AI U1:" << (int)mData.mAiData.mU1 << std::endl;
std::cout << " AI U2:" << (int)mData.mAiData.mU2 << std::endl; std::cout << " AI U2:" << (int)mData.mAiData.mU2 << std::endl;
std::cout << " AI U3:" << (int)mData.mAiData.mU3 << std::endl; std::cout << " AI U3:" << (int)mData.mAiData.mU3 << std::endl;
std::cout << " AI Services:" << boost::format("0x%08X") % mData.mAiData.mServices << std::endl; std::cout << " AI Services:" << Misc::StringUtils::format("0x%08X", mData.mAiData.mServices) << std::endl;
for (const ESM::AIPackage &package : mData.mAiPackage.mList) for (const ESM::AIPackage &package : mData.mAiPackage.mList)
printAIPackage(package); printAIPackage(package);
@ -1212,7 +1211,7 @@ void Record<ESM::Script>::print()
std::cout << " ByteCode: "; std::cout << " ByteCode: ";
for (const unsigned char &byte : mData.mScriptData) for (const unsigned char &byte : mData.mScriptData)
std::cout << boost::format("%02X") % (int)(byte); std::cout << Misc::StringUtils::format("%02X", (int)(byte));
std::cout << std::endl; std::cout << std::endl;
if (mPrintPlain) if (mPrintPlain)

View file

@ -340,7 +340,7 @@ namespace MWGui
if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(item.getCellRef().getRefId(), mPtr)) if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(item.getCellRef().getRefId(), mPtr))
{ {
std::string msg = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("sNotifyMessage49")->mValue.getString(); std::string msg = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("sNotifyMessage49")->mValue.getString();
Misc::StringUtils::replace(msg, "%s", item.getClass().getName(item).c_str(), 2); msg = Misc::StringUtils::format(msg, item.getClass().getName(item));
MWBase::Environment::get().getWindowManager()->messageBox(msg); MWBase::Environment::get().getWindowManager()->messageBox(msg);
MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, item, mPtr, 1); MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, item, mPtr, 1);

View file

@ -102,7 +102,7 @@ namespace MWGui
else else
message = gmst.find("sNotifyMessage43")->mValue.getString(); message = gmst.find("sNotifyMessage43")->mValue.getString();
Misc::StringUtils::replace(message, "%d", std::to_string(mDays).c_str(), 2); message = Misc::StringUtils::format(message, mDays);
for (const int& skill : skills) for (const int& skill : skills)
{ {
@ -112,8 +112,7 @@ namespace MWGui
if (skill == ESM::Skill::Sneak || skill == ESM::Skill::Security) if (skill == ESM::Skill::Sneak || skill == ESM::Skill::Security)
skillMsg = gmst.find("sNotifyMessage39")->mValue.getString(); skillMsg = gmst.find("sNotifyMessage39")->mValue.getString();
Misc::StringUtils::replace(skillMsg, "%s", skillName.c_str(), 2); skillMsg = Misc::StringUtils::format(skillMsg, skillName, skillValue);
Misc::StringUtils::replace(skillMsg, "%d", std::to_string(skillValue).c_str(), 2);
message += "\n" + skillMsg; message += "\n" + skillMsg;
} }

View file

@ -177,7 +177,7 @@ void Recharge::onItemClicked(MyGUI::Widget *sender, const MWWorld::Ptr& item)
if (gem.getRefData().getCount() == 0) if (gem.getRefData().getCount() == 0)
{ {
std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("sNotifyMessage51")->mValue.getString(); std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("sNotifyMessage51")->mValue.getString();
Misc::StringUtils::replace(message, "%s", gem.getClass().getName(gem).c_str(), 2); message = Misc::StringUtils::format(message, gem.getClass().getName(gem));
MWBase::Environment::get().getWindowManager()->messageBox(message); MWBase::Environment::get().getWindowManager()->messageBox(message);

View file

@ -175,7 +175,7 @@ namespace MWGui
MyGUI::TextBox* textBox; MyGUI::TextBox* textBox;
getWidget(textBox, labelWidgetName); getWidget(textBox, labelWidgetName);
std::string labelCaption = scroller->getUserString("SettingLabelCaption"); std::string labelCaption = scroller->getUserString("SettingLabelCaption");
Misc::StringUtils::replaceAll(labelCaption, "%s", value.c_str(), 2); labelCaption = Misc::StringUtils::format(labelCaption, value);
textBox->setCaptionWithReplacing(labelCaption); textBox->setCaptionWithReplacing(labelCaption);
} }
} }

View file

@ -172,7 +172,7 @@ namespace MWGui
mSpellToDelete = spellId; mSpellToDelete = spellId;
ConfirmationDialog* dialog = MWBase::Environment::get().getWindowManager()->getConfirmationDialog(); ConfirmationDialog* dialog = MWBase::Environment::get().getWindowManager()->getConfirmationDialog();
std::string question = MWBase::Environment::get().getWindowManager()->getGameSettingString("sQuestionDeleteSpell", "Delete %s?"); std::string question = MWBase::Environment::get().getWindowManager()->getGameSettingString("sQuestionDeleteSpell", "Delete %s?");
Misc::StringUtils::replace(question, "%s", spell->mName.c_str(), 2); question = Misc::StringUtils::format(question, spell->mName);
dialog->askForConfirmation(question); dialog->askForConfirmation(question);
dialog->eventOkClicked.clear(); dialog->eventOkClicked.clear();
dialog->eventOkClicked += MyGUI::newDelegate(this, &SpellWindow::onDeleteSpellAccept); dialog->eventOkClicked += MyGUI::newDelegate(this, &SpellWindow::onDeleteSpellAccept);

View file

@ -309,7 +309,7 @@ namespace MWGui
if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(itemStack.mBase.getCellRef().getRefId(), mPtr)) if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(itemStack.mBase.getCellRef().getRefId(), mPtr))
{ {
std::string msg = gmst.find("sNotifyMessage49")->mValue.getString(); std::string msg = gmst.find("sNotifyMessage49")->mValue.getString();
Misc::StringUtils::replace(msg, "%s", itemStack.mBase.getClass().getName(itemStack.mBase).c_str(), 2); msg = Misc::StringUtils::format(msg, itemStack.mBase.getClass().getName(itemStack.mBase));
MWBase::Environment::get().getWindowManager()->messageBox(msg); MWBase::Environment::get().getWindowManager()->messageBox(msg);
MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, itemStack.mBase, mPtr, itemStack.mCount); MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, itemStack.mBase, mPtr, itemStack.mCount);

View file

@ -61,7 +61,7 @@ namespace MWMechanics
std::string msg = "sMagicContractDisease"; std::string msg = "sMagicContractDisease";
msg = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find(msg)->mValue.getString(); msg = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find(msg)->mValue.getString();
Misc::StringUtils::replace(msg, "%s", spell->mName.c_str(), 2); msg = Misc::StringUtils::format(msg, spell->mName);
MWBase::Environment::get().getWindowManager()->messageBox(msg); MWBase::Environment::get().getWindowManager()->messageBox(msg);
} }
} }

View file

@ -265,8 +265,7 @@ void MWMechanics::NpcStats::increaseSkill(int skillIndex, const ESM::Class &clas
MWBase::Environment::get().getWindowManager()->playSound("skillraise"); MWBase::Environment::get().getWindowManager()->playSound("skillraise");
std::string message = MWBase::Environment::get().getWindowManager ()->getGameSettingString ("sNotifyMessage39", ""); std::string message = MWBase::Environment::get().getWindowManager ()->getGameSettingString ("sNotifyMessage39", "");
Misc::StringUtils::replace(message, "%s", ("#{" + ESM::Skill::sSkillNameIds[skillIndex] + "}").c_str(), 2); message = Misc::StringUtils::format(message, ("#{" + ESM::Skill::sSkillNameIds[skillIndex] + "}"), base);
Misc::StringUtils::replace(message, "%d", std::to_string(base).c_str(), 2);
if (readBook) if (readBook)
message = "#{sBookSkillMessage}\n" + message; message = "#{sBookSkillMessage}\n" + message;

View file

@ -83,7 +83,7 @@ void Repair::repair(const MWWorld::Ptr &itemToRepair)
std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>() std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
.find("sNotifyMessage51")->mValue.getString(); .find("sNotifyMessage51")->mValue.getString();
Misc::StringUtils::replace(message, "%s", mTool.getClass().getName(mTool).c_str(), 2); message = Misc::StringUtils::format(message, mTool.getClass().getName(mTool));
MWBase::Environment::get().getWindowManager()->messageBox(message); MWBase::Environment::get().getWindowManager()->messageBox(message);

View file

@ -1012,7 +1012,7 @@ namespace MWMechanics
{ {
// "X has no effect on you" // "X has no effect on you"
std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("sNotifyMessage50")->mValue.getString(); std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("sNotifyMessage50")->mValue.getString();
Misc::StringUtils::replace(message, "%s", ingredient->mName.c_str(), 2); message = Misc::StringUtils::format(message, ingredient->mName);
MWBase::Environment::get().getWindowManager()->messageBox(message); MWBase::Environment::get().getWindowManager()->messageBox(message);
return false; return false;
} }

View file

@ -83,13 +83,13 @@ namespace MWScript
if (count == 1) if (count == 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}");
msgBox = ::Misc::StringUtils::format(msgBox, itemName);
} }
else else
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}");
::Misc::StringUtils::replace(msgBox, "%d", std::to_string(count).c_str(), 2); msgBox = ::Misc::StringUtils::format(msgBox, count, itemName);
} }
::Misc::StringUtils::replace(msgBox, "%s", itemName.c_str(), 2);
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, MWGui::ShowInDialogueMode_Only); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, MWGui::ShowInDialogueMode_Only);
} }
} }
@ -171,13 +171,13 @@ namespace MWScript
if (numRemoved > 1) if (numRemoved > 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}");
::Misc::StringUtils::replace(msgBox, "%d", std::to_string(numRemoved).c_str(), 2); msgBox = ::Misc::StringUtils::format(msgBox, numRemoved, itemName);
} }
else else
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}");
msgBox = ::Misc::StringUtils::format(msgBox, itemName);
} }
::Misc::StringUtils::replace(msgBox, "%s", itemName.c_str(), 2);
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, MWGui::ShowInDialogueMode_Only); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, MWGui::ShowInDialogueMode_Only);
} }
} }

View file

@ -66,13 +66,12 @@ namespace MWWorld
if (itemCount == 1) if (itemCount == 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("\n#{sNotifyMessage60}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("\n#{sNotifyMessage60}");
Misc::StringUtils::replace(msgBox, "%s", itemName.c_str(), 2); msgBox = Misc::StringUtils::format(msgBox, itemName);
} }
else else
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("\n#{sNotifyMessage61}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("\n#{sNotifyMessage61}");
Misc::StringUtils::replace(msgBox, "%d", std::to_string(itemCount).c_str(), 2); msgBox = Misc::StringUtils::format(msgBox, itemCount, itemName);
Misc::StringUtils::replace(msgBox, "%s", itemName.c_str(), 2);
} }
stream << msgBox; stream << msgBox;

View file

@ -1,7 +1,6 @@
#ifndef MISC_STRINGOPS_H #ifndef MISC_STRINGOPS_H
#define MISC_STRINGOPS_H #define MISC_STRINGOPS_H
#include <cstring>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
@ -18,6 +17,19 @@ class StringUtils
} }
}; };
// Allow to convert complex arguments to C-style strings for format() function
template <typename T>
static T argument(T value) noexcept
{
return value;
}
template <typename T>
static T const * argument(std::basic_string<T> const & value) noexcept
{
return value.c_str();
}
public: public:
/// Plain and simple locale-unaware toLower. Anything from A to Z is lower-cased, multibyte characters are unchanged. /// Plain and simple locale-unaware toLower. Anything from A to Z is lower-cased, multibyte characters are unchanged.
@ -212,30 +224,26 @@ public:
return str; return str;
} }
/** @brief Replaces the first occurrence of a string in another string. // Requires some C++11 features:
* // 1. std::string needs to be contiguous
* @param str The string to operate on. // 2. std::snprintf with zero size (second argument) returns an output string size
* @param what The string to replace. // 3. variadic templates support
* @param with The replacement string. template <typename ... Args>
* @param whatLen The length of the string to replace. static std::string format(const char* fmt, Args const & ... args)
* @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) auto size = std::snprintf(nullptr, 0, fmt, argument(args) ...);
whatLen = strlen(what); // Note: sprintf also writes a trailing null character. We should remove it.
std::string ret(size+1, '\0');
std::sprintf(&ret[0], fmt, argument(args) ...);
ret.erase(size);
if (withLen == std::string::npos) return ret;
withLen = strlen(with); }
std::size_t found; template <typename ... Args>
if ((found = str.find(what)) != std::string::npos) static std::string format(const std::string& fmt, Args const & ... args)
str.replace(found, whatLen, with, withLen); {
return format(fmt.c_str(), args ...);
return str;
} }
}; };