summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2024-05-24 09:22:40 -0400
committerNat Goodspeed <nat@lindenlab.com>2024-05-24 09:22:40 -0400
commitb0ef843fe0702482e843379b4975b2dda4d58935 (patch)
tree0db08787dfcf84c8a12f4b8a5f189de6e180a8e4
parent591a80765c4438f72795230824d2e549bafc592d (diff)
Nat's ideas from PR #1547
-rw-r--r--indra/llcommon/llstring.h62
-rw-r--r--indra/llui/llchat.h13
-rw-r--r--indra/newview/llchathistory.cpp22
-rw-r--r--indra/newview/llfloaterimnearbychathandler.cpp5
-rw-r--r--indra/newview/llfloaterimnearbychatlistener.cpp24
-rw-r--r--indra/newview/llfloaterimnearbychatlistener.h4
-rw-r--r--indra/newview/llviewerchat.cpp2
-rw-r--r--indra/newview/llviewermessage.cpp5
-rw-r--r--indra/newview/scripts/lua/test_LLChat.lua12
9 files changed, 108 insertions, 41 deletions
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h
index 21b0da4822..cd8b2a2dcd 100644
--- a/indra/llcommon/llstring.h
+++ b/indra/llcommon/llstring.h
@@ -315,6 +315,14 @@ public:
static void trim(string_type& string) { trimHead(string); trimTail(string); }
static void truncate(string_type& string, size_type count);
+ // if string startsWith prefix, remove it and return true
+ static bool removePrefix(string_type& string, const string_type& prefix);
+ // if string startsWith prefix, return (string without prefix, true), else (string, false)
+ static std::pair<string_type, bool> withoutPrefix(const string_type& string, const string_type& prefix);
+ // like removePrefix()
+ static bool removeSuffix(string_type& string, const string_type& suffix);
+ static std::pair<string_type, bool> withoutSuffix(const string_type& string, const string_type& suffix);
+
static void toUpper(string_type& string);
static void toLower(string_type& string);
@@ -1479,6 +1487,60 @@ void LLStringUtilBase<T>::trimTail(string_type& string)
}
}
+// if string startsWith prefix, remove it and return true
+template<class T>
+bool LLStringUtilBase<T>::removePrefix(string_type& string, const string_type& prefix)
+{
+ bool found{ startsWith(string, prefix) };
+ if (found)
+ {
+ string.erase(0, prefix.length());
+ }
+ return found;
+}
+
+// if string startsWith prefix, return (string without prefix, true), else (string, false)
+template<class T>
+std::pair<typename LLStringUtilBase<T>::string_type, bool>
+LLStringUtilBase<T>::withoutPrefix(const string_type& string, const string_type& prefix)
+{
+ bool found{ startsWith(string, prefix) };
+ if (! found)
+ {
+ return { string, false };
+ }
+ else
+ {
+ return { string.substr(prefix.length()), true };
+ }
+}
+
+// like removePrefix()
+template<class T>
+bool LLStringUtilBase<T>::removeSuffix(string_type& string, const string_type& suffix)
+{
+ bool found{ endsWith(string, suffix) };
+ if (found)
+ {
+ string.erase(string.length() - suffix.length());
+ }
+ return found;
+}
+
+template<class T>
+std::pair<typename LLStringUtilBase<T>::string_type, bool>
+LLStringUtilBase<T>::withoutSuffix(const string_type& string, const string_type& suffix)
+{
+ bool found{ endsWith(string, suffix) };
+ if (! found)
+ {
+ return { string, false };
+ }
+ else
+ {
+ return { string.substr(0, string.length() - suffix.length()), true };
+ }
+}
// Replace line feeds with carriage return-line feed pairs.
//static
diff --git a/indra/llui/llchat.h b/indra/llui/llchat.h
index 8fd92c2550..70d7e82970 100644
--- a/indra/llui/llchat.h
+++ b/indra/llui/llchat.h
@@ -113,4 +113,17 @@ public:
};
static const std::string LUA_PREFIX("[LUA]");
+inline
+std::string without_LUA_PREFIX(const std::string& string, bool is_lua)
+{
+ if (is_lua)
+ {
+ return string.substr(LUA_PREFIX.size());
+ }
+ else
+ {
+ return string;
+ }
+}
+
#endif
diff --git a/indra/newview/llchathistory.cpp b/indra/newview/llchathistory.cpp
index 54b0c171f1..f16377d7e3 100644
--- a/indra/newview/llchathistory.cpp
+++ b/indra/newview/llchathistory.cpp
@@ -665,6 +665,7 @@ public:
mSessionID = chat.mSessionID;
mSourceType = chat.mSourceType;
mIsFromScript = is_script;
+ mScriptPrefix = is_script? LLTrans::getString("ScriptBy") : "";
// To be able to report a message, we need a copy of it's text
// and it's easier to store text directly than trying to get
@@ -734,7 +735,7 @@ public:
username_end == (chat.mFromName.length() - 1))
{
mFrom = chat.mFromName.substr(0, username_start);
- user_name->setValue(mIsFromScript ? LLTrans::getString("ScriptBy") + mFrom : mFrom);
+ user_name->setValue(mScriptPrefix + mFrom);
if (gSavedSettings.getBOOL("NameTagShowUsernames"))
{
@@ -789,7 +790,7 @@ public:
icon->setValue(LLSD("Command_Destinations_Icon"));
break;
case CHAT_SOURCE_UNKNOWN:
- icon->setValue(mIsFromScript ? LLSD("Inv_Script") : LLSD(chat.mFromID));
+ icon->setValue(mIsFromScript ? LLSD("Inv_Script") : LLSD("Unknown_Icon"));
}
// In case the message came from an object, save the object info
@@ -1031,14 +1032,7 @@ private:
mFrom = av_name.getDisplayName();
LLTextBox* user_name = getChild<LLTextBox>("user_name");
- if(mIsFromScript)
- {
- user_name->setValue(LLSD(LLTrans::getString("ScriptBy") + av_name.getDisplayName()));
- }
- else
- {
- user_name->setValue(LLSD(av_name.getDisplayName()));
- }
+ user_name->setValue(LLSD(mScriptPrefix + av_name.getDisplayName()));
user_name->setToolTip( av_name.getUserName() );
if (gSavedSettings.getBOOL("NameTagShowUsernames") &&
@@ -1070,6 +1064,7 @@ protected:
std::string mFrom;
LLUUID mSessionID;
std::string mText;
+ std::string mScriptPrefix;
F64 mTime; // IM's frame time
time_t mCreationTime; // Views's time
@@ -1080,7 +1075,7 @@ protected:
bool mNeedsTimeBox;
- bool mIsFromScript;
+ bool mIsFromScript;
private:
boost::signals2::connection mAvatarNameCacheConnection;
@@ -1270,8 +1265,8 @@ void LLChatHistory::appendMessage(const LLChat& chat, const LLSD &args, const LL
name_params.color(name_color);
name_params.readonly_color(name_color);
- bool is_lua = (chat.mText.substr(0, LUA_PREFIX.size()) == LUA_PREFIX);
- std::string prefix = chat.mText.substr(is_lua ? LUA_PREFIX.size() : 0, 4);
+ auto [message, is_lua] = LLStringUtil::withoutPrefix(chat.mText, LUA_PREFIX);
+ std::string prefix = message.substr(0, 4);
//IRC styled /me messages.
bool irc_me = prefix == "/me " || prefix == "/me'";
@@ -1505,7 +1500,6 @@ void LLChatHistory::appendMessage(const LLChat& chat, const LLSD &args, const LL
// usual messages showing
else if (!teleport_separator)
{
- std::string message = is_lua ? chat.mText.substr(LUA_PREFIX.size()) : chat.mText;
message = irc_me ? message.substr(3) : message;
//MESSAGE TEXT PROCESSING
diff --git a/indra/newview/llfloaterimnearbychathandler.cpp b/indra/newview/llfloaterimnearbychathandler.cpp
index 91d9cd56cf..cda71f97d4 100644
--- a/indra/newview/llfloaterimnearbychathandler.cpp
+++ b/indra/newview/llfloaterimnearbychathandler.cpp
@@ -599,6 +599,7 @@ void LLFloaterIMNearbyChatHandler::processChat(const LLChat& chat_msg,
{
// Handle IRC styled messages.
std::string toast_msg;
+ std::string msg_text = without_LUA_PREFIX(chat_msg.mText, chat_msg.mIsScript);
if (chat_msg.mChatStyle == CHAT_STYLE_IRC)
{
if (chat_msg.mIsScript)
@@ -609,11 +610,11 @@ void LLFloaterIMNearbyChatHandler::processChat(const LLChat& chat_msg,
{
toast_msg += chat_msg.mFromName;
}
- toast_msg += chat_msg.mText.substr(chat_msg.mIsScript ? LUA_PREFIX.size() + 3 : 3);
+ toast_msg += msg_text.substr(3);
}
else
{
- toast_msg = chat_msg.mIsScript ? chat_msg.mText.substr(LUA_PREFIX.size()) : chat_msg.mText;
+ toast_msg = msg_text;
}
bool chat_overlaps = false;
diff --git a/indra/newview/llfloaterimnearbychatlistener.cpp b/indra/newview/llfloaterimnearbychatlistener.cpp
index e8fb510111..9093db282a 100644
--- a/indra/newview/llfloaterimnearbychatlistener.cpp
+++ b/indra/newview/llfloaterimnearbychatlistener.cpp
@@ -34,6 +34,7 @@
#include "llagent.h"
#include "llchat.h"
#include "llviewercontrol.h"
+#include "stringize.h"
static const F32 CHAT_THROTTLE_PERIOD = 1.f;
@@ -51,17 +52,16 @@ LLFloaterIMNearbyChatListener::LLFloaterIMNearbyChatListener()
// "sendChat" command
-void LLFloaterIMNearbyChatListener::sendChat(LLSD const & chat_data) const
+void LLFloaterIMNearbyChatListener::sendChat(LLSD const & chat_data)
{
- static F64 last_throttle_time = 0.0;
F64 cur_time = LLTimer::getElapsedSeconds();
- if (cur_time < last_throttle_time + CHAT_THROTTLE_PERIOD)
+ if (cur_time < mLastThrottleTime + CHAT_THROTTLE_PERIOD)
{
LL_DEBUGS("LLFloaterIMNearbyChatListener") << "'sendChat' was throttled" << LL_ENDL;
return;
}
- last_throttle_time = cur_time;
+ mLastThrottleTime = cur_time;
// Extract the data
std::string chat_text = LUA_PREFIX + chat_data["message"].asString();
@@ -91,20 +91,14 @@ void LLFloaterIMNearbyChatListener::sendChat(LLSD const & chat_data) const
}
// Have to prepend /42 style channel numbers
- std::string chat_to_send;
- if (channel == 0)
+ if (channel)
{
- chat_to_send = chat_text;
- }
- else
- {
- chat_to_send += "/";
- chat_to_send += chat_data["channel"].asString();
- chat_to_send += " ";
- chat_to_send += chat_text;
+ chat_text = stringize("/", chat_data["channel"].asString(), " ", chat_text);
}
// Send it as if it was typed in
- LLFloaterIMNearbyChat::sendChatFromViewer(chat_to_send, type_o_chat, ((BOOL) (channel == 0)) && gSavedSettings.getBOOL("PlayChatAnim"));
+ LLFloaterIMNearbyChat::sendChatFromViewer(chat_text, type_o_chat,
+ (channel == 0) &&
+ gSavedSettings.getBOOL("PlayChatAnim"));
}
diff --git a/indra/newview/llfloaterimnearbychatlistener.h b/indra/newview/llfloaterimnearbychatlistener.h
index 0df0341d36..34d4effd48 100644
--- a/indra/newview/llfloaterimnearbychatlistener.h
+++ b/indra/newview/llfloaterimnearbychatlistener.h
@@ -41,7 +41,9 @@ public:
LLFloaterIMNearbyChatListener();
private:
- void sendChat(LLSD const & chat_data) const;
+ F64 mLastThrottleTime{ 0.0 };
+
+ void sendChat(LLSD const & chat_data);
};
#endif // LL_LLFLOATERIMNEARBYCHATLISTENER_H
diff --git a/indra/newview/llviewerchat.cpp b/indra/newview/llviewerchat.cpp
index 70c426740b..00520f100e 100644
--- a/indra/newview/llviewerchat.cpp
+++ b/indra/newview/llviewerchat.cpp
@@ -217,7 +217,7 @@ S32 LLViewerChat::getChatFontSize()
//static
void LLViewerChat::formatChatMsg(const LLChat& chat, std::string& formated_msg)
{
- std::string tmpmsg = chat.mIsScript ? chat.mText.substr(LUA_PREFIX.size()) : chat.mText;
+ std::string tmpmsg = without_LUA_PREFIX(chat.mText, chat.mIsScript);
if(chat.mChatStyle == CHAT_STYLE_IRC)
{
formated_msg = chat.mFromName + tmpmsg.substr(3);
diff --git a/indra/newview/llviewermessage.cpp b/indra/newview/llviewermessage.cpp
index 4fe5beceae..d1c773171b 100644
--- a/indra/newview/llviewermessage.cpp
+++ b/indra/newview/llviewermessage.cpp
@@ -2600,10 +2600,11 @@ void process_chat_from_simulator(LLMessageSystem *msg, void **user_data)
BOOL ircstyle = FALSE;
- chat.mIsScript = (mesg.substr(0, LUA_PREFIX.size()) == LUA_PREFIX);
+ auto [message, is_script] = LLStringUtil::withoutPrefix(mesg, LUA_PREFIX);
+ chat.mIsScript = is_script;
// Look for IRC-style emotes here so chatbubbles work
- std::string prefix = mesg.substr(chat.mIsScript ? LUA_PREFIX.size() : 0, 4);
+ std::string prefix = message.substr(0, 4);
if (prefix == "/me " || prefix == "/me'")
{
ircstyle = TRUE;
diff --git a/indra/newview/scripts/lua/test_LLChat.lua b/indra/newview/scripts/lua/test_LLChat.lua
index 95bd218baa..3c5cbeeeb2 100644
--- a/indra/newview/scripts/lua/test_LLChat.lua
+++ b/indra/newview/scripts/lua/test_LLChat.lua
@@ -2,17 +2,17 @@ LLChat = require 'LLChat'
function generateRandomWord(length)
local alphabet = "abcdefghijklmnopqrstuvwxyz"
- local word = ""
+ local word = {}
for i = 1, length do
local randomIndex = math.random(1, #alphabet)
- word = word .. alphabet:sub(randomIndex, randomIndex)
+ table.insert(word, alphabet:sub(randomIndex, randomIndex))
end
- return word
+ return table.concat(word)
end
-local msg = ""
+local msg = {'AI says:'}
math.randomseed(os.time())
for i = 1, math.random(1, 10) do
- msg = msg .. " ".. generateRandomWord(math.random(1, 8))
+ table.insert(msg, generateRandomWord(math.random(1, 8)))
end
-LLChat.sendNearby(msg)
+LLChat.sendNearby(table.concat(msg, ' '))