InspIRCd  4.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-2009 Robin Burchell <[email protected]>
12  * Copyright (C) 2007 Dennis Friis <[email protected]>
13  * Copyright (C) 2005-2010 Craig Edwards <[email protected]>
14  *
15  * This file is part of InspIRCd. InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 
29 #pragma once
30 
31 #include "inspircd.h"
32 #include "convto.h"
33 
34 /*******************************************************
35  * This file contains classes and templates that deal
36  * with the comparison and hashing of 'irc strings'.
37  * An 'irc string' is a string which compares in a
38  * case insensitive manner, and as per RFC 1459 will
39  * treat [ identical to {, ] identical to }, and \
40  * as identical to |.
41  *
42  * There are functors that accept std::string and
43  * compare/hash them as type irc::string by using
44  * mapping arrays internally.
45  *******************************************************/
46 
53 CoreExport extern unsigned const char *national_case_insensitive_map;
54 
59 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
60 
63 namespace irc
64 {
72  CoreExport bool equals(const std::string_view& s1, const std::string_view& s2);
73 
79  CoreExport size_t find(const std::string& haystack, const std::string& needle);
80 
85  struct StrHashComp
86  {
89  bool operator()(const std::string& s1, const std::string& s2) const
90  {
91  return equals(s1, s2);
92  }
93  };
94 
95  struct insensitive
96  {
97  size_t CoreExport operator()(const std::string &s) const;
98  };
99 
101  {
102  bool CoreExport operator()(const std::string& a, const std::string& b) const;
103  };
104 
110  class CoreExport sepstream
111  {
112  protected:
115  std::string tokens;
118  char sep;
121  size_t pos = 0;
125  public:
128  sepstream(const std::string &source, char separator, bool allowempty = false);
129 
134  bool GetToken(std::string& token);
135 
140  template<typename Numeric>
141  bool GetNumericToken(Numeric& token)
142  {
143  std::string str;
144  if (!GetToken(str))
145  return false;
146 
147  token = ConvToNum<Numeric>(str);
148  return true;
149  }
150 
154  const std::string GetRemaining();
155 
159  bool StreamEnd();
160 
165  bool Contains(const std::string& value);
166  };
167 
170  class CoreExport commasepstream : public sepstream
171  {
172  public:
175  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
176  {
177  }
178  };
179 
182  class CoreExport spacesepstream : public sepstream
183  {
184  public:
187  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
188  {
189  }
190  };
191 
204  class CoreExport tokenstream
205  {
206  private:
208  std::string message;
209 
211  size_t position = 0;
212 
213  public:
215  tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
216 
218  std::string& GetMessage() { return message; }
219 
224  bool GetMiddle(std::string& token);
225 
230  bool GetTrailing(std::string& token);
231  };
232 
241  class CoreExport portparser
242  {
243  private:
244 
247  commasepstream sep;
248 
251  long in_range;
252 
255  long range_begin;
256 
259  long range_end;
260 
263  bool overlapped;
264 
268  std::set<long> overlap_set;
269 
272  bool Overlaps(long val);
273  public:
274 
279  portparser(const std::string &source, bool allow_overlapped = true);
280 
284  long GetToken();
285  };
286 }
Definition: hashcomp.h:171
commasepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:175
Definition: hashcomp.h:242
Definition: hashcomp.h:111
bool GetNumericToken(Numeric &token)
Definition: hashcomp.h:141
bool allow_empty
Definition: hashcomp.h:124
char sep
Definition: hashcomp.h:118
std::string tokens
Definition: hashcomp.h:115
Definition: hashcomp.h:183
spacesepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:187
Definition: hashcomp.h:205
std::string & GetMessage()
Definition: hashcomp.h:218
Definition: hashcomp.h:64
bool equals(const std::string_view &s1, const std::string_view &s2)
Definition: hashcomp.cpp:90
size_t find(const std::string &haystack, const std::string &needle)
Definition: hashcomp.cpp:106
Definition: hashcomp.h:86
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:89
Definition: hashcomp.h:101
Definition: hashcomp.h:96