MySQL 9.1.0
Source Code Documentation
utilities.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 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 MYSQL_HARNESS_UTILITIES_INCLUDED
27#define MYSQL_HARNESS_UTILITIES_INCLUDED
28
29#include <memory>
30#include <ostream>
31#include <string>
32#include <vector>
33
34#include "harness_export.h"
35
36namespace mysql_harness {
37
38namespace utility {
39
40/**
41 * Class to turn C array into range.
42 *
43 * @see make_range
44 */
45
46template <class Type>
47class Range {
48 public:
49 class iterator {
50 public:
51 explicit iterator(Type *ptr) : ptr_(ptr) {}
52
54 ++ptr_;
55 return *this;
56 }
57
58 bool operator==(const iterator &rhs) const { return ptr_ == rhs.ptr_; }
59
60 bool operator!=(const iterator &rhs) const { return ptr_ != rhs.ptr_; }
61
62 Type &operator*() { return *ptr_; }
63
64 const Type &operator*() const { return *ptr_; }
65
66 private:
68 };
69
70 Range(Type *ptr, size_t length) : start_(ptr), finish_(ptr + length) {}
71
73
75
76 private:
79};
80
81/**
82 * Create a range from a plain C array.
83 *
84 * This function create a range from a plain array so that arrays can
85 * be used in range-based loops.
86 *
87 * @see Range
88 */
89
90template <class Type>
92 return Range<Type>(ptr, length);
93}
94
95/**
96 * Class for creating a reverse range from another range.
97 */
98
99template <typename Range>
101 public:
103
104 typename Range::reverse_iterator begin() { return range_.rbegin(); }
105
106 typename Range::const_reverse_iterator begin() const {
107 return range_.rbegin();
108 }
109
110 typename Range::reverse_iterator end() { return range_.rend(); }
111
112 typename Range::const_reverse_iterator end() const { return range_.rend(); }
113
114 private:
116};
117
118/**
119 * Iterate over a range in reverse.
120 *
121 * Function take a range, which can be any sequence container, and
122 * return a reverse range that iterate the sequence in reverse.
123 *
124 * Typical use-case is:
125 * @code
126 * for (auto item : reverse_iterate(my_list)) {
127 * ...
128 * }
129 * @endcode
130 */
131template <typename Range>
132RangeReverse<Range> reverse(Range &x) { // NOLINT(runtime/references)
133 return RangeReverse<Range>(x);
134}
135
136template <class Map>
137std::pair<typename Map::iterator, typename Map::iterator> find_range_first(
138 Map &assoc, // NOLINT(runtime/references)
139 const typename Map::key_type::first_type &first,
140 typename Map::iterator start) {
141 typename Map::iterator finish = start;
142 while (finish != assoc.end() && finish->first.first == first) ++finish;
143 return make_pair(start, finish);
144}
145
146template <class Map>
147std::pair<typename Map::iterator, typename Map::iterator> find_range_first(
148 Map &assoc, // NOLINT(runtime/references)
149 const typename Map::key_type::first_type &first) {
150 typedef typename Map::key_type::second_type SType;
151 return find_range_first(assoc, first,
152 assoc.lower_bound(make_pair(first, SType())));
153}
154
155template <class Map>
156std::pair<typename Map::const_iterator, typename Map::const_iterator>
157find_range_first(const Map &assoc,
158 const typename Map::key_type::first_type &first,
159 typename Map::const_iterator start) {
160 typename Map::const_iterator finish = start;
161 while (finish != assoc.end() && finish->first.first == first) ++finish;
162 return make_pair(start, finish);
163}
164
165template <class Map>
166std::pair<typename Map::const_iterator, typename Map::const_iterator>
167find_range_first(const Map &assoc,
168 const typename Map::key_type::first_type &first) {
169 typedef typename Map::key_type::second_type SType;
170 return find_range_first(assoc, first,
171 assoc.lower_bound(make_pair(first, SType())));
172}
173
174std::string dirname(const std::string &path);
175std::string basename(const std::string &path);
176
177/**
178 * Remove starting and trailing delimiters from string.
179 */
180void strip(std::string *str, const char *chars = " \t\n\r\f\v");
181HARNESS_EXPORT
182std::string strip_copy(std::string str, const char *chars = " \t\n\r\f\v");
183
184bool matches_glob(const std::string &word, const std::string &pattern);
185
186/*
187 * Checks whether given string matches the pattern using extended posix regex.
188 */
189bool regex_pattern_matches(const std::string &s, const std::string &pattern);
190
191} // namespace utility
192
193} // namespace mysql_harness
194#endif /* MYSQL_HARNESS_UTILITIES_INCLUDED */
Class for creating a reverse range from another range.
Definition: utilities.h:100
Range & range_
Definition: utilities.h:115
Range::const_reverse_iterator end() const
Definition: utilities.h:112
Range::const_reverse_iterator begin() const
Definition: utilities.h:106
Range::reverse_iterator end()
Definition: utilities.h:110
Range::reverse_iterator begin()
Definition: utilities.h:104
RangeReverse(Range &range)
Definition: utilities.h:102
Definition: utilities.h:49
iterator(Type *ptr)
Definition: utilities.h:51
bool operator==(const iterator &rhs) const
Definition: utilities.h:58
Type & operator*()
Definition: utilities.h:62
Type * ptr_
Definition: utilities.h:67
const Type & operator*() const
Definition: utilities.h:64
bool operator!=(const iterator &rhs) const
Definition: utilities.h:60
iterator & operator++()
Definition: utilities.h:53
Class to turn C array into range.
Definition: utilities.h:47
iterator begin()
Definition: utilities.h:72
iterator end()
Definition: utilities.h:74
Range(Type *ptr, size_t length)
Definition: utilities.h:70
Type * start_
Definition: utilities.h:77
Type * finish_
Definition: utilities.h:78
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
static char * path
Definition: mysqldump.cc:149
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1105
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
bool regex_pattern_matches(const std::string &s, const std::string &pattern)
Definition: utilities-posix.cc:44
std::string dirname(const std::string &path)
Definition: utilities.cc:38
void strip(std::string *str, const char *chars)
Remove starting and trailing delimiters from string.
Definition: utilities.cc:54
Range< Type > make_range(Type *ptr, size_t length)
Create a range from a plain C array.
Definition: utilities.h:91
std::string strip_copy(std::string str, const char *chars)
Definition: utilities.cc:59
bool matches_glob(const std::string &word, const std::string &pattern)
Definition: utilities-posix.cc:38
std::pair< typename Map::iterator, typename Map::iterator > find_range_first(Map &assoc, const typename Map::key_type::first_type &first, typename Map::iterator start)
Definition: utilities.h:137
RangeReverse< Range > reverse(Range &x)
Iterate over a range in reverse.
Definition: utilities.h:132
std::string basename(const std::string &path)
Definition: utilities.cc:46
Definition: common.h:42
Type
Definition: resource_group_basic_types.h:33
Definition: gen_lex_token.cc:149