Cppcheck
matchcompiler.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more 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 #ifndef matchcompilerH
21 #define matchcompilerH
22 
23 #include <string>
24 
25 namespace MatchCompiler {
26 
27  template<unsigned int n>
28  class ConstString {
29  public:
30  using StringRef = const char (&)[n];
31  explicit ConstString(StringRef s)
32  : _s(s) {}
33 
34  operator StringRef() const {
35  return _s;
36  }
37 
38  private:
40  };
41 
42  template<unsigned int n>
43  inline bool equalN(const char s1[], const char s2[])
44  {
45  return (*s1 == *s2) && equalN<n-1>(s1+1, s2+1);
46  }
47 
48  template<>
49  inline bool equalN<0>(const char /*s1*/[], const char /*s2*/[])
50  {
51  return true;
52  }
53 
54  template<unsigned int n>
55  inline bool operator==(const std::string & s1, ConstString<n> const & s2)
56  {
57  return equalN<n>(s1.c_str(), s2);
58  }
59 
60  template<unsigned int n>
61  inline bool operator!=(const std::string & s1, ConstString<n> const & s2)
62  {
63  return !operator==(s1,s2);
64  }
65 
66  template<unsigned int n>
67  inline ConstString<n> makeConstString(const char (&s)[n])
68  {
69  return ConstString<n>(s);
70  }
71 }
72 
73 #endif // matchcompilerH
74 
const char(&)[n] StringRef
Definition: matchcompiler.h:30
bool equalN(const char s1[], const char s2[])
Definition: matchcompiler.h:43
ConstString< n > makeConstString(const char(&s)[n])
Definition: matchcompiler.h:67
bool operator==(const std::string &s1, ConstString< n > const &s2)
Definition: matchcompiler.h:55
bool operator!=(const std::string &s1, ConstString< n > const &s2)
Definition: matchcompiler.h:61
bool equalN< 0 >(const char[], const char[])
Definition: matchcompiler.h:49