summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorEkdohibs <nathanael.courant@laposte.net>2016-04-04 18:31:00 +0200
committerCraig Robbins <kde.psych@gmail.com>2016-04-24 03:54:11 +1000
commit48939df9a5ef1ff20f4f9717d1341b51a50dff14 (patch)
tree7325a70d4dbe445d4e3726f0aea402f3181906b4 /src/util
parent21079cc8ebae0bf694c1903c07bf3e1517feab99 (diff)
downloadminetest-48939df9a5ef1ff20f4f9717d1341b51a50dff14.tar.gz
minetest-48939df9a5ef1ff20f4f9717d1341b51a50dff14.tar.bz2
minetest-48939df9a5ef1ff20f4f9717d1341b51a50dff14.zip
Escape more strings: formspecs, item descriptions, infotexts...
Also, change the escape character to the more standard \x1b Thus, it can be used in the future for translation or colored text, for example.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/string.cpp27
-rw-r--r--src/util/string.h44
2 files changed, 35 insertions, 36 deletions
diff --git a/src/util/string.cpp b/src/util/string.cpp
index c8f528a77..2c4143c76 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -729,33 +729,6 @@ 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 9e59ab20a..40ef3e4d3 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -387,14 +387,6 @@ 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.
*
* @param str The string to (potentially) modify.
@@ -476,7 +468,7 @@ inline std::string wrap_rows(const std::string &from,
* Removes backslashes from an escaped string (FormSpec strings)
*/
template <typename T>
-inline std::basic_string<T> unescape_string(std::basic_string<T> &s)
+inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
{
std::basic_string<T> res;
@@ -492,6 +484,40 @@ inline std::basic_string<T> unescape_string(std::basic_string<T> &s)
return res;
}
+/**
+ * Remove all escape sequences in \p s.
+ *
+ * @param s The string in which to remove escape sequences.
+ * @return \p s, with escape sequences removed.
+ */
+template <typename T>
+std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
+{
+ std::basic_string<T> output;
+ size_t i = 0;
+ while (i < s.length()) {
+ if (s[i] == '\x1b') {
+ ++i;
+ if (i == s.length()) continue;
+ if (s[i] == '(') {
+ ++i;
+ while (i < s.length() && s[i] != ')') {
+ if (s[i] == '\\') {
+ ++i;
+ }
+ ++i;
+ }
+ ++i;
+ } else {
+ ++i;
+ }
+ continue;
+ }
+ output += s[i];
+ ++i;
+ }
+ return output;
+}
/**
* Checks that all characters in \p to_check are a decimal digits.