InspIRCd  3.0
stdalgo.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2018, 2020 Sadie Powell <[email protected]>
5  * Copyright (C) 2014, 2016, 2018 Attila Molnar <[email protected]>
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  {
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 
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  {
74  inline const char* tocstr(const char* str)
75  {
76  return str;
77  }
78 
83  inline const char* tocstr(const std::string& str)
84  {
85  return str.c_str();
86  }
87 
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 
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 
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 
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 
157  template <typename T>
159  {
160  void operator()(T* o)
161  {
162  delete o;
163  }
164  };
165 
170  struct culldeleter
171  {
172  void operator()(classbase* item);
173  };
174 
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 
188  template<typename T>
189  void delete_zero(T*& pr)
190  {
191  T* p = pr;
192  pr = NULL;
193  delete p;
194  }
195 
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 
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  {
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 
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 
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 
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 }
stdalgo::defaultdeleter
Definition: stdalgo.h:158
classbase
Definition: base.h:46
stdalgo::culldeleter
Definition: stdalgo.h:170