From 3cf0977c0b4260741da28d1ad6a28dbf24da0acd Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Thu, 4 May 2023 19:46:49 +0200 Subject: [PATCH] Remove dialogue related instances of getRefIdString --- apps/openmw/mwdialogue/dialoguemanagerimp.cpp | 6 +-- apps/openmw/mwdialogue/hypertextparser.cpp | 7 ++- apps/openmw/mwdialogue/keywordsearch.hpp | 45 +++++++++---------- apps/openmw/mwdialogue/scripttest.cpp | 2 +- apps/openmw/mwdialogue/topic.cpp | 2 +- apps/openmw/mwgui/dialogue.hpp | 2 +- apps/openmw/mwgui/journalviewmodel.cpp | 6 +-- apps/openmw/mwworld/store.cpp | 4 +- apps/openmw/mwworld/store.hpp | 4 +- .../mwdialogue/test_keywordsearch.cpp | 28 ++++++------ 10 files changed, 50 insertions(+), 56 deletions(-) diff --git a/apps/openmw/mwdialogue/dialoguemanagerimp.cpp b/apps/openmw/mwdialogue/dialoguemanagerimp.cpp index bb06a53b35..28771d8f1f 100644 --- a/apps/openmw/mwdialogue/dialoguemanagerimp.cpp +++ b/apps/openmw/mwdialogue/dialoguemanagerimp.cpp @@ -408,12 +408,12 @@ namespace MWDialogue updateActorKnownTopics(); std::list keywordList; - + const auto& store = MWBase::Environment::get().getESMStore()->get(); for (const auto& [topic, topicInfo] : mActorKnownTopics) { // does the player know the topic? - if (mKnownTopics.count(topic)) - keywordList.push_back(topic.getRefIdString()); + if (mKnownTopics.contains(topic)) + keywordList.push_back(store.find(topic)->mStringId); } // sort again, because the previous sort was case-sensitive diff --git a/apps/openmw/mwdialogue/hypertextparser.cpp b/apps/openmw/mwdialogue/hypertextparser.cpp index 54a0d99d03..11a2de77b5 100644 --- a/apps/openmw/mwdialogue/hypertextparser.cpp +++ b/apps/openmw/mwdialogue/hypertextparser.cpp @@ -50,13 +50,12 @@ namespace MWDialogue const auto& keywordSearch = MWBase::Environment::get().getESMStore()->get().getDialogIdKeywordSearch(); - std::vector::Match> matches; + std::vector::Match> matches; keywordSearch.highlightKeywords(text.begin(), text.end(), matches); - for (std::vector::Match>::const_iterator it = matches.begin(); - it != matches.end(); ++it) + for (const auto& match : matches) { - tokens.emplace_back(std::string(it->mBeg, it->mEnd), Token::ImplicitKeyword); + tokens.emplace_back(std::string(match.mBeg, match.mEnd), Token::ImplicitKeyword); } } diff --git a/apps/openmw/mwdialogue/keywordsearch.hpp b/apps/openmw/mwdialogue/keywordsearch.hpp index e864533906..3b784cd59c 100644 --- a/apps/openmw/mwdialogue/keywordsearch.hpp +++ b/apps/openmw/mwdialogue/keywordsearch.hpp @@ -13,11 +13,11 @@ namespace MWDialogue { - template + template class KeywordSearch { public: - typedef typename string_t::const_iterator Point; + using Point = std::string::const_iterator; struct Match { @@ -26,11 +26,11 @@ namespace MWDialogue value_t mValue; }; - void seed(string_t keyword, value_t value) + void seed(std::string_view keyword, value_t value) { if (keyword.empty()) return; - seed_impl(std::move(keyword), std::move(value), 0, mRoot); + seed_impl(keyword, std::move(value), 0, mRoot); } void clear() @@ -39,23 +39,21 @@ namespace MWDialogue mRoot.mKeyword.clear(); } - bool containsKeyword(const string_t& keyword, value_t& value) + bool containsKeyword(std::string_view keyword, value_t& value) { - typename Entry::childen_t::iterator current; - typename Entry::childen_t::iterator next; - - current = mRoot.mChildren.find(Misc::StringUtils::toLower(*keyword.begin())); + auto it = keyword.begin(); + auto current = mRoot.mChildren.find(Misc::StringUtils::toLower(*it)); if (current == mRoot.mChildren.end()) return false; - else if (current->second.mKeyword.size() && Misc::StringUtils::ciEqual(current->second.mKeyword, keyword)) + else if (Misc::StringUtils::ciEqual(current->second.mKeyword, keyword)) { value = current->second.mValue; return true; } - for (Point i = ++keyword.begin(); i != keyword.end(); ++i) + for (++it; it != keyword.end(); ++it) { - next = current->second.mChildren.find(Misc::StringUtils::toLower(*i)); + auto next = current->second.mChildren.find(Misc::StringUtils::toLower(*it)); if (next == current->second.mChildren.end()) return false; if (Misc::StringUtils::ciEqual(next->second.mKeyword, keyword)) @@ -116,14 +114,12 @@ namespace MWDialogue // candidates first std::reverse(candidates.begin(), candidates.end()); - for (typename std::vector>::iterator it - = candidates.begin(); - it != candidates.end(); ++it) + for (const auto& [pos, c] : candidates) { - candidate = it->second; + candidate = c; // try to match the rest of the keyword - Point k = i + it->first; - typename string_t::const_iterator t = candidate->second.mKeyword.begin() + (k - i); + Point k = i + pos; + Point t = candidate->second.mKeyword.begin() + (k - i); while (k != end && t != candidate->second.mKeyword.end()) { @@ -196,12 +192,12 @@ namespace MWDialogue { typedef std::map childen_t; - string_t mKeyword; + std::string mKeyword; value_t mValue; childen_t mChildren; }; - void seed_impl(string_t keyword, value_t value, size_t depth, Entry& entry) + void seed_impl(std::string_view keyword, value_t value, size_t depth, Entry& entry) { int ch = Misc::StringUtils::toLower(keyword.at(depth)); @@ -210,7 +206,7 @@ namespace MWDialogue if (j == entry.mChildren.end()) { entry.mChildren[ch].mValue = std::move(value); - entry.mChildren[ch].mKeyword = std::move(keyword); + entry.mChildren[ch].mKeyword = keyword; } else { @@ -219,22 +215,21 @@ namespace MWDialogue if (keyword == j->second.mKeyword) throw std::runtime_error("duplicate keyword inserted"); - value_t pushValue = j->second.mValue; - string_t pushKeyword = j->second.mKeyword; + const auto& pushKeyword = j->second.mKeyword; if (depth >= pushKeyword.size()) throw std::runtime_error("unexpected"); if (depth + 1 < pushKeyword.size()) { - seed_impl(std::move(pushKeyword), std::move(pushValue), depth + 1, j->second); + seed_impl(pushKeyword, j->second.mValue, depth + 1, j->second); j->second.mKeyword.clear(); } } if (depth + 1 == keyword.size()) j->second.mKeyword = value; else // depth+1 < keyword.size() - seed_impl(std::move(keyword), std::move(value), depth + 1, j->second); + seed_impl(keyword, std::move(value), depth + 1, j->second); } } diff --git a/apps/openmw/mwdialogue/scripttest.cpp b/apps/openmw/mwdialogue/scripttest.cpp index 92746c8856..fd5a240711 100644 --- a/apps/openmw/mwdialogue/scripttest.cpp +++ b/apps/openmw/mwdialogue/scripttest.cpp @@ -156,7 +156,7 @@ namespace MWDialogue else { errorHandler.reset(); - errorHandler.setContext(info.mId.getRefIdString() + " in " + topic.mId.getRefIdString()); + errorHandler.setContext(info.mId.getRefIdString() + " in " + topic.mStringId); if (!test(ptr, info, compiled, total, extensions, compilerContext, errorHandler)) Log(Debug::Error) << "Test failed for " << info.mId << " in " << topic.mId << '\n' << info.mResultScript; diff --git a/apps/openmw/mwdialogue/topic.cpp b/apps/openmw/mwdialogue/topic.cpp index 9526c4f0eb..2b51c27fb1 100644 --- a/apps/openmw/mwdialogue/topic.cpp +++ b/apps/openmw/mwdialogue/topic.cpp @@ -11,7 +11,7 @@ namespace MWDialogue Topic::Topic(const ESM::RefId& topic) : mTopic(topic) - , mName(MWBase::Environment::get().getESMStore()->get().find(topic)->mId.getRefIdString()) + , mName(MWBase::Environment::get().getESMStore()->get().find(topic)->mStringId) { } diff --git a/apps/openmw/mwgui/dialogue.hpp b/apps/openmw/mwgui/dialogue.hpp index 632fd9ea09..d427d77f07 100644 --- a/apps/openmw/mwgui/dialogue.hpp +++ b/apps/openmw/mwgui/dialogue.hpp @@ -108,7 +108,7 @@ namespace MWGui void activated() override; }; - typedef MWDialogue::KeywordSearch KeywordSearchT; + typedef MWDialogue::KeywordSearch KeywordSearchT; struct DialogueText { diff --git a/apps/openmw/mwgui/journalviewmodel.cpp b/apps/openmw/mwgui/journalviewmodel.cpp index 6f9f99516a..fd5f7b73c7 100644 --- a/apps/openmw/mwgui/journalviewmodel.cpp +++ b/apps/openmw/mwgui/journalviewmodel.cpp @@ -21,7 +21,7 @@ namespace MWGui struct JournalViewModelImpl : JournalViewModel { - typedef MWDialogue::KeywordSearch KeywordSearchT; + typedef MWDialogue::KeywordSearch KeywordSearchT; mutable bool mKeywordSearchLoaded; mutable KeywordSearchT mKeywordSearch; @@ -56,7 +56,7 @@ namespace MWGui MWBase::Journal* journal = MWBase::Environment::get().getJournal(); for (MWBase::Journal::TTopicIter i = journal->topicBegin(); i != journal->topicEnd(); ++i) - mKeywordSearch.seed(i->first.getRefIdString(), intptr_t(&i->second)); + mKeywordSearch.seed(i->second.getName(), intptr_t(&i->second)); mKeywordSearchLoaded = true; } @@ -312,7 +312,7 @@ namespace MWGui for (MWBase::Journal::TTopicIter i = journal->topicBegin(); i != journal->topicEnd(); ++i) { - Utf8Stream stream(i->first.getRefIdString().c_str()); + Utf8Stream stream(i->second.getName()); Utf8Stream::UnicodeChar first = Utf8Stream::toLowerUtf8(stream.peek()); if (first != Utf8Stream::toLowerUtf8(character)) diff --git a/apps/openmw/mwworld/store.cpp b/apps/openmw/mwworld/store.cpp index b73677d39c..7a27ff8a10 100644 --- a/apps/openmw/mwworld/store.cpp +++ b/apps/openmw/mwworld/store.cpp @@ -1142,7 +1142,7 @@ namespace MWWorld list.push_back(dialogue->mId); } - const MWDialogue::KeywordSearch& Store::getDialogIdKeywordSearch() const + const MWDialogue::KeywordSearch& Store::getDialogIdKeywordSearch() const { if (mKeywordSearchModFlag) { @@ -1151,7 +1151,7 @@ namespace MWWorld std::vector keywordList; keywordList.reserve(getSize()); for (const auto& it : *this) - keywordList.push_back(Misc::StringUtils::lowerCase(it.mId.getRefIdString())); + keywordList.push_back(Misc::StringUtils::lowerCase(it.mStringId)); sort(keywordList.begin(), keywordList.end()); for (const auto& it : keywordList) diff --git a/apps/openmw/mwworld/store.hpp b/apps/openmw/mwworld/store.hpp index 4a82ea9e43..ba948ee838 100644 --- a/apps/openmw/mwworld/store.hpp +++ b/apps/openmw/mwworld/store.hpp @@ -512,7 +512,7 @@ namespace MWWorld std::vector mShared; mutable bool mKeywordSearchModFlag; - mutable MWDialogue::KeywordSearch mKeywordSearch; + mutable MWDialogue::KeywordSearch mKeywordSearch; public: Store(); @@ -535,7 +535,7 @@ namespace MWWorld void listIdentifier(std::vector& list) const override; - const MWDialogue::KeywordSearch& getDialogIdKeywordSearch() const; + const MWDialogue::KeywordSearch& getDialogIdKeywordSearch() const; }; template <> diff --git a/apps/openmw_test_suite/mwdialogue/test_keywordsearch.cpp b/apps/openmw_test_suite/mwdialogue/test_keywordsearch.cpp index b886a8b620..b97f30b319 100644 --- a/apps/openmw_test_suite/mwdialogue/test_keywordsearch.cpp +++ b/apps/openmw_test_suite/mwdialogue/test_keywordsearch.cpp @@ -12,14 +12,14 @@ protected: TEST_F(KeywordSearchTest, keyword_test_conflict_resolution) { // test to make sure the longest keyword in a chain of conflicting keywords gets chosen - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("foo bar", 0); search.seed("bar lock", 0); search.seed("lock switch", 0); std::string text = "foo bar lock switch"; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); // Should contain: "foo bar", "lock switch" @@ -30,13 +30,13 @@ TEST_F(KeywordSearchTest, keyword_test_conflict_resolution) TEST_F(KeywordSearchTest, keyword_test_conflict_resolution2) { - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("the dwemer", 0); search.seed("dwemer language", 0); std::string text = "the dwemer language"; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); EXPECT_EQ(matches.size(), 1); @@ -47,14 +47,14 @@ TEST_F(KeywordSearchTest, keyword_test_conflict_resolution3) { // testing that the longest keyword is chosen, rather than maximizing the // amount of highlighted characters by highlighting the first and last keyword - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("foo bar", 0); search.seed("bar lock", 0); search.seed("lock so", 0); std::string text = "foo bar lock so"; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); EXPECT_EQ(matches.size(), 1); @@ -64,7 +64,7 @@ TEST_F(KeywordSearchTest, keyword_test_conflict_resolution3) TEST_F(KeywordSearchTest, keyword_test_utf8_word_begin) { // We make sure that the search works well even if the character is not ASCII - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("états", 0); search.seed("ïrradiés", 0); search.seed("ça nous déçois", 0); @@ -74,7 +74,7 @@ TEST_F(KeywordSearchTest, keyword_test_utf8_word_begin) = "les nations unis ont réunis le monde entier, états units inclus pour parler du problème des gens ïrradiés " "et ça nous déçois"; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); EXPECT_EQ(matches.size(), 3); @@ -86,12 +86,12 @@ TEST_F(KeywordSearchTest, keyword_test_utf8_word_begin) TEST_F(KeywordSearchTest, keyword_test_non_alpha_non_whitespace_word_begin) { // We make sure that the search works well even if the separator is not a whitespace - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("Report to caius cosades", 0); std::string text = "I was told to \"Report to caius cosades\""; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); EXPECT_EQ(matches.size(), 1); @@ -101,13 +101,13 @@ TEST_F(KeywordSearchTest, keyword_test_non_alpha_non_whitespace_word_begin) TEST_F(KeywordSearchTest, keyword_test_russian_non_ascii_before) { // We make sure that the search works well even if the separator is not a whitespace with russian chars - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("Доложить Каю Косадесу", 0); std::string text = "Что? Да. Я Кай Косадес. То есть как это, вам велели «Доложить Каю Косадесу»? О чем вы говорите?"; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); EXPECT_EQ(matches.size(), 1); @@ -117,13 +117,13 @@ TEST_F(KeywordSearchTest, keyword_test_russian_non_ascii_before) TEST_F(KeywordSearchTest, keyword_test_russian_ascii_before) { // We make sure that the search works well even if the separator is not a whitespace with russian chars - MWDialogue::KeywordSearch search; + MWDialogue::KeywordSearch search; search.seed("Доложить Каю Косадесу", 0); std::string text = "Что? Да. Я Кай Косадес. То есть как это, вам велели 'Доложить Каю Косадесу'? О чем вы говорите?"; - std::vector::Match> matches; + std::vector::Match> matches; search.highlightKeywords(text.begin(), text.end(), matches); EXPECT_EQ(matches.size(), 1);