X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fstdalgo.h;h=d69f50bb244afd71537ac750c54d1c1dd4980a6f;hb=914d8140d98dd0adc54f739dfe550765cc466bac;hp=f4465963a65f5bd0884831d647ca5f4ffea65799;hpb=5378b913a69f0f8f527eff3d77862cb29df3476e;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/stdalgo.h b/include/stdalgo.h index f4465963a..d69f50bb2 100644 --- a/include/stdalgo.h +++ b/include/stdalgo.h @@ -95,6 +95,25 @@ namespace stdalgo return (!strcasecmp(tocstr(str1), tocstr(str2))); } + /** Joins the contents of a vector to a string. + * @param sequence Zero or more items to join. + * @param separator The character to place between the items, defaults to ' ' (space). + * @return The joined string. + */ + template + inline std::string join(const Collection& sequence, char separator = ' ') + { + std::string joined; + if (sequence.empty()) + return joined; + + for (typename Collection::const_iterator iter = sequence.begin(); iter != sequence.end(); ++iter) + joined.append(ConvToStr(*iter)).push_back(separator); + + joined.erase(joined.end() - 1); + return joined; + } + /** Replace first occurrence of a substring ('target') in a string ('str') with another string ('replacement'). * @param str String to perform replacement in * @param target String to replace @@ -191,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 + 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 + inline void escape(const std::string& str, std::string& out) + { + escape(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 + 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 + inline bool unescape(const std::string& str, std::string& out) + { + return unescape(str, out); + } + } }