2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 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 * @param vect Vector to remove the element from
32 * @param it Iterator to the element to remove
33 * @return Nothing, but all iterators, references and pointers to the erased element and the
34 * last element are invalidated
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>());
186 * Remove an element from a container
187 * @param cont Container to remove the element from
188 * @param val Value of the element to look for and remove
189 * @return True if the element was found and removed, false otherwise
191 template <template<typename, typename> class Cont, typename T, typename Alloc>
192 inline bool erase(Cont<T, Alloc>& cont, const T& val)
194 const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
195 if (it != cont.end())
204 * Check if an element with the given value is in a container. Equivalent to (std::find(cont.begin(), cont.end(), val) != cont.end()).
205 * @param cont Container to find the element in
206 * @param val Value of the element to look for
207 * @return True if the element was found in the container, false otherwise
209 template <template<typename, typename> class Cont, typename T, typename Alloc>
210 inline bool isin(const Cont<T, Alloc>& cont, const T& val)
212 return (std::find(cont.begin(), cont.end(), val) != cont.end());
219 * @param str String to escape
220 * @param out Output, must not be the same string as str
222 template <char from, char to, char esc>
223 inline void escape(const std::string& str, std::string& out)
225 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
243 * Escape a string using the backslash character as the escape character
244 * @param str String to escape
245 * @param out Output, must not be the same string as str
247 template <char from, char to>
248 inline void escape(const std::string& str, std::string& out)
250 escape<from, to, '\\'>(str, out);
255 * @param str String to unescape
256 * @param out Output, must not be the same string as str
257 * @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
259 template<char from, char to, char esc>
260 inline bool unescape(const std::string& str, std::string& out)
262 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
274 else if (nextc != to)
275 return false; // Invalid escape sequence
285 * Unescape a string using the backslash character as the escape character
286 * @param str String to unescape
287 * @param out Output, must not be the same string as str
288 * @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
290 template <char from, char to>
291 inline bool unescape(const std::string& str, std::string& out)
293 return unescape<from, to, '\\'>(str, out);