]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/stdalgo.h
Use IsCTCP in blockcolor for ignoring CTCPs.
[user/henk/code/inspircd.git] / include / stdalgo.h
index 3e00a4cdcd93030cdb2d3c56e1b48dac24599261..585114fce93474127c456cfad47331bcb3d17359 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
- *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2018, 2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2014, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
  *
  * 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 <typename T>
                inline void swaperase(typename std::vector<T>& vect, const typename std::vector<T>::iterator& it)
@@ -64,6 +65,92 @@ 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 <typename S1, typename S2>
+               inline bool equalsci(const S1& str1, const S2& str2)
+               {
+                       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<typename Collection>
+               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
+                * @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<typename CharT, typename Traits, typename Alloc>
+               inline bool replace(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
+               {
+                       const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
+                       if (p == std::basic_string<CharT, Traits, Alloc>::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<typename CharT, typename Traits, typename Alloc>
+               inline void replace_all(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
+               {
+                       if (target.empty())
+                               return;
+
+                       typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
+                       while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
+                       {
+                               str.replace(p, target.size(), replacement);
+                               p += replacement.size();
+                       }
+               }
+       }
+
        /**
         * Deleter that uses operator delete to delete the item
         */
@@ -95,6 +182,17 @@ namespace stdalgo
                std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
        }
 
+       /** 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<typename T>
+       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
@@ -124,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 <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);
+               }
+       }
 }