コード例 #1
0
void LLAutoReplace::autoreplaceCallback(LLUIString& inputText, S32& cursorPos)
{
	static LLCachedControl<bool> perform_autoreplace(gSavedSettings, "AutoReplace");
	if(perform_autoreplace)
	{
		S32 wordEnd = cursorPos-1;
		LLWString text = inputText.getWString();

		bool atSpace  = (text[wordEnd] == ' ');
		bool haveWord = (LLWStringUtil::isPartOfWord(text[wordEnd]));

		if (atSpace || haveWord)
		{
			if (atSpace && wordEnd > 0)
			{
				// find out if this space immediately follows a word
				wordEnd--;
				haveWord  = (LLWStringUtil::isPartOfWord(text[wordEnd]));
			}
			if (haveWord)
			{
				// wordEnd points to the end of a word, now find the start of the word
				std::string word;
				S32 wordStart = wordEnd;
				for ( S32 backOne = wordStart - 1;
					  backOne >= 0 && LLWStringUtil::isPartOfWord(text[backOne]);
					  backOne--
					 )
				{
					wordStart--; // walk wordStart back to the beginning of the word
				}
				LL_DEBUGS("AutoReplace")<<"wordStart: "<<wordStart<<" wordEnd: "<<wordEnd<<LL_ENDL;
				std::string strText  = std::string(text.begin(), text.end());
				std::string lastWord = strText.substr(wordStart, wordEnd-wordStart+1);
				std::string replacementWord( mSettings.replaceWord( lastWord ) );

				if ( replacementWord != lastWord )
				{
					// The last word is one for which we have a replacement
					if (atSpace)
					{
						// replace the last word in the input
						LLWString strNew = utf8str_to_wstring(replacementWord);
						LLWString strOld = utf8str_to_wstring(lastWord);
						int size_change = strNew.size() - strOld.size();

						text.replace(wordStart,lastWord.length(),strNew);
						inputText = wstring_to_utf8str(text);
						cursorPos+=size_change;
					}
				}
			}
		}
	}
}