From e3734349d2aa0dc4f6b9c7e00616cb95861bb73d Mon Sep 17 00:00:00 2001 From: elektrahesse Date: Thu, 16 Sep 2010 18:09:52 +0200 Subject: Moved all the variables to a struct to keep code more clean and added a check to run the regex match only if the text in chat changes, so it's a bit lighter --- linden/indra/newview/llchatbar.cpp | 80 ++++++++++++++++++++------------------ linden/indra/newview/llchatbar.h | 14 +++++-- 2 files changed, 53 insertions(+), 41 deletions(-) (limited to 'linden') diff --git a/linden/indra/newview/llchatbar.cpp b/linden/indra/newview/llchatbar.cpp index 9d56638..ffd5afa 100644 --- a/linden/indra/newview/llchatbar.cpp +++ b/linden/indra/newview/llchatbar.cpp @@ -116,8 +116,9 @@ LLChatBar::LLChatBar() { setIsChrome(TRUE); - current_index = 0; - last_initials = ""; + mCompletionHolder.current_index = 0; + mCompletionHolder.last_match = ""; + mCompletionHolder.last_txt = ""; #if !LL_RELEASE_FOR_DOWNLOAD childDisplayNotFound(); @@ -222,37 +223,40 @@ BOOL LLChatBar::handleKeyHere( KEY key, MASK mask ) { mInputEditor->deleteSelection(); // Clean up prev completion before attempting a new one - std::string txt(mInputEditor->getText()); - std::string to_match(txt); - std::string left_part = ""; - std::string right_part = ""; S32 cursorPos = mInputEditor->getCursor(); + std::string txt(mInputEditor->getText()); - if (cursorPos < (S32)txt.length()) - { - right_part = txt.substr(cursorPos); - left_part = txt.substr(0, cursorPos); - to_match = std::string(left_part); - } - else + if (mCompletionHolder.last_txt != mInputEditor->getText()) { - to_match = std::string(txt); - left_part = txt; - } + mCompletionHolder.last_txt = std::string(mInputEditor->getText()); - std::string pattern_s = "(^|.*[_=&\\|\\<\\>#@\\[\\]\\-\\+\"',\\.\\?!:;\\*\\(\\)\\s]+)([a-z0-9]+)$"; - boost::match_results what; - boost::regex expression(pattern_s, boost::regex::icase); - if (boost::regex_search(to_match, what, expression, boost::match_extra)) - { - to_match = what[2]; - if (to_match.length() < 1) + if (cursorPos < (S32)txt.length()) + { + mCompletionHolder.right = txt.substr(cursorPos); + mCompletionHolder.left = txt.substr(0, cursorPos); + mCompletionHolder.match = std::string(mCompletionHolder.left); + } + else + { + mCompletionHolder.right = ""; + mCompletionHolder.match = std::string(txt); + mCompletionHolder.left = txt; + } + + std::string pattern_s = "(^|.*[_=&\\|\\<\\>#@\\[\\]\\-\\+\"',\\.\\?!:;\\*\\(\\)\\s]+)([a-z0-9]+)$"; + boost::match_results what; + boost::regex expression(pattern_s, boost::regex::icase); + if (boost::regex_search(mCompletionHolder.match, what, expression, boost::match_extra)) + { + mCompletionHolder.match = what[2]; + if (mCompletionHolder.match.length() < 1) + return handled; + } + else return handled; } - else - return handled; - names.clear(); + mCompletionHolder.names.clear(); for (U32 i=0; i= names.size() || to_match != last_initials) + if (mCompletionHolder.current_index >= mCompletionHolder.names.size() || mCompletionHolder.match != mCompletionHolder.last_match) { - current_index = 0; - last_initials = to_match; + mCompletionHolder.current_index = 0; + mCompletionHolder.last_match = mCompletionHolder.match; } - if (names.size() > 0) + if (mCompletionHolder.names.size() > 0) { - std::string current_name = names[current_index]; + std::string current_name = mCompletionHolder.names[mCompletionHolder.current_index]; - mInputEditor->setText(left_part.substr(0, left_part.length() - to_match.length()) + current_name + right_part); - mInputEditor->setSelection(cursorPos, cursorPos + (current_name.length() - to_match.length())); + mInputEditor->setText(mCompletionHolder.left.substr(0, mCompletionHolder.left.length() - mCompletionHolder.match.length()) + current_name + mCompletionHolder.right); + mInputEditor->setSelection(cursorPos, cursorPos + (current_name.length() - mCompletionHolder.match.length())); - current_index++; + mCompletionHolder.current_index++; return TRUE; } diff --git a/linden/indra/newview/llchatbar.h b/linden/indra/newview/llchatbar.h index 202b006..135881f 100644 --- a/linden/indra/newview/llchatbar.h +++ b/linden/indra/newview/llchatbar.h @@ -46,6 +46,16 @@ class LLChatBarGestureObserver; class LLComboBox; class LLSpinCtrl; +typedef struct { + std::string left; + std::string right; + std::string match; + std::vector names; + std::string last_txt; + std::string last_match; + int current_index; +} CompletionHolder; + class LLChatBar : public LLPanel { @@ -118,9 +128,7 @@ private: BOOL mChanCtrlEnabled; LLSpinCtrl* mChannelControl; - std::vector names; - std::string last_initials; - int current_index; + CompletionHolder mCompletionHolder; }; extern LLChatBar *gChatBar; -- cgit v1.1