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