MySQL 8.4.0
Source Code Documentation
string.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 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 HARNESS_STRING_INCLUDED
27#define HARNESS_STRING_INCLUDED
28
29#include "harness_export.h"
30
31#include <numeric> // accumulate
32#include <string>
33#include <vector>
34
35#include "my_compiler.h" // MY_ATTRIBUTE
36
37namespace mysql_harness {
38namespace utility {
39std::vector<std::string> HARNESS_EXPORT wrap_string(const std::string &to_wrap,
40 std::size_t width,
41 std::size_t indent_size);
42
43/** @brief Checks whether string ends with the specified suffix
44 *
45 * Returns true if the string ends with the given suffix.
46 *
47 * @return bool
48 */
49bool HARNESS_EXPORT ends_with(const std::string &str,
50 const std::string &suffix);
51
52/** @brief Checks whether string starts with the specified prefix
53 *
54 * Returns true if the string begins with the given prefix.
55 *
56 * @return bool
57 */
58bool HARNESS_EXPORT starts_with(const std::string &str,
59 const std::string &prefix);
60
61HARNESS_EXPORT
62MY_ATTRIBUTE((format(printf, 1, 2)))
63std::string string_format(const char *format, ...);
64
65} // namespace utility
66
67namespace detail {
68template <class Container, class T>
69struct Join {
70 static std::string impl(Container, const std::string &);
71};
72
73template <class Container>
74struct Join<Container, std::string> {
75 static std::string impl(Container cont, const std::string &delim) {
76 if (cont.begin() == cont.end()) return {};
77
78 std::string o(*(cont.begin()));
79
80 // if T::value_type has a .size() method reallocs can be avoided
81 // when joining the strings by calculating the size upfront
82 {
83 const size_t delim_size = delim.size();
84 size_t space =
85 std::accumulate(std::next(cont.begin()), cont.end(), o.size(),
86 [delim_size](size_t sum, const std::string &b) {
87 return sum + delim_size + b.size();
88 });
89 o.reserve(space);
90 }
91
92#if 0
93 // once benchmarked that this is equivalent of the hand-rolled version
94 // (number of allocs, ...) this implementation could be used.
95 return std::accumulate(std::next(cont.begin()), cont.end(), o,
96 [&delim](std::string a, const std::string &b) {
97 return a.append(delim).append(b);
98 });
99#else
100 // add the first element directly
101 auto it = std::next(cont.begin());
102 const auto last = cont.end();
103
104 // all other elements with delim
105 for (; it != last; ++it) {
106 o += delim;
107
108 o += *it;
109 }
110
111 return o;
112#endif
113 }
114};
115
116template <class Container>
117struct Join<Container, const char *> {
118 static std::string impl(Container cont, const std::string &delim) {
119 if (cont.begin() == cont.end()) return {};
120
121 return std::accumulate(std::next(cont.begin()), cont.end(),
122 std::string(*(cont.begin())),
123 [&delim](const std::string &a, const char *b) {
124 return a + delim + b;
125 });
126 }
127};
128
129} // namespace detail
130
131/**
132 * join elements of an container into a string separated by a delimiter.
133 *
134 * Container MUST:
135 *
136 * - have .begin() and end()
137 * - ::iterator must be ForwardIterator + InputIterator
138 * - ::value_type must be appendable to std::string
139 *
140 * should work with:
141 *
142 * - std::vector<const char *|std::string>
143 * - std::array<const char *|std::string, N>
144 * - std::list<const char *|std::string>
145 *
146 * @param cont a container
147 * @param delim delimiter
148 * @returns string of elements of container separated by delim
149 */
150template <class Container>
151std::string join(Container cont, const std::string &delim) {
153 delim);
154}
155/* Checks that given string belongs to the collection of strings */
156template <class T>
157constexpr bool str_in_collection(const T &t, const std::string_view &k) {
158 for (auto v : t) {
159 if (v == k) return true;
160 }
161 return false;
162}
163
164} // namespace mysql_harness
165
166#endif
Header for compiler-dependent features.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1073
Definition: ut0tuple.h:57
HARNESS_EXPORT std::string string_format(const char *format,...)
Definition: utilities.cc:64
std::vector< std::string > HARNESS_EXPORT wrap_string(const std::string &to_wrap, std::size_t width, std::size_t indent_size)
bool HARNESS_EXPORT ends_with(const std::string &str, const std::string &suffix)
Checks whether string ends with the specified suffix.
Definition: utilities.cc:122
bool HARNESS_EXPORT starts_with(const std::string &str, const std::string &prefix)
Checks whether string starts with the specified prefix.
Definition: utilities.cc:129
Definition: common.h:42
constexpr bool str_in_collection(const T &t, const std::string_view &k)
Definition: string.h:157
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
Definition: gcs_xcom_synode.h:64
static std::string impl(Container cont, const std::string &delim)
Definition: string.h:118
static std::string impl(Container cont, const std::string &delim)
Definition: string.h:75
Definition: string.h:69
static std::string impl(Container, const std::string &)