InspIRCd  3.0
stdalgo.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2014 Attila Molnar <[email protected]>
5  *
6  * This file is part of InspIRCd. InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #pragma once
21 
22 namespace stdalgo
23 {
24  namespace vector
25  {
35  template <typename T>
36  inline void swaperase(typename std::vector<T>& vect, const typename std::vector<T>::iterator& it)
37  {
38  *it = vect.back();
39  vect.pop_back();
40  }
41 
54  template <typename T>
55  inline bool swaperase(typename std::vector<T>& vect, const T& val)
56  {
57  const typename std::vector<T>::iterator it = std::find(vect.begin(), vect.end(), val);
58  if (it != vect.end())
59  {
60  swaperase(vect, it);
61  return true;
62  }
63  return false;
64  }
65  }
66 
67  namespace string
68  {
73  inline const char* tocstr(const char* str)
74  {
75  return str;
76  }
77 
82  inline const char* tocstr(const std::string& str)
83  {
84  return str.c_str();
85  }
86 
92  template <typename S1, typename S2>
93  inline bool equalsci(const S1& str1, const S2& str2)
94  {
95  return (!strcasecmp(tocstr(str1), tocstr(str2)));
96  }
97 
103  template<typename Collection>
104  inline std::string join(const Collection& sequence, char separator = ' ')
105  {
106  std::string joined;
107  if (sequence.empty())
108  return joined;
109 
110  for (typename Collection::const_iterator iter = sequence.begin(); iter != sequence.end(); ++iter)
111  joined.append(ConvToStr(*iter)).push_back(separator);
112 
113  joined.erase(joined.end() - 1);
114  return joined;
115  }
116 
123  template<typename CharT, typename Traits, typename Alloc>
124  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)
125  {
126  const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
127  if (p == std::basic_string<CharT, Traits, Alloc>::npos)
128  return false;
129  str.replace(p, target.size(), replacement);
130  return true;
131  }
132 
138  template<typename CharT, typename Traits, typename Alloc>
139  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)
140  {
141  if (target.empty())
142  return;
143 
144  typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
145  while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
146  {
147  str.replace(p, target.size(), replacement);
148  p += replacement.size();
149  }
150  }
151  }
152 
156  template <typename T>
158  {
159  void operator()(T* o)
160  {
161  delete o;
162  }
163  };
164 
169  struct culldeleter
170  {
171  void operator()(classbase* item);
172  };
173 
178  template <template<typename, typename> class Cont, typename T, typename Alloc>
179  inline void delete_all(const Cont<T*, Alloc>& cont)
180  {
181  std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
182  }
183 
190  template <template<typename, typename> class Cont, typename T, typename Alloc>
191  inline bool erase(Cont<T, Alloc>& cont, const T& val)
192  {
193  const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
194  if (it != cont.end())
195  {
196  cont.erase(it);
197  return true;
198  }
199  return false;
200  }
201 
208  template <template<typename, typename> class Cont, typename T, typename Alloc>
209  inline bool isin(const Cont<T, Alloc>& cont, const T& val)
210  {
211  return (std::find(cont.begin(), cont.end(), val) != cont.end());
212  }
213 
214  namespace string
215  {
221  template <char from, char to, char esc>
222  inline void escape(const std::string& str, std::string& out)
223  {
224  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
225  {
226  char c = *i;
227  if (c == esc)
228  out.append(2, esc);
229  else
230  {
231  if (c == from)
232  {
233  out.push_back(esc);
234  c = to;
235  }
236  out.push_back(c);
237  }
238  }
239  }
240 
246  template <char from, char to>
247  inline void escape(const std::string& str, std::string& out)
248  {
249  escape<from, to, '\\'>(str, out);
250  }
251 
258  template<char from, char to, char esc>
259  inline bool unescape(const std::string& str, std::string& out)
260  {
261  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
262  {
263  char c = *i;
264  if (c == '\\')
265  {
266  ++i;
267  if (i == str.end())
268  return false;
269 
270  char nextc = *i;
271  if (nextc == esc)
272  c = esc;
273  else if (nextc != to)
274  return false; // Invalid escape sequence
275  else
276  c = from;
277  }
278  out.push_back(c);
279  }
280  return true;
281  }
282 
289  template <char from, char to>
290  inline bool unescape(const std::string& str, std::string& out)
291  {
292  return unescape<from, to, '\\'>(str, out);
293  }
294  }
295 }
Definition: stdalgo.h:169
Definition: stdalgo.h:157
Definition: base.h:41
Definition: stdalgo.h:22