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