2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018, 2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2014, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * Erase a single element from a vector by overwriting it with a copy of the last element,
29 * which is then removed. This, in contrast to vector::erase(), does not result in all
30 * elements after the erased element being moved.
31 * Returns nothing, but all iterators, references and pointers to the erased element and the
32 * last element are invalidated
33 * @param vect Vector to remove the element from
34 * @param it Iterator to the element to remove
37 inline void swaperase(typename std::vector<T>& vect, const typename std::vector<T>::iterator& it)
44 * Find and if exists, erase a single element from a vector by overwriting it with a
45 * copy of the last element, which is then removed. This, in contrast to vector::erase(),
46 * does not result in all elements after the erased element being moved.
47 * If the given value occurs multiple times, the one with the lowest index is removed.
48 * Individual elements are compared to the given value using operator==().
49 * @param vect Vector to remove the element from
50 * @param val Value of the element to look for and remove
51 * @return True if the element was found and removed, false if it wasn't found.
52 * If true, all iterators, references and pointers pointing to either the first element that
53 * is equal to val or to the last element are invalidated.
56 inline bool swaperase(typename std::vector<T>& vect, const T& val)
58 const typename std::vector<T>::iterator it = std::find(vect.begin(), vect.end(), val);
70 /** Get underlying C string of the string passed as parameter. Useful in template functions.
72 * @return Same as input
74 inline const char* tocstr(const char* str)
79 /** Get underlying C string of the string passed as parameter. Useful in template functions.
80 * @param str std::string object
83 inline const char* tocstr(const std::string& str)
88 /** Check if two strings are equal case insensitively.
89 * @param str1 First string to compare.
90 * @param str2 Second string to compare.
91 * @return True if the strings are equal case-insensitively, false otherwise.
93 template <typename S1, typename S2>
94 inline bool equalsci(const S1& str1, const S2& str2)
96 return (!strcasecmp(tocstr(str1), tocstr(str2)));
99 /** Joins the contents of a vector to a string.
100 * @param sequence Zero or more items to join.
101 * @param separator The character to place between the items, defaults to ' ' (space).
102 * @return The joined string.
104 template<typename Collection>
105 inline std::string join(const Collection& sequence, char separator = ' ')
108 if (sequence.empty())
111 for (typename Collection::const_iterator iter = sequence.begin(); iter != sequence.end(); ++iter)
112 joined.append(ConvToStr(*iter)).push_back(separator);
114 joined.erase(joined.end() - 1);
118 /** Replace first occurrence of a substring ('target') in a string ('str') with another string ('replacement').
119 * @param str String to perform replacement in
120 * @param target String to replace
121 * @param replacement String to put in place of 'target'
122 * @return True if 'target' was replaced with 'replacement', false if it was not found in 'str'.
124 template<typename CharT, typename Traits, typename Alloc>
125 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)
127 const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
128 if (p == std::basic_string<CharT, Traits, Alloc>::npos)
130 str.replace(p, target.size(), replacement);
134 /** Replace all occurrences of a string ('target') in a string ('str') with another string ('replacement').
135 * @param str String to perform replacement in
136 * @param target String to replace
137 * @param replacement String to put in place of 'target'
139 template<typename CharT, typename Traits, typename Alloc>
140 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)
145 typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
146 while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
148 str.replace(p, target.size(), replacement);
149 p += replacement.size();
155 * Deleter that uses operator delete to delete the item
157 template <typename T>
158 struct defaultdeleter
160 void operator()(T* o)
167 * Deleter that adds the item to the cull list, that is, queues it for
168 * deletion at the end of the current mainloop iteration
172 void operator()(classbase* item);
176 * Deletes all elements in a container using operator delete
177 * @param cont The container containing the elements to delete
179 template <template<typename, typename> class Cont, typename T, typename Alloc>
180 inline void delete_all(const Cont<T*, Alloc>& cont)
182 std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
185 /** Deletes a object and zeroes the memory location that pointed to it.
186 * @param pr A reference to the pointer that contains the object to delete.
189 void delete_zero(T*& pr)
197 * Remove an element from a container
198 * @param cont Container to remove the element from
199 * @param val Value of the element to look for and remove
200 * @return True if the element was found and removed, false otherwise
202 template <template<typename, typename> class Cont, typename T, typename Alloc>
203 inline bool erase(Cont<T, Alloc>& cont, const T& val)
205 const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
206 if (it != cont.end())
215 * Check if an element with the given value is in a container. Equivalent to (std::find(cont.begin(), cont.end(), val) != cont.end()).
216 * @param cont Container to find the element in
217 * @param val Value of the element to look for
218 * @return True if the element was found in the container, false otherwise
220 template <template<typename, typename> class Cont, typename T, typename Alloc>
221 inline bool isin(const Cont<T, Alloc>& cont, const T& val)
223 return (std::find(cont.begin(), cont.end(), val) != cont.end());
230 * @param str String to escape
231 * @param out Output, must not be the same string as str
233 template <char from, char to, char esc>
234 inline void escape(const std::string& str, std::string& out)
236 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
254 * Escape a string using the backslash character as the escape character
255 * @param str String to escape
256 * @param out Output, must not be the same string as str
258 template <char from, char to>
259 inline void escape(const std::string& str, std::string& out)
261 escape<from, to, '\\'>(str, out);
266 * @param str String to unescape
267 * @param out Output, must not be the same string as str
268 * @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
270 template<char from, char to, char esc>
271 inline bool unescape(const std::string& str, std::string& out)
273 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
285 else if (nextc != to)
286 return false; // Invalid escape sequence
296 * Unescape a string using the backslash character as the escape character
297 * @param str String to unescape
298 * @param out Output, must not be the same string as str
299 * @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
301 template <char from, char to>
302 inline bool unescape(const std::string& str, std::string& out)
304 return unescape<from, to, '\\'>(str, out);