MySQL 8.3.0
Source Code Documentation
matcher.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQLROUTER_MATCHER_INCLUDED
26#define MYSQLROUTER_MATCHER_INCLUDED
27
28#include <algorithm>
29
30// single char matcher
31//
32// API is inspired by PEGTL.
33
34namespace Matcher {
35
36/**
37 * matches a Range of characters.
38 */
39template <char S, char E>
40class Range {
41 public:
42 static constexpr bool match(char c) {
43 static_assert(S <= E, "S <= E");
44 return (S <= c) && (c <= E);
45 }
46};
47
48/**
49 * check if a initizalizer list contains a character.
50 *
51 * @note: could be constexpr with C++20
52 */
53bool contains(char c, const std::initializer_list<char> &l) {
54 return std::find(l.begin(), l.end(), c) != l.end();
55}
56
57/**
58 * matches one character in a list of possible candidates.
59 */
60template <char... Arg>
61class One;
62
63template <char Arg>
64class One<Arg> {
65 public:
66 static bool match(char c) { return Arg == c; }
67};
68
69template <char... Arg>
70class One {
71 public:
72 static bool match(char c) { return contains(c, {Arg...}); }
73};
74
75/**
76 * matches Rules left-to-right with OR.
77 */
78template <class... Rules>
79class Sor;
80
81// empty case, is false.
82template <>
83class Sor<> {
84 public:
85 static bool match(char /* c */) { return false; }
86};
87
88// generic case
89template <class... Rules>
90class Sor {
91 public:
92 static bool match(char c) {
93 bool result = false;
94
95 // emulate fold-expression for C++11 and later
96 //
97 // result = Rules::match(c) || ...;
98 //
99 // See https://en.cppreference.com/w/cpp/language/fold
100
101 // 'swallow' is just a vehicle and unused.
102 using swallow = bool[sizeof...(Rules)];
103 (void)swallow{result = result || Rules::match(c)...};
104
105 return result;
106 }
107};
108
109/**
110 * [0-9].
111 */
113
114/**
115 * [a-z].
116 */
118
119/**
120 * [A-Z].
121 */
123
124/**
125 * [a-zA-Z].
126 */
128
129/**
130 * [a-zA-Z0-9].
131 */
133
134} // namespace Matcher
135
136#endif
static bool match(char c)
Definition: matcher.h:66
matches one character in a list of possible candidates.
Definition: matcher.h:70
static bool match(char c)
Definition: matcher.h:72
matches a Range of characters.
Definition: matcher.h:40
static constexpr bool match(char c)
Definition: matcher.h:42
static bool match(char)
Definition: matcher.h:85
matches Rules left-to-right with OR.
Definition: matcher.h:90
static bool match(char c)
Definition: matcher.h:92
Definition: matcher.h:34
bool contains(char c, const std::initializer_list< char > &l)
check if a initizalizer list contains a character.
Definition: matcher.h:53
const byte * find(const Pages *pages, const page_id_t &page_id) noexcept
Find a doublewrite copy of a page.
Definition: buf0dblwr.cc:3578
struct result result
Definition: result.h:33
Definition: result.h:29