diff options
author | Peter Powell <petpow@saberuk.com> | 2018-08-13 20:17:46 +0100 |
---|---|---|
committer | Peter Powell <petpow@saberuk.com> | 2018-08-13 21:51:11 +0100 |
commit | 58a0a7e01422e62de1565a8eb0a1febdc463d04d (patch) | |
tree | 8861789deefe9df3524690de8ccd11e5366f1f2e /include/stdalgo.h | |
parent | e2a820cce21342478653a34cf8ce2b593128d035 (diff) |
Implement IRCv3 message tag support.
Co-authored-by: Attila Molnar <attilamolnar@hush.com>
Diffstat (limited to 'include/stdalgo.h')
-rw-r--r-- | include/stdalgo.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/include/stdalgo.h b/include/stdalgo.h index bb5e12262..d69f50bb2 100644 --- a/include/stdalgo.h +++ b/include/stdalgo.h @@ -210,4 +210,86 @@ namespace stdalgo { return (std::find(cont.begin(), cont.end(), val) != cont.end()); } + + namespace string + { + /** + * Escape a string + * @param str String to escape + * @param out Output, must not be the same string as str + */ + template <char from, char to, char esc> + inline void escape(const std::string& str, std::string& out) + { + for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) + { + char c = *i; + if (c == esc) + out.append(2, esc); + else + { + if (c == from) + { + out.push_back(esc); + c = to; + } + out.push_back(c); + } + } + } + + /** + * Escape a string using the backslash character as the escape character + * @param str String to escape + * @param out Output, must not be the same string as str + */ + template <char from, char to> + inline void escape(const std::string& str, std::string& out) + { + escape<from, to, '\\'>(str, out); + } + + /** + * Unescape a string + * @param str String to unescape + * @param out Output, must not be the same string as str + * @return True if the string was unescaped, false if an invalid escape sequence is present in the input in which case out will contain a partially unescaped string + */ + template<char from, char to, char esc> + inline bool unescape(const std::string& str, std::string& out) + { + for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) + { + char c = *i; + if (c == '\\') + { + ++i; + if (i == str.end()) + return false; + + char nextc = *i; + if (nextc == esc) + c = esc; + else if (nextc != to) + return false; // Invalid escape sequence + else + c = from; + } + out.push_back(c); + } + return true; + } + + /** + * Unescape a string using the backslash character as the escape character + * @param str String to unescape + * @param out Output, must not be the same string as str + * @return True if the string was unescaped, false if an invalid escape sequence is present in the input in which case out will contain a partially unescaped string + */ + template <char from, char to> + inline bool unescape(const std::string& str, std::string& out) + { + return unescape<from, to, '\\'>(str, out); + } + } } |