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 
104  template<typename CharT, typename Traits, typename Alloc>
105  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)
106  {
107  const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
108  if (p == std::basic_string<CharT, Traits, Alloc>::npos)
109  return false;
110  str.replace(p, target.size(), replacement);
111  return true;
112  }
113 
119  template<typename CharT, typename Traits, typename Alloc>
120  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)
121  {
122  if (target.empty())
123  return;
124 
125  typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
126  while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
127  {
128  str.replace(p, target.size(), replacement);
129  p += replacement.size();
130  }
131  }
132  }
133 
137  template <typename T>
139  {
140  void operator()(T* o)
141  {
142  delete o;
143  }
144  };
145 
150  struct culldeleter
151  {
152  void operator()(classbase* item);
153  };
154 
159  template <template<typename, typename> class Cont, typename T, typename Alloc>
160  inline void delete_all(const Cont<T*, Alloc>& cont)
161  {
162  std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
163  }
164 
171  template <template<typename, typename> class Cont, typename T, typename Alloc>
172  inline bool erase(Cont<T, Alloc>& cont, const T& val)
173  {
174  const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
175  if (it != cont.end())
176  {
177  cont.erase(it);
178  return true;
179  }
180  return false;
181  }
182 
189  template <template<typename, typename> class Cont, typename T, typename Alloc>
190  inline bool isin(const Cont<T, Alloc>& cont, const T& val)
191  {
192  return (std::find(cont.begin(), cont.end(), val) != cont.end());
193  }
194 }
Definition: stdalgo.h:150
Definition: stdalgo.h:138
Definition: base.h:41
std::basic_string< char, irc_char_traits, std::allocator< char > > string
Definition: hashcomp.h:151
Definition: stdalgo.h:22