]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/stdalgo.h
c0baa9dd8ef843de3050dfea8c1f8d435326032f
[user/henk/code/inspircd.git] / include / stdalgo.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018, 2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2014, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #pragma once
22
23 namespace stdalgo
24 {
25         namespace vector
26         {
27                 /**
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
35                  */
36                 template <typename T>
37                 inline void swaperase(typename std::vector<T>& vect, const typename std::vector<T>::iterator& it)
38                 {
39                         *it = vect.back();
40                         vect.pop_back();
41                 }
42
43                 /**
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.
54                  */
55                 template <typename T>
56                 inline bool swaperase(typename std::vector<T>& vect, const T& val)
57                 {
58                         const typename std::vector<T>::iterator it = std::find(vect.begin(), vect.end(), val);
59                         if (it != vect.end())
60                         {
61                                 swaperase(vect, it);
62                                 return true;
63                         }
64                         return false;
65                 }
66         }
67
68         namespace string
69         {
70                 /** Get underlying C string of the string passed as parameter. Useful in template functions.
71                  * @param str C string
72                  * @return Same as input
73                  */
74                 inline const char* tocstr(const char* str)
75                 {
76                         return str;
77                 }
78
79                 /** Get underlying C string of the string passed as parameter. Useful in template functions.
80                  * @param str std::string object
81                  * @return str.c_str()
82                  */
83                 inline const char* tocstr(const std::string& str)
84                 {
85                         return str.c_str();
86                 }
87
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.
92                  */
93                 template <typename S1, typename S2>
94                 inline bool equalsci(const S1& str1, const S2& str2)
95                 {
96                         return (!strcasecmp(tocstr(str1), tocstr(str2)));
97                 }
98
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.
103                  */
104                 template<typename Collection>
105                 inline std::string join(const Collection& sequence, char separator = ' ')
106                 {
107                         std::string joined;
108                         if (sequence.empty())
109                                 return joined;
110
111                         for (typename Collection::const_iterator iter = sequence.begin(); iter != sequence.end(); ++iter)
112                                 joined.append(ConvToStr(*iter)).push_back(separator);
113
114                         joined.erase(joined.end() - 1);
115                         return joined;
116                 }
117
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'.
123                  */
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)
126                 {
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)
129                                 return false;
130                         str.replace(p, target.size(), replacement);
131                         return true;
132                 }
133
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'
138                  */
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)
141                 {
142                         if (target.empty())
143                                 return;
144
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)
147                         {
148                                 str.replace(p, target.size(), replacement);
149                                 p += replacement.size();
150                         }
151                 }
152         }
153
154         /**
155          * Deleter that uses operator delete to delete the item
156          */
157         template <typename T>
158         struct defaultdeleter
159         {
160                 void operator()(T* o)
161                 {
162                         delete o;
163                 }
164         };
165
166         /**
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
169          */
170         struct culldeleter
171         {
172                 void operator()(classbase* item);
173         };
174
175         /**
176          * Deletes all elements in a container using operator delete
177          * @param cont The container containing the elements to delete
178          */
179         template <template<typename, typename> class Cont, typename T, typename Alloc>
180         inline void delete_all(const Cont<T*, Alloc>& cont)
181         {
182                 std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
183         }
184
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.
187          */
188         template<typename T>
189         void delete_zero(T*& pr)
190         {
191                 T* p = pr;
192                 pr = NULL;
193                 delete p;
194         }
195
196         /**
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
201          */
202         template <template<typename, typename> class Cont, typename T, typename Alloc>
203         inline bool erase(Cont<T, Alloc>& cont, const T& val)
204         {
205                 const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
206                 if (it != cont.end())
207                 {
208                         cont.erase(it);
209                         return true;
210                 }
211                 return false;
212         }
213
214         /**
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
219          */
220         template <template<typename, typename> class Cont, typename T, typename Alloc>
221         inline bool isin(const Cont<T, Alloc>& cont, const T& val)
222         {
223                 return (std::find(cont.begin(), cont.end(), val) != cont.end());
224         }
225
226         namespace string
227         {
228                 /**
229                  * Escape a string
230                  * @param str String to escape
231                  * @param out Output, must not be the same string as str
232                  */
233                 template <char from, char to, char esc>
234                 inline void escape(const std::string& str, std::string& out)
235                 {
236                         for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
237                         {
238                                 char c = *i;
239                                 if (c == esc)
240                                         out.append(2, esc);
241                                 else
242                                 {
243                                         if (c == from)
244                                         {
245                                                 out.push_back(esc);
246                                                 c = to;
247                                         }
248                                         out.push_back(c);
249                                 }
250                         }
251                 }
252
253                 /**
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
257                  */
258                 template <char from, char to>
259                 inline void escape(const std::string& str, std::string& out)
260                 {
261                         escape<from, to, '\\'>(str, out);
262                 }
263
264                 /**
265                  * Unescape a string
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
269                  */
270                 template<char from, char to, char esc>
271                 inline bool unescape(const std::string& str, std::string& out)
272                 {
273                         for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
274                         {
275                                 char c = *i;
276                                 if (c == '\\')
277                                 {
278                                         ++i;
279                                         if (i == str.end())
280                                                 return false;
281
282                                         char nextc = *i;
283                                         if (nextc == esc)
284                                                 c = esc;
285                                         else if (nextc != to)
286                                                 return false; // Invalid escape sequence
287                                         else
288                                                 c = from;
289                                 }
290                                 out.push_back(c);
291                         }
292                         return true;
293                 }
294
295                 /**
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
300                  */
301                 template <char from, char to>
302                 inline bool unescape(const std::string& str, std::string& out)
303                 {
304                         return unescape<from, to, '\\'>(str, out);
305                 }
306         }
307 }