MySQL  8.0.22
Source Code Documentation
strfunc.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
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 
36 class THD;
37 struct MEM_ROOT;
38 struct TYPELIB;
39 
40 ulonglong 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);
43 uint find_type(const TYPELIB *lib, const char *find, size_t length,
44  bool part_match);
45 uint find_type2(const TYPELIB *lib, const char *find, size_t length,
46  const CHARSET_INFO *cs);
47 uint check_word(TYPELIB *lib, const char *val, const char *end,
48  const char **end_of_word);
50  const char *lib[]);
51 char *set_to_string(THD *thd, LEX_STRING *result, ulonglong set,
52  const char *lib[]);
53 char *set_to_string(THD *thd, LEX_STRING *result, ulonglong set,
54  const char *lib[], bool quoted);
55 
56 size_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  convert a hex digit into number.
62 */
63 
64 inline int hexchar_to_int(char c) {
65  if (c <= '9' && c >= '0') return c - '0';
66  c |= 32;
67  if (c <= 'f' && c >= 'a') return c - 'a' + 10;
68  return -1;
69 }
70 
71 /**
72  Return a LEX_CSTRING handle to a std::string like (meaning someting
73  which has the c_str() and length() member functions). Note that the
74  std::string-like object retains ownership of the character array,
75  and consquently the returned LEX_CSTRING is only valid as long as the
76  std::string-like object is valid.
77 
78  @param s std::string-like object
79 
80  @return LEX_CSTRING handle to string
81 */
82 template <class STDSTRINGLIKE_TYPE>
83 MYSQL_LEX_CSTRING lex_cstring_handle(const STDSTRINGLIKE_TYPE &s) {
84  return {s.c_str(), s.length()};
85 }
86 
87 /**
88  Lowercase a string according to charset.
89 
90  @param ci pointer to charset for conversion
91  @param s string to lower-case
92  @retval modified argument if r-value
93  @retval copy of modified argument if lvalue (meaningless, don't use)
94  */
95 template <class STRLIKE_TYPE>
96 STRLIKE_TYPE casedn(const CHARSET_INFO *ci, STRLIKE_TYPE &&s) {
97  s.resize(ci->casedn_multiply * s.size());
98  s.resize(my_casedn_str(ci, &s.front()));
99  return std::forward<STRLIKE_TYPE>(s);
100 }
101 
102 /**
103  Lowercase a string according to charset. Overload for const T& which
104  copies argument and forwards to T&& overload.
105 
106  @param ci pointer to charset for conversion
107  @param src string to lower-case
108  @retval modified copy of argument
109  */
110 
111 template <class STRLIKE_TYPE>
112 STRLIKE_TYPE casedn(const CHARSET_INFO *ci, const STRLIKE_TYPE &src) {
113  return casedn(ci, STRLIKE_TYPE{src});
114 }
115 
116 /**
117  Create a LEX_STRING in a MEM_ROOT and copy the given string
118  into it.
119 
120  @param mem_root MEM_ROOT where to allocate the LEX_STRING.
121  @param str string to be copied into the LEX_STRING.
122  @param length length of str, in bytes
123 
124  @return nullptr on failure, or pointer to the LEX_STRING object
125 */
127  size_t length);
128 
129 /**
130  Copy the given string into a LEX_STRING, allocating it in the
131  given MEM_ROOT.
132 
133  @param mem_root MEM_ROOT where to allocate the string.
134  @param lex_str LEX_STRING to fill with the copied string.
135  @param str string to be copied into the LEX_STRING.
136  @param length length of str, in bytes
137 
138  @return true on failure (OOM), false otherwise.
139 */
140 bool lex_string_strmake(MEM_ROOT *mem_root, LEX_STRING *lex_str,
141  const char *str, size_t length);
142 
143 /**
144  Copy the given string into a LEX_CSTRING, allocating it in the
145  given MEM_ROOT.
146 
147  @param mem_root MEM_ROOT where to allocate the string.
148  @param lex_str LEX_CSTRING to fill with the copied string.
149  @param str string to be copied into the LEX_CSTRING.
150  @param length length of str, in bytes
151 
152  @return true on failure (OOM), false otherwise.
153 */
154 bool lex_string_strmake(MEM_ROOT *mem_root, LEX_CSTRING *lex_str,
155  const char *str, size_t length);
156 
157 /**
158  Utility function for copying a LEX_STRING_TYPE (either LEX_STRING
159  or LEX_CSTRING) onto a mem_root.
160 
161  @param mem_root Where to allocate
162  @param s Source string to copy.
163 
164  @return LEX_STRING_TYPE refering to the mem_root allocated string.
165  */
166 template <class LEX_STRING_TYPE>
167 inline LEX_STRING_TYPE LexStringDupRoot(MEM_ROOT *mem_root, LEX_STRING_TYPE s) {
168  return {strmake_root(mem_root, s.str, s.length), s.length};
169 }
170 
171 /**
172  Utility function for copying a LEX_STRING_TYPE (either LEX_STRING
173  or LEX_CSTRING) onto a mem_root, but which does not allocate space
174  for empty strings. If called on a zero-length string EMPTY_CSTR is
175  returned (where str is "").
176 
177  @param mem_root Where to allocate
178  @param s Source string to copy.
179 
180  @return LEX_STRING_TYPE refering to the mem_root allocated string,
181  or EMPTY_CSTR.
182  */
183 template <class LEX_STRING_TYPE>
184 inline LEX_STRING_TYPE LexStringDupRootUnlessEmpty(MEM_ROOT *mem_root,
185  LEX_STRING_TYPE s) {
186  return s.length == 0 ? LEX_CSTRING{"", 0} : LexStringDupRoot(mem_root, s);
187 }
188 
189 /**
190  Utility function for collating (using strnncoll) two LEX_STRING_TYPEs.
191  Saves the boiler plate and casting needed when calling the function directly.
192 */
193 template <class LEX_STRING_TYPE>
194 inline int strnncmp_nopads(const CHARSET_INFO &cs, LEX_STRING_TYPE &&a,
195  LEX_STRING_TYPE &&b) {
196  return cs.coll->strnncoll(
197  &cs, pointer_cast<const unsigned char *>(a.str), a.length,
198  pointer_cast<const unsigned char *>(b.str), b.length, false);
199 }
200 #endif /* STRFUNC_INCLUDED */
Definition: result.h:29
unsigned long long int ulonglong
Definition: my_inttypes.h:55
Definition: mysql_lex_string.h:34
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
MYSQL_LEX_CSTRING lex_cstring_handle(const STDSTRINGLIKE_TYPE &s)
Return a LEX_CSTRING handle to a std::string like (meaning someting which has the c_str() and length(...
Definition: strfunc.h:83
Some integer typedefs for easier portability.
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
int(* strnncoll)(const CHARSET_INFO *, const uchar *, size_t, const uchar *, size_t, bool)
Definition: m_ctype.h:179
uint end_of_word(const char *pos)
Definition: mysqltest.cc:10933
Definition: mysql_lex_string.h:39
MY_COLLATION_HANDLER * coll
Definition: m_ctype.h:392
int hexchar_to_int(char c)
convert a hex digit into number.
Definition: strfunc.h:64
char * set_to_string(THD *thd, LEX_STRING *result, ulonglong set, const char *lib[])
Definition: strfunc.cc:297
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:184
uchar casedn_multiply
Definition: m_ctype.h:381
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:194
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
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
uint find_type(const TYPELIB *lib, const char *find, size_t length, bool part_match)
Definition: strfunc.cc:119
size_t length
Definition: mysql_lex_string.h:41
unsigned int uint
Definition: uca-dump.cc:29
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
uint find_type2(const TYPELIB *lib, const char *find, size_t length, const CHARSET_INFO *cs)
Definition: strfunc.cc:156
#define my_casedn_str(s, a)
Definition: m_ctype.h:735
Definition: m_ctype.h:359
Definition: typelib.h:34
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
char * flagset_to_string(THD *thd, LEX_STRING *result, ulonglong set, const char *lib[])
Definition: strfunc.cc:302
#define quoted
Definition: lexyy.cc:960
STRLIKE_TYPE casedn(const CHARSET_INFO *ci, STRLIKE_TYPE &&s)
Lowercase a string according to charset.
Definition: strfunc.h:96
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:167
A better implementation of the UNIX ctype(3) library.
char * strmake_root(MEM_ROOT *root, const char *str, size_t len)
Definition: my_alloc.cc:259
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
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
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:803
const byte * find(const Pages *pages, const page_id_t &page_id) noexcept
Find a doublewrite copy of a page.
Definition: buf0dblwr.cc:2222