mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
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.
This commit is contained in:
parent
21079cc8eb
commit
48939df9a5
8 changed files with 105 additions and 83 deletions
|
@ -386,14 +386,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.
|
||||
*
|
||||
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue