MySQL 8.0.31
Source Code Documentation
string_utils.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2022, 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_STRING_UTILS_INCLUDED
26#define MYSQL_HARNESS_STRING_UTILS_INCLUDED
27
28#include "harness_export.h"
29
30#include <string>
31#include <string_view>
32#include <vector>
33
34namespace mysql_harness {
35
36/** @brief Splits a string using a delimiter
37 *
38 * Splits a string using the given delimiter. When allow_empty
39 * is true (default), tokens can be empty, and will be included
40 * as empty in the result.
41 *
42 * @param data a string to split
43 * @param delimiter a char used as delimiter
44 * @param allow_empty whether to allow empty tokens or not (default true)
45 * @return std::vector<string> containing tokens
46 */
47HARNESS_EXPORT
48std::vector<std::string> split_string(const std::string_view &data,
49 const char delimiter,
50 bool allow_empty = true);
51
52/**
53 * Removes leading whitespaces from the string
54 *
55 * @param str the string to be trimmed
56 */
57HARNESS_EXPORT
58void left_trim(std::string &str);
59
60/**
61 * Removes trailing whitespaces from the string
62 *
63 * @param str the string to be trimmed
64 */
65HARNESS_EXPORT
66void right_trim(std::string &str);
67
68/**
69 * Removes both leading and trailing whitespaces from the string
70 *
71 * @param str the string to be trimmed
72 */
73HARNESS_EXPORT
74void trim(std::string &str);
75
76/**
77 * Returns the input string with number of lines reduced to selected value.
78 *
79 * @param str input string
80 * @param limit maximum number of lines of the returned string
81 * @param replace_with string that should be used in place of the removed lines
82 *
83 * @returns If the input string contains more than 'limit' number of lines, it
84 * removes the lines from the middle leaving only 'limit' number of lines
85 * (limit/2 of the first lines and limit/2 of the last lines). Otherwise it
86 * returns the whole input string.
87 */
88HARNESS_EXPORT
89std::string limit_lines(const std::string &str, const size_t limit,
90 const std::string &replace_with = "");
91
92} // namespace mysql_harness
93
94#endif /* MYSQL_HARNESS_STRING_UTILS_INCLUDED */
const char * delimiter
Definition: mysqlslap.cc:157
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1063
Definition: common.h:41
HARNESS_EXPORT std::vector< std::string > split_string(const std::string_view &data, const char delimiter, bool allow_empty=true)
Splits a string using a delimiter.
Definition: string_utils.cc:35
HARNESS_EXPORT void left_trim(std::string &str)
Removes leading whitespaces from the string.
Definition: string_utils.cc:60
HARNESS_EXPORT void trim(std::string &str)
Removes both leading and trailing whitespaces from the string.
Definition: string_utils.cc:69
HARNESS_EXPORT std::string limit_lines(const std::string &str, const size_t limit, const std::string &replace_with="")
Returns the input string with number of lines reduced to selected value.
Definition: string_utils.cc:99
HARNESS_EXPORT void right_trim(std::string &str)
Removes trailing whitespaces from the string.
Definition: string_utils.cc:64