MySQL 9.2.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 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, as
10 * 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 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License, version 2.0, for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#ifndef ROUTER_SRC_ROUTING_GUIDELINES_SRC_UTILS_H_
25#define ROUTER_SRC_ROUTING_GUIDELINES_SRC_UTILS_H_
26
27#include <algorithm>
28#include <cstring>
29#include <functional>
30#include <ranges>
31#include <string>
32#include <vector>
33
34namespace rapidjson {
35struct ParseResult;
36}
37
38namespace routing_guidelines {
39
40std::string str_strip(const std::string &s,
41 const std::string &chars = " \r\n\t");
42
43inline bool str_eq(std::string_view a, std::string_view b) {
44 return std::ranges::equal(a, b);
45}
46
47inline bool str_caseeq(const char *a, const char *b) {
48#ifdef _WIN32
49 return ::_stricmp(a, b) == 0;
50#else
51 return ::strcasecmp(a, b) == 0;
52#endif
53}
54
55inline bool str_caseeq(const char *a, const char *b, size_t n) {
56#ifdef _WIN32
57 return ::_strnicmp(a, b, n) == 0;
58#else
59 return ::strncasecmp(a, b, n) == 0;
60#endif
61}
62
63inline bool str_caseeq(std::string_view lhs, std::string_view rhs) {
64 return std::ranges::equal(lhs, rhs, [](const auto a, const auto b) {
65 return std::tolower(static_cast<unsigned char>(a)) ==
66 std::tolower(static_cast<unsigned char>(b));
67 });
68}
69
70/** Compares 2 strings case insensitive (for ascii) */
71inline int str_casecmp(const char *a, const char *b) {
72#ifdef _WIN32
73 return ::_stricmp(a, b);
74#else
75 return ::strcasecmp(a, b);
76#endif
77}
78
79inline int str_casecmp(const std::string &a, const std::string &b) {
80 return str_casecmp(a.c_str(), b.c_str());
81}
82
83inline bool str_ibeginswith(std::string_view str, std::string_view prefix) {
84 if (prefix.size() > str.size()) return false;
85
86 for (size_t i = 0; i < prefix.size(); ++i) {
87 if (std::tolower(static_cast<unsigned char>(str[i])) !=
88 std::tolower(static_cast<unsigned char>(prefix[i]))) {
89 return false;
90 }
91 }
92
93 return true;
94}
95
96inline bool str_beginswith(std::string_view str, std::string_view prefix) {
97 return str.starts_with(prefix);
98}
99
100/** Convert a copy of an ASCII string to uppercase and return */
101inline std::string str_upper(std::string_view s) {
102 std::string r(s);
103 std::transform(r.begin(), r.end(), r.begin(), ::toupper);
104 return r;
105}
106
107/** Convert a copy of an ASCII string to lowercase and return */
108inline std::string str_lower(std::string_view s) {
109 std::string r(s);
110 std::transform(r.begin(), r.end(), r.begin(), ::tolower);
111 return r;
112}
113
114/// process escapes in a string , keep synchronous with sql_load unescape
115/// ("ntrb0ZN")
116std::string mysql_unescape_string(std::string_view s);
117
118std::string like_to_regexp(std::string_view pattern);
119
120/// Check if container contains identincal string independent of case
121template <typename Container>
122bool case_contains(const Container &container, std::string_view str) {
123 for (const auto &el : container)
124 if (str_caseeq(str, el)) return true;
125 return false;
126}
127
128bool is_ipv4(const std::string &address);
129bool is_ipv6(const std::string &address);
130
131/// Compute network part of an IPv4 address
132std::string network(const std::string &address, unsigned int bitlen);
133
134/** Format JSON parse error adding responsible JSON part
135 *
136 * @param s parsed JSON
137 * @param ok parse result
138 * @param chars how big surrounding of erroneous part to attach
139 */
140std::string format_json_error(const std::string &s,
141 const rapidjson::ParseResult &ok, size_t chars);
142
143/// Is JSON document held by the string complete or not
144bool json_document_complete(const std::string &s);
145
146} // namespace routing_guidelines
147
148#endif // ROUTER_SRC_ROUTING_GUIDELINES_SRC_UTILS_H_
static bool equal(const Item *i1, const Item *i2, const Field *f2)
Definition: sql_select.cc:3910
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1117
Definition: atomics_array.h:39
bool transform(const dd::Spatial_reference_system *source_srs, const Geometry &in, const dd::Spatial_reference_system *target_srs, const char *func_name, std::unique_ptr< Geometry > *out) noexcept
Transforms a geometry from one SRS to another.
Definition: transform.cc:216
char tolower(const char &ch)
Definition: parsing_helpers.h:41
Definition: my_rapidjson_size_t.h:38
Definition: routing_guidelines_datatypes.h:30
bool str_ibeginswith(std::string_view str, std::string_view prefix)
Definition: utils.h:83
bool json_document_complete(const std::string &s)
Is JSON document held by the string complete or not.
Definition: utils.cc:230
bool is_ipv6(const std::string &host)
Definition: utils.cc:192
std::string str_upper(std::string_view s)
Convert a copy of an ASCII string to uppercase and return.
Definition: utils.h:101
std::string like_to_regexp(std::string_view pattern)
Definition: utils.cc:115
bool str_caseeq(const char *a, const char *b)
Definition: utils.h:47
std::string str_strip(const std::string &s, const std::string &chars)
Definition: utils.cc:63
std::string mysql_unescape_string(std::string_view s)
process escapes in a string , keep synchronous with sql_load unescape ("ntrb0ZN")
Definition: utils.cc:82
int str_casecmp(const char *a, const char *b)
Compares 2 strings case insensitive (for ascii)
Definition: utils.h:71
bool case_contains(const Container &container, std::string_view str)
Check if container contains identincal string independent of case.
Definition: utils.h:122
bool is_ipv4(const std::string &address)
Definition: utils.cc:188
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:70
std::string str_lower(std::string_view s)
Convert a copy of an ASCII string to lowercase and return.
Definition: utils.h:108
bool str_beginswith(std::string_view str, std::string_view prefix)
Definition: utils.h:96
std::string network(const std::string &address, unsigned int bitlen)
Compute network part of an IPv4 address.
Definition: utils.cc:208
bool str_eq(std::string_view a, std::string_view b)
Definition: utils.h:43
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
int n
Definition: xcom_base.cc:509