Skip to content

Commit

Permalink
Remove chat escape sequences from chat messages, for future colored c…
Browse files Browse the repository at this point in the history
…hat.
  • Loading branch information
Ekdohibs authored and est31 committed Mar 15, 2016
1 parent af30183 commit 095f623
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/chat.cpp
Expand Up @@ -679,6 +679,9 @@ ChatBackend::~ChatBackend()

void ChatBackend::addMessage(std::wstring name, std::wstring text)
{
name = removeChatEscapes(name);
text = removeChatEscapes(text);

// Note: A message may consist of multiple lines, for example the MOTD.
WStrfnd fnd(text);
while (!fnd.atend())
Expand Down
27 changes: 27 additions & 0 deletions src/util/string.cpp
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/util/string.h
Expand Up @@ -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.
Expand Down

0 comments on commit 095f623

Please sign in to comment.