MySQL 8.0.33
Source Code Documentation
strfunc.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 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
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef STRFUNC_INCLUDED
24#define STRFUNC_INCLUDED
25
26#include <stddef.h>
27#include <sys/types.h>
28#include <cstring>
29#include <utility>
30
31#include "lex_string.h"
32#include "m_ctype.h"
33#include "my_inttypes.h"
34#include "mysql/mysql_lex_string.h" // MYSQL_LEX_CSTRING
35
36class THD;
37struct MEM_ROOT;
38struct TYPELIB;
39
40ulonglong find_set(const TYPELIB *lib, const char *x, size_t length,
41 const CHARSET_INFO *cs, const char **err_pos, uint *err_len,
42 bool *set_warning);
43uint find_type(const TYPELIB *lib, const char *find, size_t length,
44 bool part_match);
45uint find_type2(const TYPELIB *lib, const char *find, size_t length,
46 const CHARSET_INFO *cs);
47uint check_word(TYPELIB *lib, const char *val, const char *end,
48 const char **end_of_word);
50 const char *lib[]);
52 const char *lib[]);
54 const char *lib[], bool quoted);
55
56size_t strconvert(const CHARSET_INFO *from_cs, const char *from,
57 CHARSET_INFO *to_cs, char *to, size_t to_length,
58 uint *errors);
59
60/**
61 Return a LEX_CSTRING handle to a std::string like (meaning something
62 which has the c_str() and length() member functions). Note that the
63 std::string-like object retains ownership of the character array,
64 and consequently the returned LEX_CSTRING is only valid as long as the
65 std::string-like object is valid.
66
67 @param s std::string-like object
68
69 @return LEX_CSTRING handle to string
70*/
71template <class STDSTRINGLIKE_TYPE>
72MYSQL_LEX_CSTRING lex_cstring_handle(const STDSTRINGLIKE_TYPE &s) {
73 return {s.c_str(), s.length()};
74}
75
76/**
77 Lowercase a string according to charset.
78
79 @param ci pointer to charset for conversion
80 @param s string to lower-case
81 @retval modified argument if r-value
82 @retval copy of modified argument if lvalue (meaningless, don't use)
83 */
84template <class STRLIKE_TYPE>
85STRLIKE_TYPE casedn(const CHARSET_INFO *ci, STRLIKE_TYPE &&s) {
86 s.resize(ci->casedn_multiply * s.size());
87 s.resize(my_casedn_str(ci, &s.front()));
88 return std::forward<STRLIKE_TYPE>(s);
89}
90
91/**
92 Lowercase a string according to charset. Overload for const T& which
93 copies argument and forwards to T&& overload.
94
95 @param ci pointer to charset for conversion
96 @param src string to lower-case
97 @retval modified copy of argument
98 */
99
100template <class STRLIKE_TYPE>
101STRLIKE_TYPE casedn(const CHARSET_INFO *ci, const STRLIKE_TYPE &src) {
102 return casedn(ci, STRLIKE_TYPE{src});
103}
104
105/**
106 Create a LEX_STRING in a MEM_ROOT and copy the given string
107 into it.
108
109 @param mem_root MEM_ROOT where to allocate the LEX_STRING.
110 @param str string to be copied into the LEX_STRING.
111 @param length length of str, in bytes
112
113 @return nullptr on failure, or pointer to the LEX_STRING object
114*/
116 size_t length);
117
118/**
119 Copy the given string into a LEX_STRING, allocating it in the
120 given MEM_ROOT.
121
122 @param mem_root MEM_ROOT where to allocate the string.
123 @param lex_str LEX_STRING to fill with the copied string.
124 @param str string to be copied into the LEX_STRING.
125 @param length length of str, in bytes
126
127 @return true on failure (OOM), false otherwise.
128*/
130 const char *str, size_t length);
131
132/**
133 Copy the given string into a LEX_CSTRING, allocating it in the
134 given MEM_ROOT.
135
136 @param mem_root MEM_ROOT where to allocate the string.
137 @param lex_str LEX_CSTRING to fill with the copied string.
138 @param str string to be copied into the LEX_CSTRING.
139 @param length length of str, in bytes
140
141 @return true on failure (OOM), false otherwise.
142*/
144 const char *str, size_t length);
145
146/**
147 Utility function for copying a LEX_STRING_TYPE (either LEX_STRING
148 or LEX_CSTRING) onto a mem_root.
149
150 @param mem_root Where to allocate
151 @param s Source string to copy.
152
153 @return LEX_STRING_TYPE referring to the mem_root allocated string.
154 */
155template <class LEX_STRING_TYPE>
156inline LEX_STRING_TYPE LexStringDupRoot(MEM_ROOT *mem_root, LEX_STRING_TYPE s) {
157 return {strmake_root(mem_root, s.str, s.length), s.length};
158}
159
160/**
161 Utility function for copying a LEX_STRING_TYPE (either LEX_STRING
162 or LEX_CSTRING) onto a mem_root, but which does not allocate space
163 for empty strings. If called on a zero-length string EMPTY_CSTR is
164 returned (where str is "").
165
166 @param mem_root Where to allocate
167 @param s Source string to copy.
168
169 @return LEX_STRING_TYPE referring to the mem_root allocated string,
170 or EMPTY_CSTR.
171 */
172template <class LEX_STRING_TYPE>
174 LEX_STRING_TYPE s) {
175 return s.length == 0 ? LEX_CSTRING{"", 0} : LexStringDupRoot(mem_root, s);
176}
177
178/**
179 Utility function for collating (using strnncoll) two LEX_STRING_TYPEs.
180 Saves the boiler plate and casting needed when calling the function directly.
181*/
182template <class LEX_STRING_TYPE>
183inline int strnncmp_nopads(const CHARSET_INFO &cs, LEX_STRING_TYPE &&a,
184 LEX_STRING_TYPE &&b) {
185 return cs.coll->strnncoll(
186 &cs, pointer_cast<const unsigned char *>(a.str), a.length,
187 pointer_cast<const unsigned char *>(b.str), b.length, false);
188}
189#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:33
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
char * strmake_root(MEM_ROOT *root, const char *str, size_t len)
Definition: my_alloc.cc:281
#define quoted
Definition: lexyy.cc:960
A better implementation of the UNIX ctype(3) library.
#define my_casedn_str(s, a)
Definition: m_ctype.h:761
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
uint end_of_word(const char *pos)
Definition: mysqltest.cc:11229
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1054
Definition: commit_order_queue.h:33
const byte * find(const Pages *pages, const page_id_t &page_id) noexcept
Find a doublewrite copy of a page.
Definition: buf0dblwr.cc:3590
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:75
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2880
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:183
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:333
char * set_to_string(THD *thd, LEX_STRING *result, ulonglong set, const char *lib[])
Definition: strfunc.cc:297
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:72
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:156
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:59
STRLIKE_TYPE casedn(const CHARSET_INFO *ci, STRLIKE_TYPE &&s)
Lowercase a string according to charset.
Definition: strfunc.h:85
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:324
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:173
char * flagset_to_string(THD *thd, LEX_STRING *result, ulonglong set, const char *lib[])
Definition: strfunc.cc:302
uint check_word(TYPELIB *lib, const char *val, const char *end, const char **end_of_word)
Definition: strfunc.cc:194
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:226
uint find_type2(const TYPELIB *lib, const char *find, size_t length, const CHARSET_INFO *cs)
Definition: strfunc.cc:156
uint find_type(const TYPELIB *lib, const char *find, size_t length, bool part_match)
Definition: strfunc.cc:119
Definition: m_ctype.h:382
uchar casedn_multiply
Definition: m_ctype.h:404
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
Definition: mysql_lex_string.h:39
size_t length
Definition: mysql_lex_string.h:41
Definition: mysql_lex_string.h:34
Definition: typelib.h:34
Definition: result.h:29
unsigned int uint
Definition: uca9-dump.cc:74