From 095f623fa7278375a6fa4fe01ed39d2b68cce7af Mon Sep 17 00:00:00 2001
From: Ekdohibs <nathanael.courant@laposte.net>
Date: Tue, 15 Mar 2016 13:34:27 +0100
Subject: Remove chat escape sequences from chat messages, for future colored
 chat.

---
 src/util/string.cpp | 27 +++++++++++++++++++++++++++
 src/util/string.h   |  7 +++++++
 2 files changed, 34 insertions(+)

(limited to 'src/util')

diff --git a/src/util/string.cpp b/src/util/string.cpp
index 2c4143c76..c8f528a77 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -729,6 +729,33 @@ static bool parseNamedColorString(const std::string &value, video::SColor &color
 	return true;
 }
 
+std::wstring removeChatEscapes(const std::wstring &s) {
+	std::wstring output;
+	size_t i = 0;
+	while (i < s.length()) {
+		if (s[i] == L'\v') {
+			++i;
+			if (i == s.length()) continue;
+			if (s[i] == L'(') {
+				++i;
+				while (i < s.length() && s[i] != L')') {
+					if (s[i] == L'\\') {
+						++i;
+					}
+					++i;
+				}
+				++i;
+			} else {
+				++i;
+			}
+			continue;
+		}
+		output += s[i];
+		++i;
+	}
+	return output;
+}
+
 void str_replace(std::string &str, char from, char to)
 {
 	std::replace(str.begin(), str.end(), from, to);
diff --git a/src/util/string.h b/src/util/string.h
index cf94b7f5f..9e59ab20a 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -386,6 +386,13 @@ inline void str_replace(std::string &str, const std::string &pattern,
 	}
 }
 
+/**
+ * Remove all chat escape sequences in \p s.
+ *
+ * @param s The string in which to remove escape sequences.
+ * @return \p s, with escape sequences removed.
+ */
+std::wstring removeChatEscapes(const std::wstring &s);
 
 /**
  * Replace all occurrences of the character \p from in \p str with \p to.
-- 
cgit v1.2.3