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 Uli Schlachter <[email protected]>
10  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
11  * Copyright (C) 2008 Robin Burchell <[email protected]>
12  * Copyright (C) 2008 Pippijn van Steenhoven <[email protected]>
13  * Copyright (C) 2007 Dennis Friis <[email protected]>
14  * Copyright (C) 2006-2010 Craig Edwards <[email protected]>
15  *
16  * This file is part of InspIRCd. InspIRCd is free software: you can
17  * redistribute it and/or modify it under the terms of the GNU General Public
18  * License as published by the Free Software Foundation, version 2.
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program. If not, see <http://www.gnu.org/licenses/>.
27  */
28 
29 
30 #pragma once
31 
32 #include <cstring>
33 #include <string>
34 #include <vector>
35 #include <deque>
36 #include <map>
37 #include <set>
38 #include "inspircd.h"
39 #include "convto.h"
40 
41 /*******************************************************
42  * This file contains classes and templates that deal
43  * with the comparison and hashing of 'irc strings'.
44  * An 'irc string' is a string which compares in a
45  * case insensitive manner, and as per RFC 1459 will
46  * treat [ identical to {, ] identical to }, and \
47  * as identical to |.
48  *
49  * There are functors that accept std::string and
50  * compare/hash them as type irc::string by using
51  * mapping arrays internally.
52  *******************************************************/
53 
60 CoreExport extern unsigned const char *national_case_insensitive_map;
61 
65 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
66 
71 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
72 
75 namespace irc
76 {
84  CoreExport bool equals(const std::string& s1, const std::string& s2);
85 
91  CoreExport size_t find(const std::string& haystack, const std::string& needle);
92 
97  struct StrHashComp
98  {
101  bool operator()(const std::string& s1, const std::string& s2) const
102  {
103  return equals(s1, s2);
104  }
105  };
106 
107  struct insensitive
108  {
109  size_t CoreExport operator()(const std::string &s) const;
110  };
111 
113  {
114  bool CoreExport operator()(const std::string& a, const std::string& b) const;
115  };
116 
122  class CoreExport sepstream
123  {
124  protected:
127  std::string tokens;
130  char sep;
133  size_t pos;
137  public:
140  sepstream(const std::string &source, char separator, bool allowempty = false);
141 
146  bool GetToken(std::string& token);
147 
152  template<typename Numeric>
153  bool GetNumericToken(Numeric& token)
154  {
155  std::string str;
156  if (!GetToken(str))
157  return false;
158 
159  token = ConvToNum<Numeric>(str);
160  return true;
161  }
162 
166  const std::string GetRemaining();
167 
171  bool StreamEnd();
172 
177  bool Contains(const std::string& value);
178  };
179 
182  class CoreExport commasepstream : public sepstream
183  {
184  public:
187  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
188  {
189  }
190  };
191 
194  class CoreExport spacesepstream : public sepstream
195  {
196  public:
199  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
200  {
201  }
202  };
203 
216  class CoreExport tokenstream
217  {
218  private:
220  std::string message;
221 
223  size_t position;
224 
225  public:
227  tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
228 
230  std::string& GetMessage() { return message; }
231 
236  bool GetMiddle(std::string& token);
237 
242  bool GetTrailing(std::string& token);
243  };
244 
253  class CoreExport portparser
254  {
255  private:
256 
259  commasepstream sep;
260 
263  long in_range;
264 
267  long range_begin;
268 
271  long range_end;
272 
275  bool overlapped;
276 
280  std::set<long> overlap_set;
281 
284  bool Overlaps(long val);
285  public:
286 
291  portparser(const std::string &source, bool allow_overlapped = true);
292 
296  long GetToken();
297  };
298 }
irc::StrHashComp::operator()
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:101
irc::sepstream::sep
char sep
Definition: hashcomp.h:130
irc::sepstream::pos
size_t pos
Definition: hashcomp.h:133
irc::insensitive_swo
Definition: hashcomp.h:112
irc::spacesepstream::spacesepstream
spacesepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:199
irc::tokenstream::GetMessage
std::string & GetMessage()
Definition: hashcomp.h:230
irc::sepstream::allow_empty
bool allow_empty
Definition: hashcomp.h:136
irc::sepstream
Definition: hashcomp.h:122
irc::tokenstream
Definition: hashcomp.h:216
irc::commasepstream::commasepstream
commasepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:187
irc::portparser
Definition: hashcomp.h:253
irc::sepstream::GetNumericToken
bool GetNumericToken(Numeric &token)
Definition: hashcomp.h:153
irc::commasepstream
Definition: hashcomp.h:182
irc::equals
bool equals(const std::string &s1, const std::string &s2)
Definition: hashcomp.cpp:127
irc
Definition: hashcomp.h:75
irc::insensitive
Definition: hashcomp.h:107
irc::find
size_t find(const std::string &haystack, const std::string &needle)
Definition: hashcomp.cpp:137
irc::spacesepstream
Definition: hashcomp.h:194
irc::StrHashComp
Definition: hashcomp.h:97
irc::sepstream::tokens
std::string tokens
Definition: hashcomp.h:127