X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fstdalgo.h;h=dd97e8efa2e1447900000fdcbb1d7322c3a7e5a3;hb=e94b673532f7833aaa4789f834e61d68e0b4fc56;hp=f4465963a65f5bd0884831d647ca5f4ffea65799;hpb=565544fac966b14e046bb3042ab485f79bcf7c9e;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/stdalgo.h b/include/stdalgo.h index f4465963a..dd97e8efa 100644 --- a/include/stdalgo.h +++ b/include/stdalgo.h @@ -1,7 +1,8 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2014 Attila Molnar + * Copyright (C) 2018, 2020-2021 Sadie Powell + * Copyright (C) 2014, 2016, 2018 Attila Molnar * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public @@ -27,10 +28,10 @@ namespace stdalgo * Erase a single element from a vector by overwriting it with a copy of the last element, * which is then removed. This, in contrast to vector::erase(), does not result in all * elements after the erased element being moved. + * Returns nothing, but all iterators, references and pointers to the erased element and the + * last element are invalidated * @param vect Vector to remove the element from * @param it Iterator to the element to remove - * @return Nothing, but all iterators, references and pointers to the erased element and the - * last element are invalidated */ template inline void swaperase(typename std::vector& vect, const typename std::vector::iterator& it) @@ -95,6 +96,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 @@ -162,6 +182,17 @@ namespace stdalgo std::for_each(cont.begin(), cont.end(), defaultdeleter()); } + /** Deletes a object and zeroes the memory location that pointed to it. + * @param pr A reference to the pointer that contains the object to delete. + */ + template + void delete_zero(T*& pr) + { + T* p = pr; + pr = NULL; + delete p; + } + /** * Remove an element from a container * @param cont Container to remove the element from @@ -191,4 +222,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); + } + } }