From 492e336c0c68a8f29679488ac7cee49a598afa0b Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 21 May 2023 18:14:12 +0200 Subject: [PATCH] Use string_view in more UI code --- apps/openmw/mwgui/draganddrop.cpp | 2 +- apps/openmw/mwgui/inventorywindow.cpp | 2 +- apps/openmw/mwgui/journalviewmodel.cpp | 8 ++++---- apps/openmw/mwgui/settingswindow.cpp | 8 ++++---- apps/openmw/mwgui/spellview.cpp | 2 +- apps/openmw/mwgui/windowmanagerimp.cpp | 27 +++++++++++++------------- components/translation/translation.cpp | 16 +++++++-------- components/translation/translation.hpp | 10 +++++----- components/widgets/list.cpp | 25 ++++++++++++------------ components/widgets/list.hpp | 6 +++--- components/widgets/tags.cpp | 18 ++++++++--------- components/widgets/tags.hpp | 5 ++--- 12 files changed, 64 insertions(+), 65 deletions(-) diff --git a/apps/openmw/mwgui/draganddrop.cpp b/apps/openmw/mwgui/draganddrop.cpp index 626148c8c3..c99e97e37d 100644 --- a/apps/openmw/mwgui/draganddrop.cpp +++ b/apps/openmw/mwgui/draganddrop.cpp @@ -47,7 +47,7 @@ namespace MWGui playerModel->update(); ItemModel::ModelIndex newIndex = -1; - for (unsigned int i = 0; i < playerModel->getItemCount(); ++i) + for (size_t i = 0; i < playerModel->getItemCount(); ++i) { if (playerModel->getItem(i).mBase == item) { diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index e3f209fa2e..d96db97353 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -779,7 +779,7 @@ namespace MWGui if (selected != -1) lastId = model.getItem(selected).mBase.getCellRef().getRefId(); ItemModel::ModelIndex cycled = selected; - for (unsigned int i = 0; i < model.getItemCount(); ++i) + for (size_t i = 0; i < model.getItemCount(); ++i) { cycled += incr; cycled = (cycled + model.getItemCount()) % model.getItemCount(); diff --git a/apps/openmw/mwgui/journalviewmodel.cpp b/apps/openmw/mwgui/journalviewmodel.cpp index fd5f7b73c7..57c768aeac 100644 --- a/apps/openmw/mwgui/journalviewmodel.cpp +++ b/apps/openmw/mwgui/journalviewmodel.cpp @@ -116,10 +116,10 @@ namespace MWGui std::string link = utf8text.substr(pos_begin + 1, pos_end - pos_begin - 1); const char specialPseudoAsteriskCharacter = 127; std::replace(link.begin(), link.end(), specialPseudoAsteriskCharacter, '*'); - std::string topicName = MWBase::Environment::get() - .getWindowManager() - ->getTranslationDataStorage() - .topicStandardForm(link); + std::string_view topicName = MWBase::Environment::get() + .getWindowManager() + ->getTranslationDataStorage() + .topicStandardForm(link); std::string displayName = link; while (displayName[displayName.size() - 1] == '*') diff --git a/apps/openmw/mwgui/settingswindow.cpp b/apps/openmw/mwgui/settingswindow.cpp index 2ac12828a8..76ef5c4cdb 100644 --- a/apps/openmw/mwgui/settingswindow.cpp +++ b/apps/openmw/mwgui/settingswindow.cpp @@ -472,7 +472,7 @@ namespace MWGui void SettingsWindow::onResolutionAccept() { - std::string resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected()); + const std::string& resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected()); int resX, resY; parseResolution(resX, resY, resStr); @@ -844,7 +844,7 @@ namespace MWGui // check if this resolution is supported in fullscreen if (mResolutionList->getIndexSelected() != MyGUI::ITEM_NONE) { - std::string resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected()); + const std::string& resStr = mResolutionList->getItemNameAt(mResolutionList->getIndexSelected()); int resX, resY; parseResolution(resX, resY, resStr); Settings::Manager::setInt("resolution x", "Video", resX); @@ -853,9 +853,9 @@ namespace MWGui bool supported = false; int fallbackX = 0, fallbackY = 0; - for (unsigned int i = 0; i < mResolutionList->getItemCount(); ++i) + for (size_t i = 0; i < mResolutionList->getItemCount(); ++i) { - std::string resStr = mResolutionList->getItemNameAt(i); + const std::string& resStr = mResolutionList->getItemNameAt(i); int resX, resY; parseResolution(resX, resY, resStr); diff --git a/apps/openmw/mwgui/spellview.cpp b/apps/openmw/mwgui/spellview.cpp index 39aa0eec06..b60ec24440 100644 --- a/apps/openmw/mwgui/spellview.cpp +++ b/apps/openmw/mwgui/spellview.cpp @@ -162,7 +162,7 @@ namespace MWGui // match model against line // if don't match, then major change has happened, so do a full update - if (mModel->getItemCount() <= static_cast(spellIndex)) + if (mModel->getItemCount() <= static_cast(spellIndex)) { fullUpdateRequired = true; break; diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index 20444e5e7e..4f2979b72b 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -1096,26 +1096,25 @@ namespace MWGui void WindowManager::onRetrieveTag(const MyGUI::UString& _tag, MyGUI::UString& _result) { - std::string tag(_tag); + std::string_view tag = _tag.asUTF8(); - std::string MyGuiPrefix = "setting="; - size_t MyGuiPrefixLength = MyGuiPrefix.length(); + std::string_view MyGuiPrefix = "setting="; - std::string tokenToFind = "sCell="; - size_t tokenLength = tokenToFind.length(); + std::string_view tokenToFind = "sCell="; - if (tag.compare(0, MyGuiPrefixLength, MyGuiPrefix) == 0) + if (tag.starts_with(MyGuiPrefix)) { - tag = tag.substr(MyGuiPrefixLength, tag.length()); + tag = tag.substr(MyGuiPrefix.length()); size_t comma_pos = tag.find(','); - std::string settingSection = tag.substr(0, comma_pos); - std::string settingTag = tag.substr(comma_pos + 1, tag.length()); + std::string_view settingSection = tag.substr(0, comma_pos); + std::string_view settingTag = tag.substr(comma_pos + 1, tag.length()); _result = Settings::Manager::getString(settingTag, settingSection); } - else if (tag.compare(0, tokenLength, tokenToFind) == 0) + else if (tag.starts_with(tokenToFind)) { - _result = mTranslationDataStorage.translateCellName(tag.substr(tokenLength)); + std::string_view cellName = mTranslationDataStorage.translateCellName(tag.substr(tokenToFind.length())); + _result.assign(cellName.data(), cellName.size()); _result = MyGUI::TextIterator::toTagsString(_result); } else if (Gui::replaceTag(tag, _result)) @@ -1125,7 +1124,7 @@ namespace MWGui else { std::vector split; - Misc::StringUtils::split(tag, split, ":"); + Misc::StringUtils::split(std::string{ tag }, split, ":"); l10n::Manager& l10nManager = *MWBase::Environment::get().getL10nManager(); @@ -1141,7 +1140,7 @@ namespace MWGui { Log(Debug::Error) << "Error: WindowManager::onRetrieveTag: no Store set up yet, can not replace '" << tag << "'"; - _result = tag; + _result.assign(tag.data(), tag.size()); return; } const ESM::GameSetting* setting = mStore->get().search(tag); @@ -1149,7 +1148,7 @@ namespace MWGui if (setting && setting->mValue.getType() == ESM::VT_String) _result = setting->mValue.getString(); else - _result = tag; + _result.assign(tag.data(), tag.size()); } } diff --git a/components/translation/translation.cpp b/components/translation/translation.cpp index 30ba12853f..517a60512f 100644 --- a/components/translation/translation.cpp +++ b/components/translation/translation.cpp @@ -9,7 +9,7 @@ namespace Translation { } - void Storage::loadTranslationData(const Files::Collections& dataFileCollections, const std::string& esmFileName) + void Storage::loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName) { std::string esmNameNoExtension(Misc::StringUtils::lowerCase(esmFileName)); // changing the extension @@ -64,9 +64,9 @@ namespace Translation } } - std::string Storage::translateCellName(const std::string& cellName) const + std::string_view Storage::translateCellName(std::string_view cellName) const { - std::map::const_iterator entry = mCellNamesTranslations.find(cellName); + auto entry = mCellNamesTranslations.find(cellName); if (entry == mCellNamesTranslations.end()) return cellName; @@ -74,12 +74,12 @@ namespace Translation return entry->second; } - std::string Storage::topicID(const std::string& phrase) const + std::string_view Storage::topicID(std::string_view phrase) const { - std::string result = topicStandardForm(phrase); + std::string_view result = topicStandardForm(phrase); // seeking for the topic ID - std::map::const_iterator topicIDIterator = mTopicIDs.find(result); + auto topicIDIterator = mTopicIDs.find(result); if (topicIDIterator != mTopicIDs.end()) result = topicIDIterator->second; @@ -87,9 +87,9 @@ namespace Translation return result; } - std::string Storage::topicStandardForm(const std::string& phrase) const + std::string_view Storage::topicStandardForm(std::string_view phrase) const { - std::map::const_iterator phraseFormsIterator = mPhraseForms.find(phrase); + auto phraseFormsIterator = mPhraseForms.find(phrase); if (phraseFormsIterator != mPhraseForms.end()) return phraseFormsIterator->second; diff --git a/components/translation/translation.hpp b/components/translation/translation.hpp index ee679c4f9b..a7b6087ac7 100644 --- a/components/translation/translation.hpp +++ b/components/translation/translation.hpp @@ -11,20 +11,20 @@ namespace Translation public: Storage(); - void loadTranslationData(const Files::Collections& dataFileCollections, const std::string& esmFileName); + void loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName); - std::string translateCellName(const std::string& cellName) const; - std::string topicID(const std::string& phrase) const; + std::string_view translateCellName(std::string_view cellName) const; + std::string_view topicID(std::string_view phrase) const; // Standard form usually means nominative case - std::string topicStandardForm(const std::string& phrase) const; + std::string_view topicStandardForm(std::string_view phrase) const; void setEncoder(ToUTF8::Utf8Encoder* encoder); bool hasTranslation() const; private: - typedef std::map ContainerType; + typedef std::map> ContainerType; void loadData(ContainerType& container, const std::string& fileNameNoExtension, const std::string& extension, const Files::Collections& dataFileCollections); diff --git a/components/widgets/list.cpp b/components/widgets/list.cpp index de1c160288..b4784299a5 100644 --- a/components/widgets/list.cpp +++ b/components/widgets/list.cpp @@ -36,7 +36,7 @@ namespace Gui void MWList::addSeparator() { - mItems.emplace_back(""); + mItems.emplace_back(std::string{}); } void MWList::adjustSize() @@ -46,9 +46,9 @@ namespace Gui void MWList::redraw(bool scrollbarShown) { - const int _scrollBarWidth = 20; // fetch this from skin? + constexpr int _scrollBarWidth = 20; // fetch this from skin? const int scrollBarWidth = scrollbarShown ? _scrollBarWidth : 0; - const int spacing = 3; + constexpr int spacing = 3; int viewPosition = -mScrollView->getViewOffset().top; while (mScrollView->getChildCount()) @@ -58,16 +58,16 @@ namespace Gui mItemHeight = 0; int i = 0; - for (std::vector::const_iterator it = mItems.begin(); it != mItems.end(); ++it) + for (const auto& item : mItems) { - if (*it != "") + if (!item.empty()) { if (mListItemSkin.empty()) return; MyGUI::Button* button = mScrollView->createWidget(mListItemSkin, MyGUI::IntCoord(0, mItemHeight, mScrollView->getSize().width - scrollBarWidth - 2, 24), - MyGUI::Align::Left | MyGUI::Align::Top, getName() + "_item_" + (*it)); - button->setCaption((*it)); + MyGUI::Align::Left | MyGUI::Align::Top, getName() + "_item_" + item); + button->setCaption(item); button->getSubWidgetText()->setWordWrap(true); button->getSubWidgetText()->setTextAlign(MyGUI::Align::Left); button->eventMouseWheel += MyGUI::newDelegate(this, &MWList::onMouseWheelMoved); @@ -115,12 +115,12 @@ namespace Gui Base::setPropertyOverride(_key, _value); } - unsigned int MWList::getItemCount() + size_t MWList::getItemCount() { - return static_cast(mItems.size()); + return mItems.size(); } - std::string MWList::getItemNameAt(unsigned int at) + const std::string& MWList::getItemNameAt(size_t at) { assert(at < mItems.size() && "List item out of bounds"); return mItems[at]; @@ -134,8 +134,9 @@ namespace Gui void MWList::removeItem(const std::string& name) { - assert(std::find(mItems.begin(), mItems.end(), name) != mItems.end()); - mItems.erase(std::find(mItems.begin(), mItems.end(), name)); + auto it = std::find(mItems.begin(), mItems.end(), name); + assert(it != mItems.end()); + mItems.erase(it); } void MWList::clear() diff --git a/components/widgets/list.hpp b/components/widgets/list.hpp index b9e397f1a8..217fb1370f 100644 --- a/components/widgets/list.hpp +++ b/components/widgets/list.hpp @@ -39,9 +39,9 @@ namespace Gui void addItem(std::string_view name); void addSeparator(); ///< add a seperator between the current and the next item. void removeItem(const std::string& name); - unsigned int getItemCount(); - std::string getItemNameAt(unsigned int at); ///< \attention if there are separators, this method will return "" - ///< at the place where the separator is + size_t getItemCount(); + const std::string& getItemNameAt(size_t at); ///< \attention if there are separators, this method will return "" + ///< at the place where the separator is void clear(); MyGUI::Button* getItemWidget(std::string_view name); diff --git a/components/widgets/tags.cpp b/components/widgets/tags.cpp index 488913233c..3fe72480b7 100644 --- a/components/widgets/tags.cpp +++ b/components/widgets/tags.cpp @@ -7,17 +7,16 @@ namespace Gui { - bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out) + bool replaceTag(std::string_view tag, MyGUI::UString& out) { - std::string fontcolour = "fontcolour="; - size_t fontcolourLength = fontcolour.length(); + std::string_view fontcolour = "fontcolour="; - std::string fontcolourhtml = "fontcolourhtml="; - size_t fontcolourhtmlLength = fontcolourhtml.length(); + std::string_view fontcolourhtml = "fontcolourhtml="; - if (tag.compare(0, fontcolourLength, fontcolour) == 0) + if (tag.starts_with(fontcolour)) { - std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourLength); + std::string fallbackName = "FontColor_color_"; + fallbackName += tag.substr(fontcolour.length()); std::string_view str = Fallback::Map::getString(fallbackName); if (str.empty()) throw std::runtime_error("Unknown fallback name: " + fallbackName); @@ -36,9 +35,10 @@ namespace Gui out = col.print(); return true; } - else if (tag.compare(0, fontcolourhtmlLength, fontcolourhtml) == 0) + else if (tag.starts_with(fontcolourhtml)) { - std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourhtmlLength); + std::string fallbackName = "FontColor_color_"; + fallbackName += tag.substr(fontcolourhtml.length()); std::string_view str = Fallback::Map::getString(fallbackName); if (str.empty()) throw std::runtime_error("Unknown fallback name: " + fallbackName); diff --git a/components/widgets/tags.hpp b/components/widgets/tags.hpp index 8999cfa960..26b6e708b9 100644 --- a/components/widgets/tags.hpp +++ b/components/widgets/tags.hpp @@ -2,14 +2,13 @@ #define OPENMW_WIDGETS_TAGS_H #include -#include -#include +#include namespace Gui { /// Try to replace a tag. Returns true on success and writes the result to \a out. - bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out); + bool replaceTag(std::string_view tag, MyGUI::UString& out); }