MySQL 8.0.40
Source Code Documentation
m_string.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2000, 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 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, version 2.0, 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#ifndef M_STRING_INCLUDED
26#define M_STRING_INCLUDED
27
28/**
29 @file include/m_string.h
30*/
31
32#include <float.h>
33#include <limits.h>
34#include <stdbool.h> // IWYU pragma: keep
35#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include <algorithm>
41
42#include "decimal.h"
43#include "lex_string.h"
44#include "my_config.h"
45#include "my_inttypes.h"
46#include "my_macros.h"
47
48/**
49 Definition of the null string (a null pointer of type char *),
50 used in some of our string handling code. New code should use
51 nullptr instead.
52*/
53#define NullS (char *)0
54
55/*
56 my_str_malloc(), my_str_realloc() and my_str_free() are assigned to
57 implementations in strings/alloc.cc, but can be overridden in
58 the calling program.
59 */
60extern void *(*my_str_malloc)(size_t);
61extern void *(*my_str_realloc)(void *, size_t);
62extern void (*my_str_free)(void *);
63
64/* Declared in int2str.cc. */
65extern const char _dig_vec_upper[];
66extern const char _dig_vec_lower[];
67
68/* Prototypes for string functions */
69
70extern char *strmake(char *dst, const char *src, size_t length);
71extern char *strcont(char *src, const char *set);
72extern char *strxmov(char *dst, const char *src, ...);
73extern char *strxnmov(char *dst, size_t len, const char *src, ...);
74
75/*
76 bchange(dst, old_length, src, new_length, tot_length)
77 replaces old_length characters at dst to new_length characters from
78 src in a buffer with tot_length bytes.
79*/
80static inline void bchange(uchar *dst, size_t old_length, const uchar *src,
81 size_t new_length, size_t tot_length) {
82 memmove(dst + new_length, dst + old_length, tot_length - old_length);
83 memcpy(dst, src, new_length);
84}
85
86/*
87 strend(s) returns a character pointer to the NUL which ends s. That
88 is, strend(s)-s == strlen(s). This is useful for adding things at
89 the end of strings. It is redundant, because strchr(s,'\0') could
90 be used instead, but this is clearer and faster.
91*/
92static inline const char *strend(const char *s) {
93 while (*s++) {
94 }
95 return s - 1;
96}
97
98static inline char *strend(char *s) {
99 while (*s++) {
100 }
101 return s - 1;
102}
103
104/*
105 strcend(s, c) returns a pointer to the first place in s where c
106 occurs, or a pointer to the end-null of s if c does not occur in s.
107*/
108static inline const char *strcend(const char *s, char c) {
109 for (;;) {
110 if (*s == c) return s;
111 if (!*s++) return s - 1;
112 }
113}
114
115/*
116 strfill(dest, len, fill) makes a string of fill-characters. The result
117 string is of length == len. The des+len character is always set to NULL.
118 strfill() returns pointer to dest+len;
119*/
120static inline char *strfill(char *s, size_t len, char fill) {
121 while (len--) *s++ = fill;
122 *(s) = '\0';
123 return (s);
124}
125
126/*
127 my_stpmov(dst, src) moves all the characters of src (including the
128 closing NUL) to dst, and returns a pointer to the new closing NUL in
129 dst. The similar UNIX routine strcpy returns the old value of dst,
130 which I have never found useful. my_stpmov(my_stpmov(dst,a),b) moves a//b
131 into dst, which seems useful.
132*/
133static inline char *my_stpmov(char *dst, const char *src) {
134 while ((*dst++ = *src++)) {
135 }
136 return dst - 1;
137}
138
139/*
140 my_stpnmov(dst,src,length) moves length characters, or until end, of src to
141 dst and appends a closing NUL to dst if src is shorter than length.
142 The result is a pointer to the first NUL in dst, or is dst+n if dst was
143 truncated.
144*/
145static inline char *my_stpnmov(char *dst, const char *src, size_t n) {
146 while (n-- != 0) {
147 if (!(*dst++ = *src++)) return (char *)dst - 1;
148 }
149 return dst;
150}
151
152/**
153 Copy a string from src to dst until (and including) terminating null byte.
154
155 @param dst Destination
156 @param src Source
157
158 @note src and dst cannot overlap.
159 Use my_stpmov() if src and dst overlaps.
160
161 @note Unsafe, consider using my_stpnpy() instead.
162
163 @return pointer to terminating null byte.
164*/
165static inline char *my_stpcpy(char *dst, const char *src) {
166#if defined(HAVE_BUILTIN_STPCPY)
167 /*
168 If __builtin_stpcpy() is available, use it instead of stpcpy(), since GCC in
169 some situations is able to transform __builtin_stpcpy() into more efficient
170 strcpy() or memcpy() calls. It does not perform these transformations for a
171 plain call to stpcpy() when the compiler runs in strict mode. See GCC bug
172 82429.
173 */
174 return __builtin_stpcpy(dst, src);
175#elif defined(HAVE_STPCPY)
176 return stpcpy(dst, src);
177#else
178 /* Fallback to implementation supporting overlap. */
179 return my_stpmov(dst, src);
180#endif
181}
182
183/**
184 Copy fixed-size string from src to dst.
185
186 @param dst Destination
187 @param src Source
188 @param n Maximum number of characters to copy.
189
190 @note src and dst cannot overlap
191 Use my_stpnmov() if src and dst overlaps.
192
193 @return pointer to terminating null byte.
194*/
195static inline char *my_stpncpy(char *dst, const char *src, size_t n) {
196#if defined(HAVE_STPNCPY)
197 return stpncpy(dst, src, n);
198#else
199 /* Fallback to implementation supporting overlap. */
200 return my_stpnmov(dst, src, n);
201#endif
202}
203
204static inline longlong my_strtoll(const char *nptr, char **endptr, int base) {
205#if defined _WIN32
206 return _strtoi64(nptr, endptr, base);
207#else
208 return strtoll(nptr, endptr, base);
209#endif
210}
211
212static inline ulonglong my_strtoull(const char *nptr, char **endptr, int base) {
213#if defined _WIN32
214 return _strtoui64(nptr, endptr, base);
215#else
216 return strtoull(nptr, endptr, base);
217#endif
218}
219
220static inline char *my_strtok_r(char *str, const char *delim, char **saveptr) {
221#if defined _WIN32
222 return strtok_s(str, delim, saveptr);
223#else
224 return strtok_r(str, delim, saveptr);
225#endif
226}
227
228/* native_ rather than my_ since my_strcasecmp already exists */
229static inline int native_strcasecmp(const char *s1, const char *s2) {
230#if defined _WIN32
231 return _stricmp(s1, s2);
232#else
233 return strcasecmp(s1, s2);
234#endif
235}
236
237/* native_ rather than my_ for consistency with native_strcasecmp */
238static inline int native_strncasecmp(const char *s1, const char *s2, size_t n) {
239#if defined _WIN32
240 return _strnicmp(s1, s2, n);
241#else
242 return strncasecmp(s1, s2, n);
243#endif
244}
245
246/*
247 is_prefix(s, t) returns 1 if s starts with t.
248 A empty t is always a prefix.
249*/
250static inline int is_prefix(const char *s, const char *t) {
251 while (*t)
252 if (*s++ != *t++) return 0;
253 return 1; /* WRONG */
254}
255
256/* Conversion routines */
258
259double my_strtod(const char *str, const char **end, int *error);
260size_t my_fcvt(double x, int precision, char *to, bool *error);
261size_t my_fcvt_compact(double x, char *to, bool *error);
262size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
263 bool *error);
264
265/*
266 The longest string my_fcvt can return is 311 + "precision" bytes.
267 Here we assume that we never call my_fcvt() with precision >=
268 DECIMAL_NOT_SPECIFIED
269 (+ 1 byte for the terminating '\0').
270*/
272
273/*
274 We want to use the 'e' format in some cases even if we have enough space
275 for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
276 and to improve it for numbers < 10^(-4).
277 That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
278 it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
279 We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
280*/
281#define MAX_DECPT_FOR_F_FORMAT DBL_DIG
282
283/*
284 The maximum possible field width for my_gcvt() conversion.
285 (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
286 MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
287*/
288#define MY_GCVT_MAX_FIELD_WIDTH \
289 (DBL_DIG + 4 + std::max(5, MAX_DECPT_FOR_F_FORMAT))
290
291const char *str2int(const char *src, int radix, long lower, long upper,
292 long *val);
293longlong my_strtoll10(const char *nptr, const char **endptr, int *error);
294char *ll2str(int64_t val, char *dst, int radix, bool upcase);
295char *longlong10_to_str(int64_t val, char *dst, int radix);
296
297inline char *longlong2str(int64_t val, char *dst, int radix) {
298 return ll2str(val, dst, radix, true);
299}
300
301/*
302 This function saves a longlong value in a buffer and returns the pointer to
303 the buffer.
304*/
305static inline char *llstr(longlong value, char *buff) {
306 longlong10_to_str(value, buff, -10);
307 return buff;
308}
309
310static inline char *ullstr(longlong value, char *buff) {
311 longlong10_to_str(value, buff, 10);
312 return buff;
313}
314
315#define STRING_WITH_LEN(X) (X), ((sizeof(X) - 1))
316
317/**
318 Skip trailing space (ASCII spaces only).
319
320 @return New end of the string.
321*/
322static inline const uchar *skip_trailing_space(const uchar *ptr, size_t len) {
323 const uchar *end = ptr + len;
324 while (end - ptr >= 8) {
325 uint64_t chunk;
326 memcpy(&chunk, end - 8, sizeof(chunk));
327 if (chunk != 0x2020202020202020ULL) break;
328 end -= 8;
329 }
330 while (end > ptr && end[-1] == 0x20) end--;
331 return (end);
332}
333
334/*
335 Format a double (representing number of bytes) into a human-readable string.
336
337 @param buf Buffer used for printing
338 @param buf_len Length of buffer
339 @param dbl_val Value to be formatted
340
341 @note
342 Sample output format: 42 1K 234M 2G
343 If we exceed ULLONG_MAX YiB we give up, and convert to "+INF".
344
345 @todo Consider writing KiB GiB etc, since we use 1024 rather than 1000
346 */
347static inline void human_readable_num_bytes(char *buf, int buf_len,
348 double dbl_val) {
349 const char size[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
350 unsigned int i;
351 for (i = 0; dbl_val > 1024 && i < sizeof(size) - 1; i++) dbl_val /= 1024;
352 const char mult = size[i];
353 // 18446744073709551615 Yottabytes should be enough for most ...
354 // ULLONG_MAX is not exactly representable as a double. This is the largest
355 // double that is still below ULLONG_MAX.
356 if (dbl_val > 18446744073709549568.0)
357 snprintf(buf, buf_len, "+INF");
358 else
359 snprintf(buf, buf_len, "%llu%c", (unsigned long long)dbl_val, mult);
360}
361
362static inline void lex_string_set(LEX_STRING *lex_str, char *c_str) {
363 lex_str->str = c_str;
364 lex_str->length = strlen(c_str);
365}
366
367static inline void lex_cstring_set(LEX_CSTRING *lex_str, const char *c_str) {
368 lex_str->str = c_str;
369 lex_str->length = strlen(c_str);
370}
371
372#endif // M_STRING_INCLUDED
static constexpr int DECIMAL_NOT_SPECIFIED
Definition: decimal.h:154
static Bigint * mult(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:923
const char _dig_vec_lower[]
Definition: int2str.cc:41
static const char * strcend(const char *s, char c)
Definition: m_string.h:108
static int is_prefix(const char *s, const char *t)
Definition: m_string.h:250
char * longlong10_to_str(int64_t val, char *dst, int radix)
Converts a 64-bit integer to its string representation in decimal notation.
Definition: int2str.cc:101
static int native_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: m_string.h:238
char * strxmov(char *dst, const char *src,...)
Definition: strxmov.cc:48
size_t my_fcvt_compact(double x, char *to, bool *error)
Converts a given floating point number to a zero-terminated string representation using the 'f' forma...
Definition: dtoa.cc:234
static void bchange(uchar *dst, size_t old_length, const uchar *src, size_t new_length, size_t tot_length)
Definition: m_string.h:80
static constexpr int FLOATING_POINT_BUFFER
Definition: m_string.h:271
static void lex_string_set(LEX_STRING *lex_str, char *c_str)
Definition: m_string.h:362
char * strcont(char *src, const char *set)
Definition: strcont.cc:37
longlong my_strtoll10(const char *nptr, const char **endptr, int *error)
Definition: my_strtoll10.cc:87
const char _dig_vec_upper[]
Definition: int2str.cc:40
static const char * strend(const char *s)
Definition: m_string.h:92
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to, bool *error)
Converts a given floating point number to a zero-terminated string representation with a given field ...
Definition: dtoa.cc:302
static char * my_stpmov(char *dst, const char *src)
Definition: m_string.h:133
static const uchar * skip_trailing_space(const uchar *ptr, size_t len)
Skip trailing space (ASCII spaces only).
Definition: m_string.h:322
static char * my_stpcpy(char *dst, const char *src)
Copy a string from src to dst until (and including) terminating null byte.
Definition: m_string.h:165
static longlong my_strtoll(const char *nptr, char **endptr, int base)
Definition: m_string.h:204
static char * my_stpncpy(char *dst, const char *src, size_t n)
Copy fixed-size string from src to dst.
Definition: m_string.h:195
char * longlong2str(int64_t val, char *dst, int radix)
Definition: m_string.h:297
static char * strfill(char *s, size_t len, char fill)
Definition: m_string.h:120
static ulonglong my_strtoull(const char *nptr, char **endptr, int base)
Definition: m_string.h:212
char * ll2str(int64_t val, char *dst, int radix, bool upcase)
Converts a 64-bit integer value to its character form and moves it to the destination buffer followed...
Definition: int2str.cc:60
static int native_strcasecmp(const char *s1, const char *s2)
Definition: m_string.h:229
static char * my_stpnmov(char *dst, const char *src, size_t n)
Definition: m_string.h:145
my_gcvt_arg_type
Definition: m_string.h:257
@ MY_GCVT_ARG_DOUBLE
Definition: m_string.h:257
@ MY_GCVT_ARG_FLOAT
Definition: m_string.h:257
char * strxnmov(char *dst, size_t len, const char *src,...)
Definition: strxnmov.cc:54
static char * my_strtok_r(char *str, const char *delim, char **saveptr)
Definition: m_string.h:220
static void human_readable_num_bytes(char *buf, int buf_len, double dbl_val)
Definition: m_string.h:347
static char * llstr(longlong value, char *buff)
Definition: m_string.h:305
void(* my_str_free)(void *)
Definition: str_alloc.cc:44
static void lex_cstring_set(LEX_CSTRING *lex_str, const char *c_str)
Definition: m_string.h:367
const char * str2int(const char *src, int radix, long lower, long upper, long *val)
size_t my_fcvt(double x, int precision, char *to, bool *error)
Converts a given floating point number to a zero-terminated string representation using the 'f' forma...
Definition: dtoa.cc:203
double my_strtod(const char *str, const char **end, int *error)
Converts string to double (string does not have to be zero-terminated)
Definition: dtoa.cc:515
static char * ullstr(longlong value, char *buff)
Definition: m_string.h:310
char * strmake(char *dst, const char *src, size_t length)
Definition: strmake.cc:43
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
Some common macros.
Log error(cerr, "ERROR")
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
Definition: buf0block_hint.cc:30
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
static std::string lower(std::string str)
Definition: config_parser.cc:65
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:2883
required string type
Definition: replication_group_member_actions.proto:34
Definition: mysql_lex_string.h:40
const char * str
Definition: mysql_lex_string.h:41
size_t length
Definition: mysql_lex_string.h:42
Definition: mysql_lex_string.h:35
char * str
Definition: mysql_lex_string.h:36
size_t length
Definition: mysql_lex_string.h:37
int n
Definition: xcom_base.cc:509