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