InspIRCd  3.0
hashcomp.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 linuxdaemon <[email protected]>
5  * Copyright (C) 2013, 2018-2019 Sadie Powell <[email protected]>
6  * Copyright (C) 2013 Adam <[email protected]>
7  * Copyright (C) 2012-2013, 2016 Attila Molnar <[email protected]>
8  * Copyright (C) 2012 Robby <[email protected]>
9  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
10  * Copyright (C) 2008-2009 Robin Burchell <[email protected]>
11  * Copyright (C) 2007 Dennis Friis <[email protected]>
12  * Copyright (C) 2005-2010 Craig Edwards <[email protected]>
13  *
14  * This file is part of InspIRCd. InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 #pragma once
29 
30 #include "inspircd.h"
31 #include "convto.h"
32 
33 /*******************************************************
34  * This file contains classes and templates that deal
35  * with the comparison and hashing of 'irc strings'.
36  * An 'irc string' is a string which compares in a
37  * case insensitive manner, and as per RFC 1459 will
38  * treat [ identical to {, ] identical to }, and \
39  * as identical to |.
40  *
41  * There are functors that accept std::string and
42  * compare/hash them as type irc::string by using
43  * mapping arrays internally.
44  *******************************************************/
45 
52 CoreExport extern unsigned const char *national_case_insensitive_map;
53 
57 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
58 
63 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
64 
67 namespace irc
68 {
76  CoreExport bool equals(const std::string& s1, const std::string& s2);
77 
83  CoreExport size_t find(const std::string& haystack, const std::string& needle);
84 
89  struct StrHashComp
90  {
93  bool operator()(const std::string& s1, const std::string& s2) const
94  {
95  return equals(s1, s2);
96  }
97  };
98 
99  struct insensitive
100  {
101  size_t CoreExport operator()(const std::string &s) const;
102  };
103 
105  {
106  bool CoreExport operator()(const std::string& a, const std::string& b) const;
107  };
108 
114  class CoreExport sepstream
115  {
116  protected:
119  std::string tokens;
122  char sep;
125  size_t pos;
129  public:
132  sepstream(const std::string &source, char separator, bool allowempty = false);
133 
138  bool GetToken(std::string& token);
139 
144  template<typename Numeric>
145  bool GetNumericToken(Numeric& token)
146  {
147  std::string str;
148  if (!GetToken(str))
149  return false;
150 
151  token = ConvToNum<Numeric>(str);
152  return true;
153  }
154 
158  const std::string GetRemaining();
159 
163  bool StreamEnd();
164 
169  bool Contains(const std::string& value);
170  };
171 
174  class CoreExport commasepstream : public sepstream
175  {
176  public:
179  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
180  {
181  }
182  };
183 
186  class CoreExport spacesepstream : public sepstream
187  {
188  public:
191  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
192  {
193  }
194  };
195 
208  class CoreExport tokenstream
209  {
210  private:
212  std::string message;
213 
215  size_t position;
216 
217  public:
219  tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
220 
222  std::string& GetMessage() { return message; }
223 
228  bool GetMiddle(std::string& token);
229 
234  bool GetTrailing(std::string& token);
235  };
236 
245  class CoreExport portparser
246  {
247  private:
248 
251  commasepstream sep;
252 
255  long in_range;
256 
259  long range_begin;
260 
263  long range_end;
264 
267  bool overlapped;
268 
272  std::set<long> overlap_set;
273 
276  bool Overlaps(long val);
277  public:
278 
283  portparser(const std::string &source, bool allow_overlapped = true);
284 
288  long GetToken();
289  };
290 }
Definition: hashcomp.h:175
commasepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:179
Definition: hashcomp.h:246
Definition: hashcomp.h:115
bool GetNumericToken(Numeric &token)
Definition: hashcomp.h:145
bool allow_empty
Definition: hashcomp.h:128
char sep
Definition: hashcomp.h:122
std::string tokens
Definition: hashcomp.h:119
size_t pos
Definition: hashcomp.h:125
Definition: hashcomp.h:187
spacesepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:191
Definition: hashcomp.h:209
std::string & GetMessage()
Definition: hashcomp.h:222
Definition: hashcomp.h:68
bool equals(const std::string &s1, const std::string &s2)
Definition: hashcomp.cpp:127
size_t find(const std::string &haystack, const std::string &needle)
Definition: hashcomp.cpp:137
Definition: hashcomp.h:90
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:93
Definition: hashcomp.h:105
Definition: hashcomp.h:100