MySQL 8.0.39
Source Code Documentation
sql_string.h
Go to the documentation of this file.
1#ifndef SQL_STRING_INCLUDED
2#define SQL_STRING_INCLUDED
3
4/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/**
28 @file include/sql_string.h
29 Our own string classes, used pervasively throughout the executor.
30 See in particular the comment on String before you use anything from here.
31*/
32
33#include <assert.h>
34#include <stdint.h>
35#include <string.h>
36#include <sys/types.h>
37#include <new>
38#include <string>
39#include <string_view>
40
41#include "lex_string.h"
42#include "m_ctype.h" // my_convert
43#include "m_string.h" // LEX_CSTRING
44#include "memory_debugging.h"
45#include "my_alloc.h"
46#include "my_compiler.h"
47#include "my_sys.h"
48
49#include "my_inttypes.h"
51#include "mysql/mysql_lex_string.h" // LEX_STRING
53#include "mysql/service_mysql_alloc.h" // my_free
54
55struct MEM_ROOT;
56
57#ifdef MYSQL_SERVER
59#define STRING_PSI_MEMORY_KEY key_memory_String_value
60#else
61#define STRING_PSI_MEMORY_KEY PSI_NOT_INSTRUMENTED
62#endif
63
64/**
65 A wrapper class for null-terminated constant strings.
66 Constructors make sure that the position of the '\0' terminating byte
67 in m_str is always in sync with m_length.
68
69 This class must stay as small as possible as we often
70 pass it and its descendants (such as Name_string) into functions
71 using call-by-value evaluation.
72
73 Don't add new members or virtual methods to this class!
74*/
76 private:
77 const char *m_str;
78 size_t m_length;
79
80 public:
81 /**
82 Initialize from a C string whose length is already known.
83 */
84 void set(const char *str_arg, size_t length_arg) {
85 // NULL is allowed only with length==0
86 assert(str_arg || length_arg == 0);
87 // For non-NULL, make sure length_arg is in sync with '\0' terminator.
88 assert(!str_arg || str_arg[length_arg] == '\0');
89 m_str = str_arg;
90 m_length = length_arg;
91 }
92 Simple_cstring() { set(nullptr, 0); }
93 Simple_cstring(const char *str_arg, size_t length_arg) {
94 set(str_arg, length_arg);
95 }
96 Simple_cstring(const LEX_STRING arg) { set(arg.str, arg.length); }
97 Simple_cstring(const LEX_CSTRING arg) { set(arg.str, arg.length); }
98 void reset() { set(nullptr, 0); }
99 /**
100 Set to a null-terminated string.
101 */
102 void set(const char *str) { set(str, str ? strlen(str) : 0); }
103 /**
104 Return string buffer.
105 */
106 const char *ptr() const { return m_str; }
107 /**
108 Check if m_ptr is set.
109 */
110 bool is_set() const { return m_str != nullptr; }
111 /**
112 Return name length.
113 */
114 size_t length() const { return m_length; }
115 /**
116 Compare to another Simple_cstring.
117 */
118 bool eq_bin(const Simple_cstring other) const {
119 return m_length == other.m_length &&
120 memcmp(m_str, other.m_str, m_length) == 0;
121 }
122 /**
123 Copy to the given buffer
124 */
125 void strcpy(char *buff) const {
126 memcpy(buff, m_str, m_length);
127 buff[m_length] = '\0';
128 }
129};
130
131class String;
132
133struct CHARSET_INFO;
134struct IO_CACHE;
135
136bool validate_string(const CHARSET_INFO *cs, const char *str, size_t length,
137 size_t *valid_length, bool *length_error);
138int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
139String *copy_if_not_alloced(String *to, String *from, size_t from_length);
140inline size_t copy_and_convert(char *to, size_t to_length,
141 const CHARSET_INFO *to_cs, const char *from,
142 size_t from_length, const CHARSET_INFO *from_cs,
143 uint *errors) {
144 return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
145}
146size_t well_formed_copy_nchars(const CHARSET_INFO *to_cs, char *to,
147 size_t to_length, const CHARSET_INFO *from_cs,
148 const char *from, size_t from_length,
149 size_t nchars,
150 const char **well_formed_error_pos,
151 const char **cannot_convert_error_pos,
152 const char **from_end_pos);
153size_t convert_to_printable(char *to, size_t to_len, const char *from,
154 size_t from_len, const CHARSET_INFO *from_cs,
155 size_t nbytes = 0);
156
157size_t bin_to_hex_str(char *to, size_t to_len, const char *from,
158 size_t from_len);
159
160/**
161 Using this class is fraught with peril, and you need to be very careful
162 when doing so. In particular, copy construction and assignment does not
163 do a deep _nor_ a shallow copy; instead, it makes a _reference_ to the
164 original string that will be invalid as soon as that string goes out of scope.
165 (Move constructiong and assignment is safe, though.) In general, it is
166 probably better not to use this class at all if you can avoid it.
167*/
168class String {
169 char *m_ptr;
170 size_t m_length;
172 uint32
173 m_alloced_length; // should be size_t, but kept uint32 for size reasons
175
176 public:
178 : m_ptr(nullptr),
179 m_length(0),
182 m_is_alloced(false) {}
183 explicit String(size_t length_arg)
184 : m_ptr(nullptr),
185 m_length(0),
188 m_is_alloced(false) {
189 (void)real_alloc(length_arg);
190 }
191 String(const char *str, const CHARSET_INFO *cs)
192 : m_ptr(const_cast<char *>(str)),
193 m_length(strlen(str)),
194 m_charset(cs),
196 m_is_alloced(false) {}
197 String(const char *str, size_t len, const CHARSET_INFO *cs)
198 : m_ptr(const_cast<char *>(str)),
199 m_length(len),
200 m_charset(cs),
202 m_is_alloced(false) {}
203 String(char *str, size_t len, const CHARSET_INFO *cs)
204 : m_ptr(str),
205 m_length(len),
206 m_charset(cs),
207 m_alloced_length(static_cast<uint32>(len)),
208 m_is_alloced(false) {}
210 : m_ptr(str.m_ptr),
214 m_is_alloced(false) {}
215 String(String &&str) noexcept
216 : m_ptr(str.m_ptr),
217 m_length(str.m_length),
218 m_charset(str.m_charset),
219 m_alloced_length(str.m_alloced_length),
220 m_is_alloced(str.m_is_alloced) {
221 str.m_is_alloced = false;
222 }
223 static void *operator new(size_t size, MEM_ROOT *mem_root,
224 const std::nothrow_t &arg
225 [[maybe_unused]] = std::nothrow) noexcept {
226 return mem_root->Alloc(size);
227 }
228 static void operator delete(void *ptr_arg, size_t size) {
229 (void)ptr_arg;
230 (void)size;
231 TRASH(ptr_arg, size);
232 }
233
234 static void operator delete(
235 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* never called */
236 }
237
239
240 void set_charset(const CHARSET_INFO *charset_arg) { m_charset = charset_arg; }
241 const CHARSET_INFO *charset() const { return m_charset; }
242 size_t length() const { return m_length; }
243 size_t alloced_length() const { return m_alloced_length; }
244 const char &operator[](size_t i) const { return m_ptr[i]; }
245 char &operator[](size_t i) { return m_ptr[i]; }
246 void length(size_t len) { m_length = len; }
247 bool is_empty() const { return (m_length == 0); }
249 /* Returns a pointer to data, may not include NULL terminating character. */
250 const char *ptr() const { return m_ptr; }
251 char *ptr() { return m_ptr; }
252 char *c_ptr() {
253 assert(!m_is_alloced || !m_ptr || !m_alloced_length ||
254 (m_alloced_length >= (m_length + 1)));
255
256 /*
257 Should be safe, but in case valgrind complains on this line, it means
258 there is a misuse of c_ptr(). Please prefer <ptr(), length()> instead.
259 */
260 if (!m_ptr || m_ptr[m_length]) (void)mem_realloc(m_length);
261 return m_ptr;
262 }
263 char *c_ptr_quick() {
265 return m_ptr;
266 }
267 char *c_ptr_safe() {
269 m_ptr[m_length] = 0;
270 else
271 (void)mem_realloc(m_length);
272 return m_ptr;
273 }
275
278 return lex_cstring;
279 }
280
281 void set(String &str, size_t offset, size_t arg_length) {
282 assert(&str != this);
283 mem_free();
284 m_ptr = str.ptr() + offset;
285 m_length = arg_length;
286 m_is_alloced = false;
287 if (str.m_alloced_length)
288 m_alloced_length = str.m_alloced_length - static_cast<uint32>(offset);
289 else
291 m_charset = str.m_charset;
292 }
293
294 /**
295 Points the internal buffer to the supplied one. The old buffer is freed.
296 @param str Pointer to the new buffer.
297 @param arg_length Length of the new buffer in characters, excluding any
298 null character.
299 @param cs Character set to use for interpreting string data.
300 @note The new buffer will not be null terminated.
301 */
302 void set(char *str, size_t arg_length, const CHARSET_INFO *cs) {
303 mem_free();
304 m_ptr = str;
305 m_length = m_alloced_length = static_cast<uint32>(arg_length);
306 m_is_alloced = false;
307 m_charset = cs;
308 }
309 void set(const char *str, size_t arg_length, const CHARSET_INFO *cs) {
310 mem_free();
311 m_ptr = const_cast<char *>(str);
312 m_length = arg_length;
314 m_is_alloced = false;
315 m_charset = cs;
316 }
317 bool set_ascii(const char *str, size_t arg_length);
318 void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs) {
319 if (!m_is_alloced) {
320 m_ptr = str;
321 m_length = arg_length;
322 m_alloced_length = static_cast<uint32>(arg_length);
323 }
324 m_charset = cs;
325 }
326 bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs);
327 bool set(longlong num, const CHARSET_INFO *cs) {
328 return set_int(num, false, cs);
329 }
330 bool set(ulonglong num, const CHARSET_INFO *cs) {
331 return set_int((longlong)num, true, cs);
332 }
333
334 /**
335 Sets the contents of this string to the string representation of the given
336 double value.
337
338 @param num the double value
339 @param decimals the number of decimals
340 @param cs the character set of the string
341 @return false on success, true on error
342 */
343 bool set_real(double num, uint decimals, const CHARSET_INFO *cs);
344
345 /*
346 PMG 2004.11.12
347 This is a method that works the same as perl's "chop". It simply
348 drops the last byte of a string. This is useful in the case
349 of the federated storage handler where I'm building a unknown
350 number, list of values and fields to be used in a sql insert
351 statement to be run on the remote server, and have a comma after each.
352 When the list is complete, I "chop" off the trailing comma
353
354 ex.
355 String stringobj;
356 stringobj.append("VALUES ('foo', 'fi', 'fo',");
357 stringobj.chop();
358 stringobj.append(")");
359
360 In this case, the value of string was:
361
362 VALUES ('foo', 'fi', 'fo',
363 VALUES ('foo', 'fi', 'fo'
364 VALUES ('foo', 'fi', 'fo')
365
366 This is not safe to call when the string ends in a multi-byte character!
367 */
368 void chop() {
369 m_length--;
370 m_ptr[m_length] = '\0';
371 }
372
373 void mem_claim(bool claim) {
374 if (m_is_alloced) {
375 my_claim(m_ptr, claim);
376 }
377 }
378
379 void mem_free() {
380 if (m_is_alloced) {
381 m_is_alloced = false;
383 my_free(m_ptr);
384 m_ptr = nullptr;
385 m_length = 0; /* Safety */
386 }
387 }
388
389 bool alloc(size_t arg_length) {
390 if (arg_length < m_alloced_length) return false;
391 return real_alloc(arg_length);
392 }
393 bool real_alloc(size_t arg_length); // Empties old string
394 bool mem_realloc(size_t arg_length, bool force_on_heap = false);
395
396 private:
397 size_t next_realloc_exp_size(size_t sz);
398 bool mem_realloc_exp(size_t arg_length);
399
400 public:
401 // Shrink the buffer, but only if it is allocated on the heap.
402 void shrink(size_t arg_length) {
403 if (!is_alloced()) return;
404 if (arg_length < m_alloced_length) {
405 char *new_ptr;
406 if (!(new_ptr = static_cast<char *>(my_realloc(
407 STRING_PSI_MEMORY_KEY, m_ptr, arg_length, MYF(0))))) {
409 real_alloc(arg_length);
410 } else {
411 m_ptr = new_ptr;
412 m_alloced_length = static_cast<uint32>(arg_length);
413 }
414 }
415 }
416 bool is_alloced() const { return m_is_alloced; }
418 if (&s != this) {
419 /*
420 It is forbidden to do assignments like
421 some_string = substring_of_that_string
422 */
423 assert(!s.uses_buffer_owned_by(this));
424 mem_free();
425 m_ptr = s.m_ptr;
426 m_length = s.m_length;
429 m_is_alloced = false;
430 }
431 return *this;
432 }
433 String &operator=(String &&s) noexcept {
434 if (&s != this) {
435 /*
436 It is forbidden to do assignments like
437 some_string = substring_of_that_string
438 */
439 assert(!s.uses_buffer_owned_by(this));
440 mem_free();
441 m_ptr = s.m_ptr;
442 m_length = s.m_length;
443 m_alloced_length = s.m_alloced_length;
444 m_charset = s.m_charset;
445 // This is the primary difference between move and copy.
446 m_is_alloced = s.m_is_alloced;
447 s.m_is_alloced = false;
448 }
449 return *this;
450 }
451 /**
452 Takeover the buffer owned by another string.
453 "this" becomes the owner of the buffer and
454 is further responsible to free it.
455 The string "s" is detached from the buffer (cleared).
456
457 @param s - a String object to steal buffer from.
458 */
459 void takeover(String &s) {
460 assert(this != &s);
461 // Make sure buffers of the two Strings do not overlap
462 assert(!s.uses_buffer_owned_by(this));
463 mem_free();
464 m_ptr = s.m_ptr;
465 m_length = s.m_length;
469 s.m_ptr = nullptr;
470 s.m_alloced_length = 0;
471 s.m_length = 0;
472 s.m_is_alloced = false;
473 }
474
475 bool copy(); // Alloc string if not allocated
476 bool copy(const String &s); // Allocate new string
477 // Allocate new string
478 bool copy(const char *s, size_t arg_length, const CHARSET_INFO *cs);
479 static bool needs_conversion(size_t arg_length, const CHARSET_INFO *cs_from,
480 const CHARSET_INFO *cs_to, size_t *offset);
481 bool needs_conversion(const CHARSET_INFO *cs_to) const {
482 size_t offset;
483 return needs_conversion(length(), charset(), cs_to, &offset);
484 }
485 bool is_valid_string(const CHARSET_INFO *cs_to) const {
486 size_t valid_length;
487 bool length_error;
488 return !validate_string(cs_to, ptr(), length(), &valid_length,
489 &length_error);
490 }
491 static bool needs_conversion_on_storage(size_t arg_length,
492 const CHARSET_INFO *cs_from,
493 const CHARSET_INFO *cs_to);
494 bool copy_aligned(const char *s, size_t arg_length, size_t offset,
495 const CHARSET_INFO *cs);
496 bool set_or_copy_aligned(const char *s, size_t arg_length,
497 const CHARSET_INFO *cs);
498 bool copy(const char *s, size_t arg_length, const CHARSET_INFO *csfrom,
499 const CHARSET_INFO *csto, uint *errors);
500 bool append(const String &s);
501 bool append(std::string_view s) { return append(s.data(), s.size()); }
502 bool append(LEX_STRING *ls) { return append(ls->str, ls->length); }
503 bool append(Simple_cstring str) { return append(str.ptr(), str.length()); }
504 bool append(const char *s, size_t arg_length);
505 bool append(const char *s, size_t arg_length, const CHARSET_INFO *cs);
506 bool append_ulonglong(ulonglong val);
507 bool append_longlong(longlong val);
508 bool append_with_prefill(const char *s, size_t arg_length, size_t full_length,
509 char fill_char);
510 bool append_parenthesized(int64_t nr);
511 /**
512 Search for a substring.
513
514 @param search substring to search for
515 @param offset starting point, bytes from the start of the string
516
517 @return byte offset to the substring from the start of this string
518 @retval -1 if the substring is not found starting from the offset
519 */
520 int strstr(const String &search, size_t offset = 0) const;
521 /**
522 Reverse search for a substring.
523
524 @param search substring to search for
525 @param offset starting point, bytes from the start of the string
526
527 @return byte offset to the substring from the start of this string
528 @retval -1 if the substring is not found starting from the offset
529 */
530 int strrstr(const String &search, size_t offset = 0) const;
531 /**
532 * Returns substring of given characters length, starting at given character
533 * offset. Note that parameter indexes are character indexes and not byte
534 * indexes.
535 */
536 String substr(int offset, int count) const;
537
538 bool replace(size_t offset, size_t arg_length, const char *to, size_t length);
539 bool replace(size_t offset, size_t arg_length, const String &to);
540 bool append(char chr) {
542 m_ptr[m_length++] = chr;
543 } else {
544 if (mem_realloc_exp(m_length + 1)) return true;
545 m_ptr[m_length++] = chr;
546 }
547 return false;
548 }
549 bool fill(size_t max_length, char fill);
550 friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
551 friend int stringcmp(const String *a, const String *b);
552 friend String *copy_if_not_alloced(String *to, String *from,
553 size_t from_length);
554 size_t numchars() const;
555 size_t charpos(size_t i, size_t offset = 0) const;
556
557 bool reserve(size_t space_needed) {
560 }
561 return false;
562 }
563 bool reserve(size_t space_needed, size_t grow_by);
564
565 /* Inline (general) functions used by the protocol functions */
566
567 char *prep_append(size_t arg_length, size_t step_alloc) {
568 size_t new_length = arg_length + m_length;
569 if (new_length > m_alloced_length) {
570 if (mem_realloc(new_length + step_alloc)) return nullptr;
571 }
572 size_t old_length = m_length;
573 m_length += arg_length;
574 return m_ptr + old_length; /* Area to use */
575 }
576
577 bool append(const char *s, size_t arg_length, size_t step_alloc) {
578 size_t new_length = arg_length + m_length;
579 if (new_length > m_alloced_length &&
580 mem_realloc_exp(new_length + step_alloc))
581 return true;
582 memcpy(m_ptr + m_length, s, arg_length);
583 m_length += arg_length;
584 return false;
585 }
586 void print(String *print) const;
587
588 /* Swap two string objects. Efficient way to exchange data without memcpy. */
589 void swap(String &s) noexcept;
590
591 bool uses_buffer_owned_by(const String *s) const {
592 return (s->m_is_alloced && m_ptr >= s->m_ptr &&
593 m_ptr < s->m_ptr + s->m_length);
594 }
595 bool is_ascii() const {
596 if (length() == 0) return true;
597 if (charset()->mbminlen > 1) return false;
598 for (const char *c = ptr(), *end = c + length(); c < end; c++) {
599 if (!my_isascii(*c)) return false;
600 }
601 return true;
602 }
603 /**
604 Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT
605
606 @param root MEM_ROOT to allocate the result
607
608 @return allocated string or NULL
609 */
610 char *dup(MEM_ROOT *root) const;
611};
612
613/**
614 Checks that the source string can be just copied to the destination string
615 without conversion.
616
617 @param arg_length Length of string to copy.
618 @param from_cs Character set to copy from
619 @param to_cs Character set to copy to
620 @param *offset Returns number of unaligned characters.
621
622 @returns true if conversion is required, false otherwise.
623
624 @note
625 to_cs may be nullptr for "no conversion" if the system variable
626 character_set_results is NULL.
627*/
628
629inline bool String::needs_conversion(size_t arg_length,
630 const CHARSET_INFO *from_cs,
631 const CHARSET_INFO *to_cs,
632 size_t *offset) {
633 *offset = 0;
634 if (to_cs == nullptr || (to_cs == &my_charset_bin) || from_cs == to_cs ||
635 my_charset_same(from_cs, to_cs) ||
636 ((from_cs == &my_charset_bin) &&
637 (0 == (*offset = (arg_length % to_cs->mbminlen)))))
638 return false;
639 return true;
640}
641
642static inline void swap(String &a, String &b) noexcept { a.swap(b); }
643
644static inline std::string to_string(const String &str) {
645 return std::string(str.ptr(), str.length());
646}
647
648/**
649 String class wrapper with a preallocated buffer of size buff_sz
650
651 This class allows to replace sequences of:
652 char buff[12345];
653 String str(buff, sizeof(buff));
654 str.length(0);
655 with a simple equivalent declaration:
656 StringBuffer<12345> str;
657*/
658
659template <size_t buff_sz>
660class StringBuffer : public String {
661 char buff[buff_sz];
662
663 public:
665 explicit StringBuffer(const CHARSET_INFO *cs) : String(buff, buff_sz, cs) {
666 length(0);
667 }
668 StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
669 : String(buff, buff_sz, cs) {
670 set(str, length, cs);
671 }
672};
673
674static inline bool check_if_only_end_space(const CHARSET_INFO *cs,
675 const char *str, const char *end) {
676 return str + cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
677}
678
680 LEX_CSTRING cstr = {s.str, s.length};
681 return cstr;
682}
683
685 LEX_STRING str = {const_cast<char *>(s.str), s.length};
686 return str;
687}
688
689inline LEX_CSTRING to_lex_cstring(const char *s) {
690 LEX_CSTRING cstr = {s, s != nullptr ? strlen(s) : 0};
691 return cstr;
692}
693
694bool append_escaped(String *to_str, const String *from_str);
695
696#endif /* SQL_STRING_INCLUDED */
A wrapper class for null-terminated constant strings.
Definition: sql_string.h:75
Simple_cstring(const LEX_STRING arg)
Definition: sql_string.h:96
const char * m_str
Definition: sql_string.h:77
bool eq_bin(const Simple_cstring other) const
Compare to another Simple_cstring.
Definition: sql_string.h:118
const char * ptr() const
Return string buffer.
Definition: sql_string.h:106
bool is_set() const
Check if m_ptr is set.
Definition: sql_string.h:110
void set(const char *str)
Set to a null-terminated string.
Definition: sql_string.h:102
void reset()
Definition: sql_string.h:98
size_t length() const
Return name length.
Definition: sql_string.h:114
void strcpy(char *buff) const
Copy to the given buffer.
Definition: sql_string.h:125
Simple_cstring(const LEX_CSTRING arg)
Definition: sql_string.h:97
Simple_cstring(const char *str_arg, size_t length_arg)
Definition: sql_string.h:93
size_t m_length
Definition: sql_string.h:78
Simple_cstring()
Definition: sql_string.h:92
void set(const char *str_arg, size_t length_arg)
Initialize from a C string whose length is already known.
Definition: sql_string.h:84
String class wrapper with a preallocated buffer of size buff_sz.
Definition: sql_string.h:660
char buff[buff_sz]
Definition: sql_string.h:661
StringBuffer()
Definition: sql_string.h:664
StringBuffer(const CHARSET_INFO *cs)
Definition: sql_string.h:665
StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
Definition: sql_string.h:668
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
LEX_CSTRING lex_cstring() const
Definition: sql_string.h:276
size_t next_realloc_exp_size(size_t sz)
Definition: sql_string.cc:139
friend String * copy_if_not_alloced(String *to, String *from, size_t from_length)
Makes a copy of a String's buffer unless it's already heap-allocated.
Definition: sql_string.cc:734
LEX_STRING lex_string()
Definition: sql_string.h:274
char & operator[](size_t i)
Definition: sql_string.h:245
const CHARSET_INFO * m_charset
Definition: sql_string.h:171
bool append_ulonglong(ulonglong val)
Append an unsigned longlong to the string.
Definition: sql_string.cc:456
bool append_longlong(longlong val)
Append a signed longlong to the string.
Definition: sql_string.cc:466
bool alloc(size_t arg_length)
Definition: sql_string.h:389
bool mem_realloc_exp(size_t arg_length)
This function is used by the various append() and replace() member functions, to ensure that function...
Definition: sql_string.cc:163
bool append(const String &s)
Definition: sql_string.cc:413
bool append(LEX_STRING *ls)
Definition: sql_string.h:502
bool set_ascii(const char *str, size_t arg_length)
Definition: sql_string.cc:391
bool is_empty() const
Definition: sql_string.h:247
uint32 m_alloced_length
Definition: sql_string.h:173
bool append(Simple_cstring str)
Definition: sql_string.h:503
bool set(longlong num, const CHARSET_INFO *cs)
Definition: sql_string.h:327
void takeover(String &s)
Takeover the buffer owned by another string.
Definition: sql_string.h:459
void set(const char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:309
char * prep_append(size_t arg_length, size_t step_alloc)
Definition: sql_string.h:567
String & operator=(String &&s) noexcept
Definition: sql_string.h:433
bool set(ulonglong num, const CHARSET_INFO *cs)
Definition: sql_string.h:330
void mem_free()
Definition: sql_string.h:379
char * c_ptr_safe()
Definition: sql_string.h:267
void chop()
Definition: sql_string.h:368
bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs)
Definition: sql_string.cc:169
bool append_parenthesized(int64_t nr)
Append a parenthesized number to String.
Definition: sql_string.cc:514
char * ptr()
Definition: sql_string.h:251
String(String &&str) noexcept
Definition: sql_string.h:215
char * c_ptr_quick()
Definition: sql_string.h:263
bool needs_conversion(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:481
String(const String &str)
Definition: sql_string.h:209
String(char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:203
String(const char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:197
const CHARSET_INFO * charset() const
Definition: sql_string.h:241
static bool needs_conversion(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to, size_t *offset)
Checks that the source string can be just copied to the destination string without conversion.
Definition: sql_string.h:629
size_t m_length
Definition: sql_string.h:170
String(size_t length_arg)
Definition: sql_string.h:183
bool is_ascii() const
Definition: sql_string.h:595
const char & operator[](size_t i) const
Definition: sql_string.h:244
bool real_alloc(size_t arg_length)
Definition: sql_string.cc:47
int strrstr(const String &search, size_t offset=0) const
Reverse search for a substring.
Definition: sql_string.cc:568
void set_charset(const CHARSET_INFO *charset_arg)
Definition: sql_string.h:240
const char * ptr() const
Definition: sql_string.h:250
String(const char *str, const CHARSET_INFO *cs)
Definition: sql_string.h:191
void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:318
bool reserve(size_t space_needed)
Definition: sql_string.h:557
String & operator=(const String &s)
Definition: sql_string.h:417
char * m_ptr
Definition: sql_string.h:169
size_t charpos(size_t i, size_t offset=0) const
Definition: sql_string.cc:536
String()
Definition: sql_string.h:177
friend int stringcmp(const String *a, const String *b)
Definition: sql_string.cc:705
bool append(char chr)
Definition: sql_string.h:540
bool fill(size_t max_length, char fill)
Definition: sql_string.cc:402
bool mem_realloc(size_t arg_length, bool force_on_heap=false)
Allocates a new buffer on the heap for this String.
Definition: sql_string.cc:98
bool append(std::string_view s)
Definition: sql_string.h:501
int strstr(const String &search, size_t offset=0) const
Search for a substring.
Definition: sql_string.cc:542
char * dup(MEM_ROOT *root) const
Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT.
Definition: sql_string.cc:994
bool append(const char *s, size_t arg_length, size_t step_alloc)
Definition: sql_string.h:577
void swap(String &s) noexcept
Definition: sql_string.cc:985
char * c_ptr()
Definition: sql_string.h:252
static bool needs_conversion_on_storage(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to)
Definition: sql_string.cc:259
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.cc:326
void shrink(size_t arg_length)
Definition: sql_string.h:402
bool replace(size_t offset, size_t arg_length, const char *to, size_t length)
Definition: sql_string.cc:614
friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:682
void print(String *print) const
Definition: sql_string.cc:942
~String()
Definition: sql_string.h:238
void mark_as_const()
Definition: sql_string.h:248
bool append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char)
Definition: sql_string.cc:518
size_t length() const
Definition: sql_string.h:242
size_t alloced_length() const
Definition: sql_string.h:243
size_t numchars() const
Definition: sql_string.cc:532
void length(size_t len)
Definition: sql_string.h:246
bool copy()
Definition: sql_string.cc:192
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:281
bool copy_aligned(const char *s, size_t arg_length, size_t offset, const CHARSET_INFO *cs)
Definition: sql_string.cc:303
bool m_is_alloced
Definition: sql_string.h:174
bool is_alloced() const
Definition: sql_string.h:416
void mem_claim(bool claim)
Definition: sql_string.h:373
bool is_valid_string(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:485
bool set_real(double num, uint decimals, const CHARSET_INFO *cs)
Sets the contents of this string to the string representation of the given double value.
Definition: sql_string.cc:179
String substr(int offset, int count) const
Returns substring of given characters length, starting at given character offset.
Definition: sql_string.cc:591
void set(char *str, size_t arg_length, const CHARSET_INFO *cs)
Points the internal buffer to the supplied one.
Definition: sql_string.h:302
bool uses_buffer_owned_by(const String *s) const
Definition: sql_string.h:591
static MEM_ROOT mem_root
Definition: client_plugin.cc:110
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
A better implementation of the UNIX ctype(3) library.
size_t my_convert(char *to, size_t to_length, const CHARSET_INFO *to_cs, const char *from, size_t from_length, const CHARSET_INFO *from_cs, uint *errors)
Convert a string between two character sets.
Definition: ctype.cc:909
#define my_isascii(c)
Definition: m_ctype.h:686
MYSQL_PLUGIN_IMPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:511
bool my_charset_same(const CHARSET_INFO *cs1, const CHARSET_INFO *cs2)
Definition: m_ctype.h:647
#define MY_SEQ_SPACES
Definition: m_ctype.h:107
Various macros useful for communicating with memory debuggers, such as Valgrind.
void TRASH(void *ptr, size_t length)
Put bad content in memory to be sure it will segfault if dereferenced.
Definition: memory_debugging.h:71
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
long long int longlong
Definition: my_inttypes.h:55
#define MYF(v)
Definition: my_inttypes.h:97
uint32_t uint32
Definition: my_inttypes.h:67
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
Common header for many mysys elements.
static int count
Definition: myisam_ftdump.cc:43
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
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
bool space_needed(const THD *thd, const Json_wrapper *value, bool large, size_t *needed)
How much space is needed for a JSON value when it is stored in the binary format.
Definition: json_binary.cc:1538
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Performance schema instrumentation interface.
Performance schema instrumentation interface.
void my_claim(const void *ptr, bool claim)
Definition: my_malloc.cc:458
void * my_realloc(PSI_memory_key key, void *ptr, size_t size, myf_t flags)
Definition: my_malloc.cc:449
String * copy_if_not_alloced(String *to, String *from, size_t from_length)
Makes a copy of a String's buffer unless it's already heap-allocated.
Definition: sql_string.cc:734
LEX_CSTRING to_lex_cstring(const LEX_STRING &s)
Definition: sql_string.h:679
PSI_memory_key key_memory_String_value
Definition: sql_string.cc:40
static bool check_if_only_end_space(const CHARSET_INFO *cs, const char *str, const char *end)
Definition: sql_string.h:674
size_t well_formed_copy_nchars(const CHARSET_INFO *to_cs, char *to, size_t to_length, const CHARSET_INFO *from_cs, const char *from, size_t from_length, size_t nchars, const char **well_formed_error_pos, const char **cannot_convert_error_pos, const char **from_end_pos)
Definition: sql_string.cc:783
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:642
size_t copy_and_convert(char *to, size_t to_length, const CHARSET_INFO *to_cs, const char *from, size_t from_length, const CHARSET_INFO *from_cs, uint *errors)
Definition: sql_string.h:140
size_t convert_to_printable(char *to, size_t to_len, const char *from, size_t from_len, const CHARSET_INFO *from_cs, size_t nbytes=0)
Convert string to printable ASCII string.
Definition: sql_string.cc:1018
int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:682
LEX_STRING to_lex_string(const LEX_CSTRING &s)
Definition: sql_string.h:684
static std::string to_string(const String &str)
Definition: sql_string.h:644
size_t bin_to_hex_str(char *to, size_t to_len, const char *from, size_t from_len)
Convert a buffer to printable HEX encoded string For eg: ABCDEF1234.
Definition: sql_string.cc:1075
#define STRING_PSI_MEMORY_KEY
Definition: sql_string.h:59
bool validate_string(const CHARSET_INFO *cs, const char *str, size_t length, size_t *valid_length, bool *length_error)
Check if an input byte sequence is a valid character string of a given charset.
Definition: sql_string.cc:1125
bool append_escaped(String *to_str, const String *from_str)
Appends from_str to to_str, escaping certain characters.
Definition: sql_string.cc:1167
Definition: m_ctype.h:385
uint mbminlen
Definition: m_ctype.h:408
Definition: my_sys.h:341
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:145
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
unsigned int uint
Definition: uca9-dump.cc:75