MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, 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, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, 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 Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef ROUTER_SRC_ROUTING_GUIDELINES_SRC_UTILS_H_
27#define ROUTER_SRC_ROUTING_GUIDELINES_SRC_UTILS_H_
28
29#include <algorithm>
30#include <cstring>
31#include <functional>
32#include <ranges>
33#include <string>
34#include <vector>
35
36namespace rapidjson {
37struct ParseResult;
38}
39
40namespace routing_guidelines {
41
42std::string str_strip(const std::string &s,
43 const std::string &chars = " \r\n\t");
44
45inline bool str_eq(std::string_view a, std::string_view b) {
46 return std::ranges::equal(a, b);
47}
48
49inline bool str_caseeq(const char *a, const char *b) {
50#ifdef _WIN32
51 return ::_stricmp(a, b) == 0;
52#else
53 return ::strcasecmp(a, b) == 0;
54#endif
55}
56
57inline bool str_caseeq(const char *a, const char *b, size_t n) {
58#ifdef _WIN32
59 return ::_strnicmp(a, b, n) == 0;
60#else
61 return ::strncasecmp(a, b, n) == 0;
62#endif
63}
64
65inline bool str_caseeq(std::string_view lhs, std::string_view rhs) {
66 return std::ranges::equal(lhs, rhs, [](const auto a, const auto b) {
67 return std::tolower(static_cast<unsigned char>(a)) ==
68 std::tolower(static_cast<unsigned char>(b));
69 });
70}
71
72/** Compares 2 strings case insensitive (for ascii) */
73inline int str_casecmp(const char *a, const char *b) {
74#ifdef _WIN32
75 return ::_stricmp(a, b);
76#else
77 return ::strcasecmp(a, b);
78#endif
79}
80
81inline int str_casecmp(const std::string &a, const std::string &b) {
82 return str_casecmp(a.c_str(), b.c_str());
83}
84
85inline bool str_ibeginswith(std::string_view str, std::string_view prefix) {
86 if (prefix.size() > str.size()) return false;
87
88 for (size_t i = 0; i < prefix.size(); ++i) {
89 if (std::tolower(static_cast<unsigned char>(str[i])) !=
90 std::tolower(static_cast<unsigned char>(prefix[i]))) {
91 return false;
92 }
93 }
94
95 return true;
96}
97
98inline bool str_beginswith(std::string_view str, std::string_view prefix) {
99 return str.starts_with(prefix);
100}
101
102/** Convert a copy of an ASCII string to uppercase and return */
103inline std::string str_upper(std::string_view s) {
104 std::string r(s);
105 std::transform(r.begin(), r.end(), r.begin(), ::toupper);
106 return r;
107}
108
109/** Convert a copy of an ASCII string to lowercase and return */
110inline std::string str_lower(std::string_view s) {
111 std::string r(s);
112 std::transform(r.begin(), r.end(), r.begin(), ::tolower);
113 return r;
114}
115
116/// process escapes in a string , keep synchronous with sql_load unescape
117/// ("ntrb0ZN")
118std::string mysql_unescape_string(std::string_view s);
119
120std::string like_to_regexp(std::string_view pattern);
121
122/// Check if container contains identincal string independent of case
123template <typename Container>
124bool case_contains(const Container &container, std::string_view str) {
125 for (const auto &el : container)
126 if (str_caseeq(str, el)) return true;
127 return false;
128}
129
130bool is_ipv4(const std::string &address);
131bool is_ipv6(const std::string &address);
132
133/// Compute network part of an IPv4 address
134std::string network(const std::string &address, unsigned int bitlen);
135
136/** Format JSON parse error adding responsible JSON part
137 *
138 * @param s parsed JSON
139 * @param ok parse result
140 * @param chars how big surrounding of erroneous part to attach
141 */
142std::string format_json_error(const std::string &s,
143 const rapidjson::ParseResult &ok, size_t chars);
144
145/// Is JSON document held by the string complete or not
146bool json_document_complete(const std::string &s);
147
148} // namespace routing_guidelines
149
150#endif // ROUTER_SRC_ROUTING_GUIDELINES_SRC_UTILS_H_
static bool equal(const Item *i1, const Item *i2, const Field *f2)
Definition: sql_select.cc:3937
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
Definition: atomics_array.h:39
char tolower(const char &ch)
Definition: parsing_helpers.h:41
Definition: my_rapidjson_size_t.h:38
Definition: routing_guidelines_datatypes.h:31
bool str_ibeginswith(std::string_view str, std::string_view prefix)
Definition: utils.h:85
bool json_document_complete(const std::string &s)
Is JSON document held by the string complete or not.
Definition: utils.cc:232
bool is_ipv6(const std::string &host)
Definition: utils.cc:194
std::string str_upper(std::string_view s)
Convert a copy of an ASCII string to uppercase and return.
Definition: utils.h:103
std::string like_to_regexp(std::string_view pattern)
Definition: utils.cc:117
bool str_caseeq(const char *a, const char *b)
Definition: utils.h:49
std::string str_strip(const std::string &s, const std::string &chars)
Definition: utils.cc:65
std::string mysql_unescape_string(std::string_view s)
process escapes in a string , keep synchronous with sql_load unescape ("ntrb0ZN")
Definition: utils.cc:84
int str_casecmp(const char *a, const char *b)
Compares 2 strings case insensitive (for ascii)
Definition: utils.h:73
bool case_contains(const Container &container, std::string_view str)
Check if container contains identincal string independent of case.
Definition: utils.h:124
bool is_ipv4(const std::string &address)
Definition: utils.cc:190
std::string format_json_error(const std::string &s, const rapidjson::ParseResult &ok, size_t chars)
Format JSON parse error adding responsible JSON part.
Definition: utils.cc:72
std::string str_lower(std::string_view s)
Convert a copy of an ASCII string to lowercase and return.
Definition: utils.h:110
bool str_beginswith(std::string_view str, std::string_view prefix)
Definition: utils.h:98
std::string network(const std::string &address, unsigned int bitlen)
Compute network part of an IPv4 address.
Definition: utils.cc:210
bool str_eq(std::string_view a, std::string_view b)
Definition: utils.h:45
std::basic_string< Char > transform(std::basic_string_view< Char > s, F fun)
Definition: utils_string.h:53
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
int n
Definition: xcom_base.cc:509