From c1ddf4c0840bb15016dc69af4a4751d09908393f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 8 Jan 2010 16:03:29 +0200 Subject: Small refactoring in chat auto completing --- src/beingmanager.cpp | 29 ++++++------------------ src/beingmanager.h | 3 ++- src/gui/chat.cpp | 58 ++++++++++++++++++++++++++++++++++------------- src/gui/chat.h | 4 ++++ src/gui/partywindow.cpp | 23 +++---------------- src/gui/partywindow.h | 3 ++- src/utils/stringutils.cpp | 2 +- src/utils/stringutils.h | 2 +- 8 files changed, 62 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp index 74461d44..8bd951fa 100644 --- a/src/beingmanager.cpp +++ b/src/beingmanager.cpp @@ -300,36 +300,21 @@ bool BeingManager::hasBeing(Being *being) const return false; } -std::string BeingManager::getAutoCompletePlayerName(std::string partName) +void BeingManager::getPlayerNames(std::vector &names, + bool npcNames) { Beings::iterator i = mBeings.begin(); - std::transform(partName.begin(), partName.end(), partName.begin(), tolower); - std::string newName(""); + names.clear(); while (i != mBeings.end()) { Being *being = (*i); - if (being->getType() != Being::MONSTER && being->getName() != "") + if ((being->getType() == Being::PLAYER + || (being->getType() == Being::NPC && npcNames)) + && being->getName() != "") { - std::string name = being->getName(); - std::transform(name.begin(), name.end(), name.begin(), tolower); - - std::string::size_type pos = name.find(partName, 0); - if (pos == 0) - { - if (newName != "") - { - std::transform(newName.begin(), newName.end(), newName.begin(), tolower); - newName = findSameSubstring(name, newName); - } - else - { - newName = being->getName(); - } - } + names.push_back(being->getName()); } ++i; } - - return newName; } diff --git a/src/beingmanager.h b/src/beingmanager.h index 4490738e..bea84e25 100644 --- a/src/beingmanager.h +++ b/src/beingmanager.h @@ -118,7 +118,8 @@ class BeingManager */ void clear(); - std::string getAutoCompletePlayerName(std::string partName); + void getPlayerNames(std::vector &names, + bool npcNames); protected: Beings mBeings; diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index af0c3808..c9defe33 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -531,13 +531,16 @@ void ChatWindow::autoComplete() ChatTab *cTab = static_cast(mChatTabs->getSelectedTab()); + std::vector nameList; if (cTab && cTab->getType() == ChatTab::PARTY) { - newName = partyWindow->getAutoCompleteName(name); + partyWindow->getNames(nameList); + newName = autoComplete(nameList, name); } if (newName == "") { - newName = beingManager->getAutoCompletePlayerName(name); + beingManager->getPlayerNames(nameList, true); + newName = autoComplete(nameList, name); } if (newName == "") { @@ -556,10 +559,44 @@ void ChatWindow::autoComplete() } } +std::string ChatWindow::autoComplete(std::vector &names, + std::string partName) const +{ + std::vector::iterator i = names.begin(); + toLower(partName); + std::string newName(""); + + while (i != names.end()) + { + if (!i->empty()) + { + std::string name = *i; + toLower(name); + + std::string::size_type pos = name.find(partName, 0); + if (pos == 0) + { + if (newName != "") + { + toLower(newName); + newName = findSameSubstring(name, newName); + } + else + { + newName = *i; + } + } + } + ++i; + } + + return newName; +} + std::string ChatWindow::autoCompleteHistory(std::string partName) { History::iterator i = mHistory.begin(); - std::string newName = ""; + std::vector nameList; while (i != mHistory.end()) { @@ -572,20 +609,9 @@ std::string ChatWindow::autoCompleteHistory(std::string partName) line = line.substr(0, f); if (line != "") { - std::string::size_type pos = line.find(partName, 0); - if (pos == 0) - { - if (newName != "") - { - newName = findSameSubstring(line, newName); - } - else - { - newName = line; - } - } + nameList.push_back(line); } ++i; } - return newName; + return autoComplete(nameList, partName); } diff --git a/src/gui/chat.h b/src/gui/chat.h index b64ea5e8..4516257f 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -32,6 +32,7 @@ #include #include #include +#include class BrowserBox; class Channel; @@ -191,6 +192,9 @@ class ChatWindow : public Window, std::string autoCompleteHistory(std::string partName); + std::string autoComplete(std::vector &names, + std::string partName) const; + /** Used for showing item popup on clicking links **/ ItemLinkHandler *mItemLinkHandler; Recorder *mRecorder; diff --git a/src/gui/partywindow.cpp b/src/gui/partywindow.cpp index ae8112c8..67f1d4c0 100644 --- a/src/gui/partywindow.cpp +++ b/src/gui/partywindow.cpp @@ -254,11 +254,10 @@ void PartyWindow::buildLayout() } } -std::string PartyWindow::getAutoCompleteName(std::string partName) +void PartyWindow::getNames(std::vector &names) { PartyList::iterator i = mMembers.begin(); - std::transform(partName.begin(), partName.end(), partName.begin(), tolower); - std::string newName(""); + names.clear(); while (i != mMembers.end()) { @@ -266,24 +265,8 @@ std::string PartyWindow::getAutoCompleteName(std::string partName) if (member->getAvatar() && member->getAvatar()->getName() != "") { std::string name = member->getAvatar()->getName(); - std::transform(name.begin(), name.end(), name.begin(), tolower); - - std::string::size_type pos = name.find(partName, 0); - if (pos == 0) - { - if (newName != "") - { - std::transform(newName.begin(), newName.end(), newName.begin(), tolower); - newName = findSameSubstring(name, newName); - } - else - { - newName = member->getAvatar()->getName(); - } - } + names.push_back(name); } ++i; } - - return newName; } diff --git a/src/gui/partywindow.h b/src/gui/partywindow.h index ed217910..0fe2986f 100644 --- a/src/gui/partywindow.h +++ b/src/gui/partywindow.h @@ -32,6 +32,7 @@ #include #include +#include class PartyWindow; @@ -127,7 +128,7 @@ class PartyWindow : public Window, gcn::ActionListener void clearMembers(); - std::string getAutoCompleteName(std::string partName); + void getNames(std::vector &names); private: /** diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 6727dcdf..996be29d 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -146,7 +146,7 @@ bool isWordSeparator(char chr) return (chr == ' ' || chr == ',' || chr == '.'); } -const std::string findSameSubstring(std::string &str1, std::string &str2) +const std::string findSameSubstring(const std::string &str1, const std::string &str2) { int minLength = str1.length() > str2.length() ? str2.length() : str1.length(); for (int f = 0; f < minLength; f ++) diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 4f0138de..2b8cd45c 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -121,6 +121,6 @@ int compareStrI(const std::string &a, const std::string &b); bool isWordSeparator(char chr); -const std::string findSameSubstring(std::string &str1, std::string &str2); +const std::string findSameSubstring(const std::string &str1, const std::string &str2); #endif // UTILS_STRINGUTILS_H -- cgit v1.2.3-70-g09d2