]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/stdalgo.h
Fix STATUSMSG tag messages not including the status in the target.
[user/henk/code/inspircd.git] / include / stdalgo.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 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         /**
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
190          */
191         template <template<typename, typename> class Cont, typename T, typename Alloc>
192         inline bool erase(Cont<T, Alloc>& cont, const T& val)
193         {
194                 const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
195                 if (it != cont.end())
196                 {
197                         cont.erase(it);
198                         return true;
199                 }
200                 return false;
201         }
202
203         /**
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
208          */
209         template <template<typename, typename> class Cont, typename T, typename Alloc>
210         inline bool isin(const Cont<T, Alloc>& cont, const T& val)
211         {
212                 return (std::find(cont.begin(), cont.end(), val) != cont.end());
213         }
214
215         namespace string
216         {
217                 /**
218                  * Escape a string
219                  * @param str String to escape
220                  * @param out Output, must not be the same string as str
221                  */
222                 template <char from, char to, char esc>
223                 inline void escape(const std::string& str, std::string& out)
224                 {
225                         for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
226                         {
227                                 char c = *i;
228                                 if (c == esc)
229                                         out.append(2, esc);
230                                 else
231                                 {
232                                         if (c == from)
233                                         {
234                                                 out.push_back(esc);
235                                                 c = to;
236                                         }
237                                         out.push_back(c);
238                                 }
239                         }
240                 }
241
242                 /**
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
246                  */
247                 template <char from, char to>
248                 inline void escape(const std::string& str, std::string& out)
249                 {
250                         escape<from, to, '\\'>(str, out);
251                 }
252
253                 /**
254                  * Unescape a string
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
258                  */
259                 template<char from, char to, char esc>
260                 inline bool unescape(const std::string& str, std::string& out)
261                 {
262                         for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
263                         {
264                                 char c = *i;
265                                 if (c == '\\')
266                                 {
267                                         ++i;
268                                         if (i == str.end())
269                                                 return false;
270
271                                         char nextc = *i;
272                                         if (nextc == esc)
273                                                 c = esc;
274                                         else if (nextc != to)
275                                                 return false; // Invalid escape sequence
276                                         else
277                                                 c = from;
278                                 }
279                                 out.push_back(c);
280                         }
281                         return true;
282                 }
283
284                 /**
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
289                  */
290                 template <char from, char to>
291                 inline bool unescape(const std::string& str, std::string& out)
292                 {
293                         return unescape<from, to, '\\'>(str, out);
294                 }
295         }
296 }