MySQL 9.1.0
Source Code Documentation
matcher.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQLROUTER_MATCHER_INCLUDED
27#define MYSQLROUTER_MATCHER_INCLUDED
28
29#include <algorithm>
30
31// single char matcher
32//
33// API is inspired by PEGTL.
34
35namespace Matcher {
36
37/**
38 * matches a Range of characters.
39 */
40template <char S, char E>
41class Range {
42 public:
43 static constexpr bool match(char c) {
44 static_assert(S <= E, "S <= E");
45 return (S <= c) && (c <= E);
46 }
47};
48
49/**
50 * check if a initizalizer list contains a character.
51 *
52 * @note: could be constexpr with C++20
53 */
54bool contains(char c, const std::initializer_list<char> &l) {
55 return std::find(l.begin(), l.end(), c) != l.end();
56}
57
58/**
59 * matches one character in a list of possible candidates.
60 */
61template <char... Arg>
62class One;
63
64template <char Arg>
65class One<Arg> {
66 public:
67 static bool match(char c) { return Arg == c; }
68};
69
70template <char... Arg>
71class One {
72 public:
73 static bool match(char c) { return contains(c, {Arg...}); }
74};
75
76/**
77 * matches Rules left-to-right with OR.
78 */
79template <class... Rules>
80class Sor;
81
82// empty case, is false.
83template <>
84class Sor<> {
85 public:
86 static bool match(char /* c */) { return false; }
87};
88
89// generic case
90template <class... Rules>
91class Sor {
92 public:
93 static bool match(char c) {
94 bool result = false;
95
96 // emulate fold-expression for C++11 and later
97 //
98 // result = Rules::match(c) || ...;
99 //
100 // See https://en.cppreference.com/w/cpp/language/fold
101
102 // 'swallow' is just a vehicle and unused.
103 using swallow = bool[sizeof...(Rules)];
104 (void)swallow{result = result || Rules::match(c)...};
105
106 return result;
107 }
108};
109
110/**
111 * [0-9].
112 */
114
115/**
116 * [a-z].
117 */
119
120/**
121 * [A-Z].
122 */
124
125/**
126 * [a-zA-Z].
127 */
129
130/**
131 * [a-zA-Z0-9].
132 */
134
135} // namespace Matcher
136
137#endif
static bool match(char c)
Definition: matcher.h:67
matches one character in a list of possible candidates.
Definition: matcher.h:71
static bool match(char c)
Definition: matcher.h:73
matches a Range of characters.
Definition: matcher.h:41
static constexpr bool match(char c)
Definition: matcher.h:43
static bool match(char)
Definition: matcher.h:86
matches Rules left-to-right with OR.
Definition: matcher.h:91
static bool match(char c)
Definition: matcher.h:93
Definition: matcher.h:35
bool contains(char c, const std::initializer_list< char > &l)
check if a initizalizer list contains a character.
Definition: matcher.h:54
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:39
struct result result
Definition: result.h:34
Definition: result.h:30