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