MySQL 9.1.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 <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
42HARNESS_EXPORT
43MY_ATTRIBUTE((format(printf, 1, 2)))
44std::string string_format(const char *format, ...);
45
46} // namespace utility
47
48namespace detail {
49
50// a simplified variant of the std::ranges::range concept
51//
52// C++20 ranges lib isn't fully available yet.
53
54template <class R>
55concept range = requires(const R &rng) {
56 std::begin(rng);
57 std::end(rng);
58 };
59
60} // namespace detail
61
62/**
63 * join elements of a range into a string separated by a delimiter.
64 *
65 * works with:
66 *
67 * - std::vector, std::array, c-array, list, forward_list, deque
68 * - and std::string, c-string, std::string_view
69 *
70 * @param rng a range of strings
71 * @param delim delimiter
72 * @returns string of elements of the range separated by delim
73 */
74std::string join(const detail::range auto &rng, std::string_view delim)
75 requires(std::constructible_from<std::string, decltype(*std::begin(rng))>)
76{
77 auto cur = std::begin(rng);
78 const auto end = std::end(rng);
79
80 if (cur == end) return {}; // empty
81
82 std::string joined(*cur); // first element.
83
84 // append delim + element
85 for (cur = std::next(cur); cur != end; cur = std::next(cur)) {
86 joined.append(delim).append(*cur);
87 }
88
89 return joined;
90}
91
92/* Checks that given string belongs to the collection of strings */
93template <class T>
94constexpr bool str_in_collection(const T &t, const std::string_view &k) {
95 for (auto v : t) {
96 if (v == k) return true;
97 }
98 return false;
99}
100
101} // namespace mysql_harness
102
103#endif
Definition: string.h:55
Header for compiler-dependent features.
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)
Definition: common.h:42
constexpr bool str_in_collection(const T &t, const std::string_view &k)
Definition: string.h:94
std::string join(const detail::range auto &rng, std::string_view delim)
join elements of a range into a string separated by a delimiter.
Definition: string.h:74
const char * begin(const char *const c)
Definition: base64.h:44
Definition: gcs_xcom_synode.h:64