summaryrefslogtreecommitdiff
path: root/indra
diff options
context:
space:
mode:
authornat-goodspeed <nat@lindenlab.com>2024-05-24 11:09:16 -0400
committerGitHub <noreply@github.com>2024-05-24 11:09:16 -0400
commit30da8853c88e755ac5c0836f3952d16d501d3502 (patch)
tree2e730bce8741bc8130e0f822d4d15394c41c9d84 /indra
parent66a8b0ebb7fc1e6acdb0028f7ca8547153e10062 (diff)
parent94cac4c443b5acc189b5223c78eb278648846338 (diff)
Merge pull request #1565 from secondlife/lua-chatplus
Nat's ideas from PR #1547
Diffstat (limited to 'indra')
-rw-r--r--indra/llcommon/llstring.h62
-rw-r--r--indra/llui/llchat.h3
-rw-r--r--indra/newview/llchathistory.cpp9
-rw-r--r--indra/newview/llfloaterimnearbychathandler.cpp5
-rw-r--r--indra/newview/llfloaterimnearbychatlistener.cpp17
-rw-r--r--indra/newview/llfloaterimnearbychatlistener.h2
-rw-r--r--indra/newview/llviewerchat.cpp2
-rw-r--r--indra/newview/llviewermessage.cpp5
-rw-r--r--indra/newview/scripts/lua/test_LLChat.lua6
9 files changed, 84 insertions, 27 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 343b81ba8d..70d7e82970 100644
--- a/indra/llui/llchat.h
+++ b/indra/llui/llchat.h
@@ -113,7 +113,8 @@ public:
};
static const std::string LUA_PREFIX("[LUA]");
-inline std::string remove_LUA_PREFIX(const std::string &string, bool is_lua)
+inline
+std::string without_LUA_PREFIX(const std::string& string, bool is_lua)
{
if (is_lua)
{
diff --git a/indra/newview/llchathistory.cpp b/indra/newview/llchathistory.cpp
index 7ee0f3dd7a..d549f372e3 100644
--- a/indra/newview/llchathistory.cpp
+++ b/indra/newview/llchathistory.cpp
@@ -1074,8 +1074,8 @@ protected:
bool mNeedsTimeBox;
- bool mIsFromScript;
- std::string mPrefix;
+ bool mIsFromScript;
+ std::string mPrefix;
private:
boost::signals2::connection mAvatarNameCacheConnection;
@@ -1265,11 +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 = LLStringUtil::startsWith(chat.mText, LUA_PREFIX);
-
- std::string message = remove_LUA_PREFIX(chat.mText, is_lua);
+ 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'";
diff --git a/indra/newview/llfloaterimnearbychathandler.cpp b/indra/newview/llfloaterimnearbychathandler.cpp
index 0c7cd3aec4..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 = remove_LUA_PREFIX(chat_msg.mText, chat_msg.mIsScript);
+ toast_msg = msg_text;
}
bool chat_overlaps = false;
diff --git a/indra/newview/llfloaterimnearbychatlistener.cpp b/indra/newview/llfloaterimnearbychatlistener.cpp
index e1be6f7281..0618741cc4 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;
@@ -91,20 +92,14 @@ void LLFloaterIMNearbyChatListener::sendChat(LLSD const & chat_data)
}
// 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, ((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 2d7c851f2c..18a8bacfaa 100644
--- a/indra/newview/llfloaterimnearbychatlistener.h
+++ b/indra/newview/llfloaterimnearbychatlistener.h
@@ -43,7 +43,7 @@ public:
private:
void sendChat(LLSD const & chat_data);
- F64 mLastThrottleTime;
+ F64 mLastThrottleTime{ 0.0 };
};
#endif // LL_LLFLOATERIMNEARBYCHATLISTENER_H
diff --git a/indra/newview/llviewerchat.cpp b/indra/newview/llviewerchat.cpp
index ce29ceac1a..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 = remove_LUA_PREFIX(chat.mText, chat.mIsScript);
+ 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 883b82fafb..3abaf28e42 100644
--- a/indra/newview/scripts/lua/test_LLChat.lua
+++ b/indra/newview/scripts/lua/test_LLChat.lua
@@ -10,9 +10,9 @@ function generateRandomWord(length)
return table.concat(wordTable)
end
-local msg = "AI says:"
+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, ' '))