MySQL 8.0.29
Source Code Documentation
common.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2021, 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_COMMON_INCLUDED
26#define MYSQL_HARNESS_COMMON_INCLUDED
27
28#include <cstdlib>
29#include <functional>
30#include <map>
31#include <sstream>
32#include <string>
33#include "harness_export.h"
34
35/**
36 * @defgroup Various operations
37 *
38 * This module contain various utility operations.
39 */
40
41namespace mysql_harness {
42
43/** @brief Return a truncated version of input string (fast version)
44 *
45 * WARNING!
46 * This function is optimised for speed, but see note below for use
47 * restrictions. If these are a problem, use truncate_string_r() instead.
48 *
49 * This function returns a refernce to the input string if input.size() <=
50 * max_len, otherwise it returns a reference to a truncated copy of input
51 * string.
52 *
53 * @param input input text
54 * @param max_len maximum length after truncation
55 * @return const reference to truncated string
56 *
57 * @note This function may return a reference to a string allocated on
58 * thread-local storage. Therefore, the resulting string reference is only valid
59 * until another call to this function is made from caller's thread (other
60 * threads calling this function have no impact), and by the same token,
61 * dereferencing it outside of the caller's thread may lead to a race. If your
62 * use case violates these limitations, you should use truncate_string_r()
63 * instead to ensure safety.
64 */
65HARNESS_EXPORT
66const std::string &truncate_string(const std::string &input,
67 size_t max_len = 80);
68
69/** @brief Return a truncated version of input string (reentrant version)
70 *
71 * This is a safe version of truncate_string(), which lifts its use restrictions
72 * by always returning a copy of result string. Please see documentation of
73 * truncate_string() for more information.
74 */
75HARNESS_EXPORT
76std::string truncate_string_r(const std::string &input, size_t max_len = 80);
77
78/**
79 * Emit a range of elements using the serial comma.
80 *
81 * This function can be used to output a range of elements using a
82 * serial comma (also known as the Oxford comma). To emit a list of
83 * the first five prime numbers as "The first five prime numbers are
84 * 2, 3, 5, 7, and 11":
85 *
86 * @code
87 * std::vector<int> primes{2, 3, 5, 7, 11};
88 * std::cout << "The first five prime numbers are ";
89 * serial_comma(std::cout, primes.begin(), primes.end());
90 * std::cout << std::endl;
91 * @endcode
92 *
93 * @param out Output stream
94 * @param start Input iterator to start of range.
95 * @param finish Input iterator to one-after-end of range.
96 * @param delim Delimiter to use. Defaults to "and".
97 */
98template <class InputIt>
99void serial_comma(std::ostream &out, InputIt start, InputIt finish,
100 const std::string &delim = "and") {
101 auto elements = std::distance(start, finish);
102 if (elements == 1) {
103 out << *start;
104 } else if (elements == 2) {
105 out << *start++;
106 out << " " << delim << " " << *start;
107 } else {
108 while (elements-- > 0) {
109 out << *start++;
110 if (elements > 0) out << ", ";
111 if (elements == 1) out << delim << " ";
112 }
113 }
114}
115
116/**
117 * Returns string containing list of the elements using the serial comma.
118 *
119 * This function can be used to output a range of elements using a
120 * serial comma (also known as the Oxford comma). To return a list of
121 * the first five prime numbers as "The first five prime numbers are
122 * 2, 3, 5, 7, and 11":
123 *
124 * @code
125 * std::vector<int> primes{2, 3, 5, 7, 11};
126 * std::cout << "The first five prime numbers are "
127 * << serial_comma(primes.begin(), primes.end()) << std::endl;
128 * @endcode
129 *
130 * @param start Input iterator to start of range.
131 * @param finish Input iterator to one-after-end of range.
132 * @param delim Delimiter to use. Defaults to "and".
133 *
134 * @return string containing list of the elements
135 */
136template <class InputIt>
137std::string serial_comma(InputIt start, InputIt finish,
138 const std::string &delim = "and") {
139 std::stringstream out;
140 serial_comma(out, start, finish, delim);
141
142 return out.str();
143}
144
145/**
146 * Returns string containing list of the elements separated by selected
147 * delimiter.
148 *
149 * To return a list of the first five prime numbers as "The first five prime
150 * numbers are 2, 3, 5, 7, 11":
151 *
152 * @code
153 * std::vector<int> primes{2, 3, 5, 7, 11};
154 * std::cout << "The first five prime numbers are "
155 * << list_elements(primes.begin(), primes.end()) << std::endl;
156 * @endcode
157 *
158 * @param start Input iterator to start of range.
159 * @param finish Input iterator to one-after-end of range.
160 * @param delim Delimiter to use. Defaults to ",".
161 *
162 * @return string containing list of the elements
163 */
164template <class InputIt>
165std::string list_elements(InputIt start, InputIt finish,
166 const std::string &delim = ",") {
167 std::string result;
168 for (auto cur = start; cur != finish; ++cur) {
169 if (cur != start) result += delim;
170 result += *cur;
171 }
172
173 return result;
174}
175
176/**
177 * Returns string containing list of the elements separated by selected
178 * delimiter.
179 *
180 * To return a list of the first five prime numbers as "The first five prime
181 * numbers are 2, 3, 5, 7, 11":
182 *
183 * @code
184 * std::vector<int> primes{2, 3, 5, 7, 11};
185 * std::cout << "The first five prime numbers are "
186 * << list_elements(primes) << std::endl;
187 * @endcode
188 *
189 * @param collection Collection of the elements to output.
190 * @param delim Delimiter to use. Defaults to ",".
191 *
192 * @return string containing list of the elements
193 */
194template <class Collection>
195std::string list_elements(Collection collection,
196 const std::string &delim = ",") {
197 return list_elements(collection.begin(), collection.end(), delim);
198}
199
200/**
201 * Gets a Value from std::map for given Key. Returns provided default if the Key
202 * is not in the map.
203 */
204template <class Key, class Value>
205Value get_from_map(const std::map<Key, Value> &map, const Key &key,
206 const Value &default_value) {
207 auto iter = map.find(key);
208 if (iter == map.end()) return default_value;
209 return iter->second;
210}
211
212} // namespace mysql_harness
213
214#endif /* MYSQL_HARNESS_COMMON_INCLUDED */
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:168
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:39
std::string_view Key
The key type for the hash structure in HashJoinRowBuffer.
Definition: hash_join_buffer.h:107
Definition: common.h:41
Value get_from_map(const std::map< Key, Value > &map, const Key &key, const Value &default_value)
Gets a Value from std::map for given Key.
Definition: common.h:205
void serial_comma(std::ostream &out, InputIt start, InputIt finish, const std::string &delim="and")
Emit a range of elements using the serial comma.
Definition: common.h:99
HARNESS_EXPORT const std::string & truncate_string(const std::string &input, size_t max_len=80)
Return a truncated version of input string (fast version)
Definition: common.cc:66
std::string list_elements(InputIt start, InputIt finish, const std::string &delim=",")
Returns string containing list of the elements separated by selected delimiter.
Definition: common.h:165
HARNESS_EXPORT std::string truncate_string_r(const std::string &input, size_t max_len=80)
Return a truncated version of input string (reentrant version)
Definition: common.cc:72
struct result result
Definition: result.h:33
required string key
Definition: replication_asynchronous_connection_failover.proto:59
Definition: result.h:29