MySQL 8.4.2
Source Code Documentation
strfunc.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef STRFUNC_INCLUDED
25#define STRFUNC_INCLUDED
26
27#include <stddef.h>
28#include <sys/types.h>
29#include <cstring>
30#include <utility>
31
32#include "lex_string.h"
33#include "my_inttypes.h"
34#include "mysql/mysql_lex_string.h" // MYSQL_LEX_CSTRING
36#include "template_utils.h"
37
38class THD;
39struct MEM_ROOT;
40struct TYPELIB;
41
42ulonglong find_set(const TYPELIB *lib, const char *x, size_t length,
43 const CHARSET_INFO *cs, const char **err_pos, uint *err_len,
44 bool *set_warning);
45uint find_type(const TYPELIB *lib, const char *find, size_t length,
46 bool part_match);
47uint find_type2(const TYPELIB *lib, const char *find, size_t length,
48 const CHARSET_INFO *cs);
49uint check_word(TYPELIB *lib, const char *val, const char *end,
50 const char **end_of_word);
52 const char *lib[]);
54 const char *lib[]);
56 const char *lib[], bool quoted);
57
58size_t strconvert(const CHARSET_INFO *from_cs, const char *from,
59 CHARSET_INFO *to_cs, char *to, size_t to_length,
60 uint *errors);
61
62/**
63 Return a LEX_CSTRING handle to a std::string like (meaning something
64 which has the c_str() and length() member functions). Note that the
65 std::string-like object retains ownership of the character array,
66 and consequently the returned LEX_CSTRING is only valid as long as the
67 std::string-like object is valid.
68
69 @param s std::string-like object
70
71 @return LEX_CSTRING handle to string
72*/
73template <class STDSTRINGLIKE_TYPE>
74MYSQL_LEX_CSTRING lex_cstring_handle(const STDSTRINGLIKE_TYPE &s) {
75 return {s.c_str(), s.length()};
76}
77
78/**
79 Lowercase a string according to charset.
80
81 @param ci pointer to charset for conversion
82 @param s string to lower-case
83 @retval modified argument if r-value
84 @retval copy of modified argument if lvalue (meaningless, don't use)
85 */
86template <class STRLIKE_TYPE>
87STRLIKE_TYPE casedn(const CHARSET_INFO *ci, STRLIKE_TYPE &&s) {
88 s.resize(ci->casedn_multiply * s.size());
89 s.resize(my_casedn_str(ci, &s.front()));
90 return std::forward<STRLIKE_TYPE>(s);
91}
92
93/**
94 Lowercase a string according to charset. Overload for const T& which
95 copies argument and forwards to T&& overload.
96
97 @param ci pointer to charset for conversion
98 @param src string to lower-case
99 @retval modified copy of argument
100 */
101
102template <class STRLIKE_TYPE>
103STRLIKE_TYPE casedn(const CHARSET_INFO *ci, const STRLIKE_TYPE &src) {
104 return casedn(ci, STRLIKE_TYPE{src});
105}
106
107/**
108 Create a LEX_STRING in a MEM_ROOT and copy the given string
109 into it.
110
111 @param mem_root MEM_ROOT where to allocate the LEX_STRING.
112 @param str string to be copied into the LEX_STRING.
113 @param length length of str, in bytes
114
115 @return nullptr on failure, or pointer to the LEX_STRING object
116*/
118 size_t length);
119
120/**
121 Copy the given string into a LEX_STRING, allocating it in the
122 given MEM_ROOT.
123
124 @param mem_root MEM_ROOT where to allocate the string.
125 @param lex_str LEX_STRING to fill with the copied string.
126 @param str string to be copied into the LEX_STRING.
127 @param length length of str, in bytes
128
129 @return true on failure (OOM), false otherwise.
130*/
132 const char *str, size_t length);
133
134/**
135 Copy the given string into a LEX_CSTRING, allocating it in the
136 given MEM_ROOT.
137
138 @param mem_root MEM_ROOT where to allocate the string.
139 @param lex_str LEX_CSTRING to fill with the copied string.
140 @param str string to be copied into the LEX_CSTRING.
141 @param length length of str, in bytes
142
143 @return true on failure (OOM), false otherwise.
144*/
146 const char *str, size_t length);
147
148/**
149 Utility function for copying a LEX_STRING_TYPE (either LEX_STRING
150 or LEX_CSTRING) onto a mem_root.
151
152 @param mem_root Where to allocate
153 @param s Source string to copy.
154
155 @return LEX_STRING_TYPE referring to the mem_root allocated string.
156 */
157template <class LEX_STRING_TYPE>
158inline LEX_STRING_TYPE LexStringDupRoot(MEM_ROOT *mem_root, LEX_STRING_TYPE s) {
159 return {strmake_root(mem_root, s.str, s.length), s.length};
160}
161
162/**
163 Utility function for copying a LEX_STRING_TYPE (either LEX_STRING
164 or LEX_CSTRING) onto a mem_root, but which does not allocate space
165 for empty strings. If called on a zero-length string EMPTY_CSTR is
166 returned (where str is "").
167
168 @param mem_root Where to allocate
169 @param s Source string to copy.
170
171 @return LEX_STRING_TYPE referring to the mem_root allocated string,
172 or EMPTY_CSTR.
173 */
174template <class LEX_STRING_TYPE>
176 LEX_STRING_TYPE s) {
177 return s.length == 0 ? LEX_CSTRING{"", 0} : LexStringDupRoot(mem_root, s);
178}
179
180/**
181 Utility function for collating (using strnncoll) two LEX_STRING_TYPEs.
182 Saves the boiler plate and casting needed when calling the function directly.
183*/
184template <class LEX_STRING_TYPE>
185inline int strnncmp_nopads(const CHARSET_INFO &cs, LEX_STRING_TYPE &&a,
186 LEX_STRING_TYPE &&b) {
187 return cs.coll->strnncoll(
188 &cs, pointer_cast<const unsigned char *>(a.str), a.length,
189 pointer_cast<const unsigned char *>(b.str), b.length, false);
190}
191#endif /* STRFUNC_INCLUDED */
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
char * strmake_root(MEM_ROOT *root, const char *str, size_t len)
Definition: my_alloc.cc:287
#define quoted
Definition: lexyy.cc:960
A better implementation of the UNIX ctype(3) library.
size_t my_casedn_str(const CHARSET_INFO *cs, char *str)
Definition: m_ctype.h:734
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
uint end_of_word(const char *pos)
Definition: mysqltest.cc:11290
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
Definition: commit_order_queue.h:34
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:39
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2882
int strnncmp_nopads(const CHARSET_INFO &cs, LEX_STRING_TYPE &&a, LEX_STRING_TYPE &&b)
Utility function for collating (using strnncoll) two LEX_STRING_TYPEs.
Definition: strfunc.h:185
bool lex_string_strmake(MEM_ROOT *mem_root, LEX_STRING *lex_str, const char *str, size_t length)
Copy the given string into a LEX_STRING, allocating it in the given MEM_ROOT.
Definition: strfunc.cc:334
char * set_to_string(THD *thd, LEX_STRING *result, ulonglong set, const char *lib[])
Definition: strfunc.cc:298
MYSQL_LEX_CSTRING lex_cstring_handle(const STDSTRINGLIKE_TYPE &s)
Return a LEX_CSTRING handle to a std::string like (meaning something which has the c_str() and length...
Definition: strfunc.h:74
LEX_STRING_TYPE LexStringDupRoot(MEM_ROOT *mem_root, LEX_STRING_TYPE s)
Utility function for copying a LEX_STRING_TYPE (either LEX_STRING or LEX_CSTRING) onto a mem_root.
Definition: strfunc.h:158
ulonglong find_set(const TYPELIB *lib, const char *x, size_t length, const CHARSET_INFO *cs, const char **err_pos, uint *err_len, bool *set_warning)
Definition: strfunc.cc:60
STRLIKE_TYPE casedn(const CHARSET_INFO *ci, STRLIKE_TYPE &&s)
Lowercase a string according to charset.
Definition: strfunc.h:87
LEX_STRING * make_lex_string_root(MEM_ROOT *mem_root, const char *str, size_t length)
Create a LEX_STRING in a MEM_ROOT and copy the given string into it.
Definition: strfunc.cc:325
LEX_STRING_TYPE LexStringDupRootUnlessEmpty(MEM_ROOT *mem_root, LEX_STRING_TYPE s)
Utility function for copying a LEX_STRING_TYPE (either LEX_STRING or LEX_CSTRING) onto a mem_root,...
Definition: strfunc.h:175
char * flagset_to_string(THD *thd, LEX_STRING *result, ulonglong set, const char *lib[])
Definition: strfunc.cc:303
uint check_word(TYPELIB *lib, const char *val, const char *end, const char **end_of_word)
Definition: strfunc.cc:195
size_t strconvert(const CHARSET_INFO *from_cs, const char *from, CHARSET_INFO *to_cs, char *to, size_t to_length, uint *errors)
Definition: strfunc.cc:227
uint find_type2(const TYPELIB *lib, const char *find, size_t length, const CHARSET_INFO *cs)
Definition: strfunc.cc:157
uint find_type(const TYPELIB *lib, const char *find, size_t length, bool part_match)
Definition: strfunc.cc:120
Definition: m_ctype.h:423
uint8_t casedn_multiply
Definition: m_ctype.h:445
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_lex_string.h:40
size_t length
Definition: mysql_lex_string.h:42
Definition: mysql_lex_string.h:35
Definition: typelib.h:35
Definition: result.h:30