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