From 4a07c811962dd9c488d9ce5d6c98b617b3eace7b Mon Sep 17 00:00:00 2001
From: Kitty Barnett <develop@catznip.com>
Date: Wed, 7 Dec 2016 19:57:54 +0100
Subject: [PATCH] Make string splitting code generally available so it can be
 reused elsehwere

--HG--
branch : RLVa
---
 indra/llcommon/llstring.cpp | 35 +++++++++++++++++++++++++++++++++++
 indra/llcommon/llstring.h   |  4 ++++
 indra/newview/rlvcommon.cpp | 33 +++++----------------------------
 3 files changed, 44 insertions(+), 28 deletions(-)

diff --git a/indra/llcommon/llstring.cpp b/indra/llcommon/llstring.cpp
index 1af90c871b4..a815b15fb32 100644
--- a/indra/llcommon/llstring.cpp
+++ b/indra/llcommon/llstring.cpp
@@ -611,6 +611,41 @@ std::string utf8str_substr(const std::string& utf8str, const S32 index, const S3
 		return utf8str.substr(index, cur_char);
 	}
 }
+
+void utf8str_split(std::list<std::string>& split_list, const std::string& utf8str, size_t maxlen, char split_token)
+{
+	split_list.clear();
+
+	std::string::size_type lenMsg = utf8str.length(), lenIt = 0;
+
+	const char* pstrIt = utf8str.c_str(); std::string strTemp;
+	while (lenIt < lenMsg)
+	{
+		if (lenIt + maxlen < lenMsg)
+		{
+			// Find the last split character
+			const char* pstrTemp = pstrIt + maxlen;
+			while ( (pstrTemp > pstrIt) && (*pstrTemp != split_token) )
+				pstrTemp--;
+
+			if (pstrTemp > pstrIt)
+				strTemp = utf8str.substr(lenIt, pstrTemp - pstrIt);
+			else
+				strTemp = utf8str_substr(utf8str, lenIt, maxlen);
+		}
+		else
+		{
+			strTemp = utf8str.substr(lenIt, std::string::npos);
+		}
+
+		split_list.push_back(strTemp);
+
+		lenIt += strTemp.length();
+		pstrIt = utf8str.c_str() + lenIt;
+		if (*pstrIt == split_token)
+			lenIt++;
+	}
+}
 // [/RLVa:KB]
 
 std::string utf8str_symbol_truncate(const std::string& utf8str, const S32 symbol_len)
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h
index f7d304b55ad..c9493c30dff 100644
--- a/indra/llcommon/llstring.h
+++ b/indra/llcommon/llstring.h
@@ -35,6 +35,9 @@
 #include <vector>
 #include <map>
 #include "llformat.h"
+// [RLVa:KB] - Checked: RLVa-2.1.0
+#include <list>
+// [/RLVa:KB]
 
 #if LL_LINUX || LL_SOLARIS
 #include <wctype.h>
@@ -559,6 +562,7 @@ LL_COMMON_API std::string utf8str_truncate(const std::string& utf8str, const S32
 
 // [RLVa:KB] - Checked: RLVa-2.1.0
 LL_COMMON_API std::string utf8str_substr(const std::string& utf8str, const S32 index, const S32 max_len);
+LL_COMMON_API void utf8str_split(std::list<std::string>& split_list, const std::string& utf8str, size_t maxlen, char split_token);
 // [/RLVa:KB]
 
 LL_COMMON_API std::string utf8str_trim(const std::string& utf8str);
diff --git a/indra/newview/rlvcommon.cpp b/indra/newview/rlvcommon.cpp
index 237b10c8697..8d9a87e0341 100644
--- a/indra/newview/rlvcommon.cpp
+++ b/indra/newview/rlvcommon.cpp
@@ -613,28 +613,10 @@ void RlvUtil::sendIMMessage(const LLUUID& idRecipient, const std::string& strMsg
 	std::string strAgentName;
 	LLAgentUI::buildFullname(strAgentName);
 
-	std::string::size_type lenMsg = strMsg.length(), lenIt = 0;
-
-	const char* pstrIt = strMsg.c_str(); std::string strTemp;
-	while (lenIt < lenMsg)
+	std::list<std::string> msgList;
+	utf8str_split(msgList, strMsg, MAX_MSG_STR_LEN, chSplit);
+	for (const std::string& strMsg : msgList)
 	{
-		if (lenIt + MAX_MSG_STR_LEN < lenMsg)
-		{
-			// Find the last split character
-			const char* pstrTemp = pstrIt + MAX_MSG_STR_LEN;
-			while ( (pstrTemp > pstrIt) && (*pstrTemp != chSplit) )
-				pstrTemp--;
-
-			if (pstrTemp > pstrIt)
-				strTemp = strMsg.substr(lenIt, pstrTemp - pstrIt);
-			else
-				strTemp = utf8str_substr(strMsg, lenIt, MAX_MSG_STR_LEN);
-		}
-		else
-		{
-			strTemp = strMsg.substr(lenIt, std::string::npos);
-		}
-
 		pack_instant_message(
 			gMessageSystem,
 			gAgent.getID(),
@@ -642,16 +624,11 @@ void RlvUtil::sendIMMessage(const LLUUID& idRecipient, const std::string& strMsg
 			gAgent.getSessionID(),
 			idRecipient,
 			strAgentName.c_str(),
-			strTemp.c_str(),
-			( (!pBuddyInfo) || (pBuddyInfo->isOnline()) ) ? IM_ONLINE : IM_OFFLINE,
+			strMsg.c_str(),
+			((!pBuddyInfo) || (pBuddyInfo->isOnline())) ? IM_ONLINE : IM_OFFLINE,
 			IM_NOTHING_SPECIAL,
 			idSession);
 		gAgent.sendReliableMessage();
-
-		lenIt += strTemp.length();
-		pstrIt = strMsg.c_str() + lenIt;
-		if (*pstrIt == chSplit)
-			lenIt++;
 	}
 }
 
-- 
GitLab