X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=include%2Fstdalgo.h;h=f4465963a65f5bd0884831d647ca5f4ffea65799;hb=74dd288542e28f3604306cc69468f88b14c1b3c5;hp=3e00a4cdcd93030cdb2d3c56e1b48dac24599261;hpb=48f8f79317a04891e2becd859363add6eb2d6444;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/stdalgo.h b/include/stdalgo.h index 3e00a4cdc..f4465963a 100644 --- a/include/stdalgo.h +++ b/include/stdalgo.h @@ -64,6 +64,73 @@ namespace stdalgo } } + namespace string + { + /** Get underlying C string of the string passed as parameter. Useful in template functions. + * @param str C string + * @return Same as input + */ + inline const char* tocstr(const char* str) + { + return str; + } + + /** Get underlying C string of the string passed as parameter. Useful in template functions. + * @param str std::string object + * @return str.c_str() + */ + inline const char* tocstr(const std::string& str) + { + return str.c_str(); + } + + /** Check if two strings are equal case insensitively. + * @param str1 First string to compare. + * @param str2 Second string to compare. + * @return True if the strings are equal case-insensitively, false otherwise. + */ + template + inline bool equalsci(const S1& str1, const S2& str2) + { + return (!strcasecmp(tocstr(str1), tocstr(str2))); + } + + /** 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 + * @param replacement String to put in place of 'target' + * @return True if 'target' was replaced with 'replacement', false if it was not found in 'str'. + */ + template + inline bool replace(std::basic_string& str, const std::basic_string& target, const std::basic_string& replacement) + { + const typename std::basic_string::size_type p = str.find(target); + if (p == std::basic_string::npos) + return false; + str.replace(p, target.size(), replacement); + return true; + } + + /** Replace all occurrences of a string ('target') in a string ('str') with another string ('replacement'). + * @param str String to perform replacement in + * @param target String to replace + * @param replacement String to put in place of 'target' + */ + template + inline void replace_all(std::basic_string& str, const std::basic_string& target, const std::basic_string& replacement) + { + if (target.empty()) + return; + + typename std::basic_string::size_type p = 0; + while ((p = str.find(target, p)) != std::basic_string::npos) + { + str.replace(p, target.size(), replacement); + p += replacement.size(); + } + } + } + /** * Deleter that uses operator delete to delete the item */