MySQL 9.6.0
Source Code Documentation
repeat.h
Go to the documentation of this file.
1// Copyright (c) 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
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, version 2.0, 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#ifndef MYSQL_STRCONV_DECODE_REPEAT_H
25#define MYSQL_STRCONV_DECODE_REPEAT_H
26
27/// @file
28/// Experimental API header
29
30#include <cassert> // assert
31#include <concepts> // integral
32#include <limits> // numeric_limits
33
34/// @addtogroup GroupLibsMysqlStrconv
35/// @{
36
37namespace mysql::strconv {
38
39namespace detail {
40/// Common base class for the Repeat and Repeat_optional classes, providing
41/// functionality for representing the upper bound on the number of repetitions.
42/// Provides a constructor that takes an integral value for the upper bound on
43/// the number of repetitions, as well as several static factory functions for
44/// creating Repeat_optional objects with specific repetition ranges.
45template <class Repeat_optional_t>
47 public:
48 /// Construct a new object with the given maximum.
49 explicit Repeat_base(std::integral auto max) : m_max(std::size_t(max)) {}
50
51 /// Return a Repeat_optional object representing zero or one instances.
52 [[nodiscard]] static Repeat_optional_t optional() {
53 return Repeat_optional_t{1};
54 }
55
56 /// Return a Repeat object representing between 0 and `min_arg`
57 /// repetitions.
58 [[nodiscard]] static Repeat_optional_t at_most(std::integral auto max_arg) {
59 return Repeat_optional_t{max_arg};
60 }
61
62 /// Return a Repeat_optional object representing any number of repetitions
63 /// from 0 and up (bounded only by std::numeric_limits).
64 [[nodiscard]] static Repeat_optional_t any() {
65 return Repeat_optional_t{std::numeric_limits<std::size_t>::max()};
66 }
67
68 /// Return the maximum number of repetitions, inclusive.
69 [[nodiscard]] std::size_t max() const { return m_max; }
70
71 private:
72 /// The maximum number of repetitions, inclusive.
73 std::size_t m_max;
74};
75} // namespace detail
76
77/// Represents a range of integers for which the lower bound is 0, representing
78/// the number of repetitions of a token when parsing a string.
79///
80/// This class is similar to Repeat, but limited to ranges that begin at zero.
81/// Examples:
82/// - Repeat_optional(3): up to 3 repetitions (0 to 3)
83/// - Repeat_optional::optional(): 0 or 1 repetitions (analogous to the regex
84/// syntax "?")
85/// - Repeat_optional::any(): 0 or more repetitions (analogous to the regex
86/// syntax "*")
87///
88/// The reason we define a distinct class for this case, rather than use
89/// `Repeat` with a specified lower bound of 0, is that some functions taking a
90/// number of repetitions as argument cannot fail when 0 is an allowed number of
91/// repetitions. Such functions can therefore return void and/or omit the
92/// [[nodiscard]] attribute when the argument is of type `Repeat_optional`.
93class Repeat_optional : public detail::Repeat_base<Repeat_optional> {
94 public:
95 /// Default-construct a Repeat_optional object representing a range of 0..1
96 /// repetitions, inclusive.
98
99 /// Construct an object representing a range of 0 up to the given number of
100 /// repetitions.
101 explicit Repeat_optional(std::integral auto max) : Repeat_base(max) {}
102
103 /// Return the mimimum number of repetitions, inclusive; the return value is
104 /// always 0.
105 [[nodiscard]] constexpr std::size_t min() const { return 0; }
106};
107
108/// Represents a range of integers specifying the number of times a token or
109/// pattern should be repeated when parsing a string.
110///
111/// The range is defined by a minimum and maximum value, which can be used to
112/// constrain the number of repetitions.
113///
114/// Examples:
115/// - Repeat::one(): exactly 1 repetition
116/// - Repeat::exact(3): exactly 3 repetitions
117/// - Repeat::range(2, 5): between 2 and 5 repetitions
118/// - Repeat::at_least(1): 1 or more repetitions (analogous to regex syntax "+")
119class Repeat : public detail::Repeat_base<Repeat_optional> {
120 public:
121 /// Default-construct a Repeat object representing exactly 1 repetition.
122 Repeat() : Repeat(1) {}
123
124 /// Construct an object representing exactly the given number of repetitions
125 explicit Repeat(std::integral auto count) : Repeat(count, count) {}
126
127 /// Construct an object representing a range of at least `min_arg` and at most
128 /// `max_arg` repetitions.
129 template <std::integral Int_t>
130 explicit Repeat(Int_t min_arg, Int_t max_arg)
131 : Repeat_base(std::size_t(max_arg)), m_min(std::size_t(min_arg)) {
132 if constexpr (std::signed_integral<Int_t>) {
133 assert(min_arg >= 0);
134 }
135 assert(max_arg >= min_arg);
138 assert(max_arg <= Int_t(std::numeric_limits<std::size_t>::max()));
139 }
140 }
141
142 /// Return a Repeat object representing `min_arg` or more repetitions
143 /// (bounded only by std::numeric_limits)
144 template <std::integral Int_t>
145 [[nodiscard]] static Repeat at_least(Int_t min_arg) {
148 assert(min_arg <= Int_t(std::numeric_limits<std::size_t>::max()));
149 }
150 return Repeat{std::size_t(min_arg),
152 }
153
154 /// Return a Repeat object representing a range of at least `min_arg` and
155 /// at most `max_arg` repetitions.
156 template <std::integral Int_t>
157 [[nodiscard]] static Repeat range(Int_t min_arg, Int_t max_arg) {
158 return Repeat{min_arg, max_arg};
159 }
160
161 /// Return a Repeat object representing exactly the given number of
162 /// repetitions.
163 [[nodiscard]] static Repeat exact(std::integral auto count) {
164 return Repeat{count};
165 }
166
167 /// Return a Repeat object representing exactly one repetition.
168 [[nodiscard]] static Repeat one() { return Repeat{}; }
169
170 /// Return the mimimum number of repetitions, inclusive.
171 [[nodiscard]] std::size_t min() const { return m_min; }
172
173 private:
174 /// The mimimum number of repetitions, inclusive.
175 std::size_t m_min;
176};
177
178/// True if Test is either Repeat or Repeat_optional
179template <class Test>
180concept Is_repeat =
181 std::same_as<Test, Repeat> || std::same_as<Test, Repeat_optional>;
182
183} // namespace mysql::strconv
184
185// addtogroup GroupLibsMysqlStrconv
186/// @}
187
188#endif // ifndef MYSQL_STRCONV_DECODE_REPEAT_H
Represents a range of integers for which the lower bound is 0, representing the number of repetitions...
Definition: repeat.h:93
Repeat_optional(std::integral auto max)
Construct an object representing a range of 0 up to the given number of repetitions.
Definition: repeat.h:101
constexpr std::size_t min() const
Return the mimimum number of repetitions, inclusive; the return value is always 0.
Definition: repeat.h:105
Repeat_optional()
Default-construct a Repeat_optional object representing a range of 0..1 repetitions,...
Definition: repeat.h:97
Represents a range of integers specifying the number of times a token or pattern should be repeated w...
Definition: repeat.h:119
static Repeat range(Int_t min_arg, Int_t max_arg)
Return a Repeat object representing a range of at least min_arg and at most max_arg repetitions.
Definition: repeat.h:157
static Repeat exact(std::integral auto count)
Return a Repeat object representing exactly the given number of repetitions.
Definition: repeat.h:163
std::size_t min() const
Return the mimimum number of repetitions, inclusive.
Definition: repeat.h:171
static Repeat at_least(Int_t min_arg)
Return a Repeat object representing min_arg or more repetitions (bounded only by std::numeric_limits)
Definition: repeat.h:145
Repeat(Int_t min_arg, Int_t max_arg)
Construct an object representing a range of at least min_arg and at most max_arg repetitions.
Definition: repeat.h:130
std::size_t m_min
The mimimum number of repetitions, inclusive.
Definition: repeat.h:175
static Repeat one()
Return a Repeat object representing exactly one repetition.
Definition: repeat.h:168
Repeat()
Default-construct a Repeat object representing exactly 1 repetition.
Definition: repeat.h:122
Repeat(std::integral auto count)
Construct an object representing exactly the given number of repetitions.
Definition: repeat.h:125
Common base class for the Repeat and Repeat_optional classes, providing functionality for representin...
Definition: repeat.h:46
std::size_t m_max
The maximum number of repetitions, inclusive.
Definition: repeat.h:73
Repeat_base(std::integral auto max)
Construct a new object with the given maximum.
Definition: repeat.h:49
static Repeat_optional_t at_most(std::integral auto max_arg)
Return a Repeat object representing between 0 and min_arg repetitions.
Definition: repeat.h:58
std::size_t max() const
Return the maximum number of repetitions, inclusive.
Definition: repeat.h:69
static Repeat_optional_t any()
Return a Repeat_optional object representing any number of repetitions from 0 and up (bounded only by...
Definition: repeat.h:64
static Repeat_optional_t optional()
Return a Repeat_optional object representing zero or one instances.
Definition: repeat.h:52
True if Test is either Repeat or Repeat_optional.
Definition: repeat.h:180
static int count
Definition: myisam_ftdump.cc:45
Definition: fts0fts.cc:236
ValueType max(X &&first)
Definition: gtid.h:103
Definition: gtid_binary_format.h:41
Define std::hash<Gtid>.
Definition: gtid.h:355
static const char digits[]
Definition: stacktrace.cc:644