MySQL 9.2.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 <system_error>
34#include <vector>
35
37
38namespace mysql_harness {
39
40/** @brief Splits a string using a delimiter
41 *
42 * Splits a string using the given delimiter. When allow_empty
43 * is true (default), tokens can be empty, and will be included
44 * as empty in the result.
45 *
46 * @param data a string to split
47 * @param delimiter a char used as delimiter
48 * @param allow_empty whether to allow empty tokens or not (default true)
49 * @return std::vector<string> containing tokens
50 */
51HARNESS_EXPORT
52std::vector<std::string> split_string(const std::string_view &data,
53 const char delimiter,
54 bool allow_empty = true);
55
56/**
57 * Removes leading whitespaces from the string
58 *
59 * @param str the string to be trimmed
60 */
61HARNESS_EXPORT
62void left_trim(std::string &str);
63
64/**
65 * Removes trailing whitespaces from the string
66 *
67 * @param str the string to be trimmed
68 */
69HARNESS_EXPORT
70void right_trim(std::string &str);
71
72/**
73 * Removes both leading and trailing whitespaces from the string
74 *
75 * @param str the string to be trimmed
76 */
77HARNESS_EXPORT
78void trim(std::string &str);
79
80/**
81 * Compares two string values for equality (case insensitive).
82 *
83 * @param a string value to be compared
84 * @param b string value to be compared
85 */
86HARNESS_EXPORT bool ieq(const std::string_view &a, const std::string_view &b);
87
88/**
89 * Returns the input string with number of lines reduced to selected value.
90 *
91 * @param str input string
92 * @param limit maximum number of lines of the returned string
93 * @param replace_with string that should be used in place of the removed lines
94 *
95 * @returns If the input string contains more than 'limit' number of lines, it
96 * removes the lines from the middle leaving only 'limit' number of lines
97 * (limit/2 of the first lines and limit/2 of the last lines). Otherwise it
98 * returns the whole input string.
99 */
100HARNESS_EXPORT
101std::string limit_lines(const std::string &str, const size_t limit,
102 const std::string &replace_with = "");
103
104/** Returns a boolean value parsed from a string.
105 *
106 * @param str input string
107 *
108 * @return true if input string contains 'true' and false for 'false' (case
109 * insensitive). Returns error code otherwise.
110 */
111HARNESS_EXPORT
113
114} // namespace mysql_harness
115
116#endif /* MYSQL_HARNESS_STRING_UTILS_INCLUDED */
Definition: expected.h:286
const char * delimiter
Definition: mysqlslap.cc:160
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1117
Definition: common.h:44
HARNESS_EXPORT stdx::expected< bool, std::error_code > bool_from_string(std::string str)
Returns a boolean value parsed from a string.
Definition: string_utils.cc:142
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 bool ieq(const std::string_view &a, const std::string_view &b)
Compares two string values for equality (case insensitive).
Definition: string_utils.cc:75
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:109
HARNESS_EXPORT void right_trim(std::string &str)
Removes trailing whitespaces from the string.
Definition: string_utils.cc:65