MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
contains.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 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,
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 ROUTER_SRC_REST_MRS_SRC_HELPER_STRING_CONTAINS_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_STRING_CONTAINS_H_
28
29#include <cstdint>
30#include <string>
31
33
34namespace helper {
35
36template <typename String1, typename String2>
37bool contains(const String1 &value, const String2 &sst) {
38 using namespace helper::string;
39 return nullptr != strstr(cstr(value), cstr(sst));
40}
41
42template <typename String1, typename String2>
43bool icontains(const String1 &value, const String2 &sst) {
44 auto sv = helper::string::size(value);
45 auto ss = helper::string::size(sst);
46
47 if (ss > sv) return false;
48
49 auto serach_break_at = sv - ss;
50
51 size_t found_chars = 0;
52 for (size_t i = 0; i < sv && found_chars != ss; ++i) {
53 if (!found_chars && (i > serach_break_at)) break;
54
55 if (tolower(value[i]) == tolower(sst[found_chars])) {
56 ++found_chars;
57 continue;
58 }
59
60 found_chars = 0;
61 }
62
63 return found_chars == ss;
64}
65
66inline bool ends_with(const std::string &value, const std::string &sst) {
67 if (sst.empty()) return false;
68
69 auto pos = value.rfind(sst);
70 if (value.npos == pos) return false;
71 return value.length() - pos == sst.length();
72}
73
74template <typename String>
75bool index(const std::string &value, const String &search_for, uint32_t *idx) {
76 auto pos = value.find(search_for);
77
78 if (value.npos == pos) return false;
79 if (idx) *idx = static_cast<uint32_t>(pos);
80
81 return true;
82}
83
84inline bool index(const char *value, const char *search_for, uint32_t *idx) {
85 auto ptr = strstr(value, search_for);
86 if (nullptr == ptr) return false;
87 if (idx) *idx = static_cast<uint32_t>(std::distance(value, ptr));
88 return true;
89}
90
91template <typename String1, typename String2>
92bool starts_with(const String1 &value, const String2 &search_for) {
93 using namespace helper::string;
94 if (is_empty(search_for)) return false;
95
96 uint32_t idx;
97 if (index(value, search_for, &idx)) {
98 return idx == 0;
99 }
100 return false;
101}
102
103} // namespace helper
104
105#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_STRING_CONTAINS_H_
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:40
Definition: generic.h:32
const char * cstr(const char *str)
Definition: generic.h:36
bool is_empty(const std::string &str)
Definition: generic.h:34
size_t size(const char *str)
Definition: generic.h:38
Definition: cache.h:33
bool icontains(const String1 &value, const String2 &sst)
Definition: contains.h:43
bool contains(const String1 &value, const String2 &sst)
Definition: contains.h:37
bool ends_with(const std::string &value, const std::string &sst)
Definition: contains.h:66
bool starts_with(const String1 &value, const String2 &search_for)
Definition: contains.h:92
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:75
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
char tolower(const char &ch)
Definition: parsing_helpers.h:41