MySQL 8.4.2
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 "memory_debugging.h"
43#include "my_alloc.h"
44#include "my_compiler.h"
45#include "my_sys.h"
46
47#include "my_inttypes.h"
49#include "mysql/mysql_lex_string.h" // LEX_STRING
51#include "mysql/service_mysql_alloc.h" // my_free
52#include "mysql/strings/m_ctype.h" // my_convert
53
54struct MEM_ROOT;
55
56#ifdef MYSQL_SERVER
58#define STRING_PSI_MEMORY_KEY key_memory_String_value
59#else
60#define STRING_PSI_MEMORY_KEY PSI_NOT_INSTRUMENTED
61#endif
62
63/**
64 A wrapper class for null-terminated constant strings.
65 Constructors make sure that the position of the '\0' terminating byte
66 in m_str is always in sync with m_length.
67
68 This class must stay as small as possible as we often
69 pass it and its descendants (such as Name_string) into functions
70 using call-by-value evaluation.
71
72 Don't add new members or virtual methods to this class!
73*/
75 private:
76 const char *m_str;
77 size_t m_length;
78
79 public:
80 /**
81 Initialize from a C string whose length is already known.
82 */
83 void set(const char *str_arg, size_t length_arg) {
84 // NULL is allowed only with length==0
85 assert(str_arg || length_arg == 0);
86 // For non-NULL, make sure length_arg is in sync with '\0' terminator.
87 assert(!str_arg || str_arg[length_arg] == '\0');
88 m_str = str_arg;
89 m_length = length_arg;
90 }
91 Simple_cstring() { set(nullptr, 0); }
92 Simple_cstring(const char *str_arg, size_t length_arg) {
93 set(str_arg, length_arg);
94 }
95 Simple_cstring(const LEX_STRING arg) { set(arg.str, arg.length); }
96 Simple_cstring(const LEX_CSTRING arg) { set(arg.str, arg.length); }
97 void reset() { set(nullptr, 0); }
98 /**
99 Set to a null-terminated string.
100 */
101 void set(const char *str) { set(str, str ? strlen(str) : 0); }
102 /**
103 Return string buffer.
104 */
105 const char *ptr() const { return m_str; }
106 /**
107 Check if m_ptr is set.
108 */
109 bool is_set() const { return m_str != nullptr; }
110 /**
111 Return name length.
112 */
113 size_t length() const { return m_length; }
114 /**
115 Compare to another Simple_cstring.
116 */
117 bool eq_bin(const Simple_cstring other) const {
118 return m_length == other.m_length &&
119 memcmp(m_str, other.m_str, m_length) == 0;
120 }
121 /**
122 Copy to the given buffer
123 */
124 void strcpy(char *buff) const {
125 memcpy(buff, m_str, m_length);
126 buff[m_length] = '\0';
127 }
128};
129
130class String;
131
132struct CHARSET_INFO;
133struct IO_CACHE;
134
135bool validate_string(const CHARSET_INFO *cs, const char *str, size_t length,
136 size_t *valid_length, bool *length_error);
137int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
138String *copy_if_not_alloced(String *to, String *from, size_t from_length);
139inline size_t copy_and_convert(char *to, size_t to_length,
140 const CHARSET_INFO *to_cs, const char *from,
141 size_t from_length, const CHARSET_INFO *from_cs,
142 uint *errors) {
143 return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
144}
145size_t well_formed_copy_nchars(const CHARSET_INFO *to_cs, char *to,
146 size_t to_length, const CHARSET_INFO *from_cs,
147 const char *from, size_t from_length,
148 size_t nchars,
149 const char **well_formed_error_pos,
150 const char **cannot_convert_error_pos,
151 const char **from_end_pos);
152size_t convert_to_printable(char *to, size_t to_len, const char *from,
153 size_t from_len, const CHARSET_INFO *from_cs,
154 size_t nbytes = 0);
155
156size_t bin_to_hex_str(char *to, size_t to_len, const char *from,
157 size_t from_len);
158
159/**
160 Using this class is fraught with peril, and you need to be very careful
161 when doing so. In particular, copy construction and assignment does not
162 do a deep _nor_ a shallow copy; instead, it makes a _reference_ to the
163 original string that will be invalid as soon as that string goes out of scope.
164 (Move constructiong and assignment is safe, though.) In general, it is
165 probably better not to use this class at all if you can avoid it.
166*/
167class String {
168 char *m_ptr;
169 size_t m_length;
171 uint32
172 m_alloced_length; // should be size_t, but kept uint32 for size reasons
174
175 public:
177 : m_ptr(nullptr),
178 m_length(0),
181 m_is_alloced(false) {}
182 explicit String(size_t length_arg)
183 : m_ptr(nullptr),
184 m_length(0),
187 m_is_alloced(false) {
188 (void)real_alloc(length_arg);
189 }
190 String(const char *str, const CHARSET_INFO *cs)
191 : m_ptr(const_cast<char *>(str)),
192 m_length(strlen(str)),
193 m_charset(cs),
195 m_is_alloced(false) {}
196 String(const char *str, size_t len, const CHARSET_INFO *cs)
197 : m_ptr(const_cast<char *>(str)),
198 m_length(len),
199 m_charset(cs),
201 m_is_alloced(false) {}
202 String(char *str, size_t len, const CHARSET_INFO *cs)
203 : m_ptr(str),
204 m_length(len),
205 m_charset(cs),
206 m_alloced_length(static_cast<uint32>(len)),
207 m_is_alloced(false) {}
209 : m_ptr(str.m_ptr),
213 m_is_alloced(false) {}
214 String(String &&str) noexcept
215 : m_ptr(str.m_ptr),
216 m_length(str.m_length),
217 m_charset(str.m_charset),
218 m_alloced_length(str.m_alloced_length),
219 m_is_alloced(str.m_is_alloced) {
220 str.m_is_alloced = false;
221 }
222 static void *operator new(size_t size, MEM_ROOT *mem_root,
223 const std::nothrow_t &arg
224 [[maybe_unused]] = std::nothrow) noexcept {
225 return mem_root->Alloc(size);
226 }
227 static void operator delete(void *ptr_arg, size_t size) {
228 (void)ptr_arg;
229 (void)size;
230 TRASH(ptr_arg, size);
231 }
232
233 static void operator delete(
234 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* never called */
235 }
236
238
239 void set_charset(const CHARSET_INFO *charset_arg) { m_charset = charset_arg; }
240 const CHARSET_INFO *charset() const { return m_charset; }
241 size_t length() const { return m_length; }
242 size_t alloced_length() const { return m_alloced_length; }
243 const char &operator[](size_t i) const { return m_ptr[i]; }
244 char &operator[](size_t i) { return m_ptr[i]; }
245 void length(size_t len) { m_length = len; }
246 bool is_empty() const { return (m_length == 0); }
248 /* Returns a pointer to data, may not include NULL terminating character. */
249 const char *ptr() const { return m_ptr; }
250 char *ptr() { return m_ptr; }
251 char *c_ptr() {
252 assert(!m_is_alloced || !m_ptr || !m_alloced_length ||
253 (m_alloced_length >= (m_length + 1)));
254
255 /*
256 Should be safe, but in case valgrind complains on this line, it means
257 there is a misuse of c_ptr(). Please prefer <ptr(), length()> instead.
258 */
259 if (!m_ptr || m_ptr[m_length]) (void)mem_realloc(m_length);
260 return m_ptr;
261 }
262 char *c_ptr_quick() {
264 return m_ptr;
265 }
266
267 /**
268 Returns a pointer to a C-style null-terminated string. The function is
269 "safe" in the sense that the returned string is guaranteed to be
270 null-terminated (as opposed to c_ptr_quick()). However, in terms of memory
271 safety, this function might perform a heap allocation, so using it
272 necessitates manual cleanup.
273
274 @note One potential pitfall when using this function happens when calling
275 c_ptr_safe() on a String object from the parser. In this case the internal
276 m_ptr string is already allocated on a MEM_ROOT and is cleaned up as part of
277 the query lifecycle. This cleanup is performed by simply clearing the
278 MEM_ROOT (freeing its allocated memory) and does not involve destructing the
279 String object. So in the case when the parser works with (ptr, length)-style
280 strings that are placed in String objects and m_length = m_allocated_length
281 we get a heap allocation when calling c_ptr_safe(), even if just to read the
282 contents of the string, and this newly allocated memory is not on the same
283 MEM_ROOT as the original string, and thus we get a memory leak if we do not
284 make sure to free it.
285
286 @return Pointer to null-terminated string.
287 */
288 char *c_ptr_safe() {
290 m_ptr[m_length] = 0;
291 else
292 (void)mem_realloc(m_length);
293 return m_ptr;
294 }
296
299 return lex_cstring;
300 }
301
302 void set(String &str, size_t offset, size_t arg_length) {
303 assert(&str != this);
304 mem_free();
305 m_ptr = str.ptr() + offset;
306 m_length = arg_length;
307 m_is_alloced = false;
308 if (str.m_alloced_length)
309 m_alloced_length = str.m_alloced_length - static_cast<uint32>(offset);
310 else
312 m_charset = str.m_charset;
313 }
314
315 /**
316 Points the internal buffer to the supplied one. The old buffer is freed.
317 @param str Pointer to the new buffer.
318 @param arg_length Length of the new buffer in characters, excluding any
319 null character.
320 @param cs Character set to use for interpreting string data.
321 @note The new buffer will not be null terminated.
322 */
323 void set(char *str, size_t arg_length, const CHARSET_INFO *cs) {
324 mem_free();
325 m_ptr = str;
326 m_length = m_alloced_length = static_cast<uint32>(arg_length);
327 m_is_alloced = false;
328 m_charset = cs;
329 }
330 void set(const char *str, size_t arg_length, const CHARSET_INFO *cs) {
331 mem_free();
332 m_ptr = const_cast<char *>(str);
333 m_length = arg_length;
335 m_is_alloced = false;
336 m_charset = cs;
337 }
338 bool set_ascii(const char *str, size_t arg_length);
339 void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs) {
340 if (!m_is_alloced) {
341 m_ptr = str;
342 m_length = arg_length;
343 m_alloced_length = static_cast<uint32>(arg_length);
344 }
345 m_charset = cs;
346 }
347 bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs);
348 bool set(longlong num, const CHARSET_INFO *cs) {
349 return set_int(num, false, cs);
350 }
351 bool set(ulonglong num, const CHARSET_INFO *cs) {
352 return set_int((longlong)num, true, cs);
353 }
354
355 /**
356 Sets the contents of this string to the string representation of the given
357 double value.
358
359 @param num the double value
360 @param decimals the number of decimals
361 @param cs the character set of the string
362 @return false on success, true on error
363 */
364 bool set_real(double num, uint decimals, const CHARSET_INFO *cs);
365
366 /*
367 PMG 2004.11.12
368 This is a method that works the same as perl's "chop". It simply
369 drops the last byte of a string. This is useful in the case
370 of the federated storage handler where I'm building a unknown
371 number, list of values and fields to be used in a sql insert
372 statement to be run on the remote server, and have a comma after each.
373 When the list is complete, I "chop" off the trailing comma
374
375 ex.
376 String stringobj;
377 stringobj.append("VALUES ('foo', 'fi', 'fo',");
378 stringobj.chop();
379 stringobj.append(")");
380
381 In this case, the value of string was:
382
383 VALUES ('foo', 'fi', 'fo',
384 VALUES ('foo', 'fi', 'fo'
385 VALUES ('foo', 'fi', 'fo')
386
387 This is not safe to call when the string ends in a multi-byte character!
388 */
389 void chop() {
390 m_length--;
391 m_ptr[m_length] = '\0';
392 }
393
394 void mem_claim(bool claim) {
395 if (m_is_alloced) {
396 my_claim(m_ptr, claim);
397 }
398 }
399
400 void mem_free() {
401 if (m_is_alloced) {
402 m_is_alloced = false;
404 my_free(m_ptr);
405 m_ptr = nullptr;
406 m_length = 0; /* Safety */
407 }
408 }
409
410 bool alloc(size_t arg_length) {
411 if (arg_length < m_alloced_length) return false;
412 return real_alloc(arg_length);
413 }
414 bool real_alloc(size_t arg_length); // Empties old string
415 bool mem_realloc(size_t arg_length, bool force_on_heap = false);
416
417 private:
418 size_t next_realloc_exp_size(size_t sz);
419 bool mem_realloc_exp(size_t arg_length);
420
421 public:
422 // Shrink the buffer, but only if it is allocated on the heap.
423 void shrink(size_t arg_length) {
424 if (!is_alloced()) return;
425 if (arg_length < m_alloced_length) {
426 char *new_ptr;
427 if (!(new_ptr = static_cast<char *>(my_realloc(
428 STRING_PSI_MEMORY_KEY, m_ptr, arg_length, MYF(0))))) {
430 real_alloc(arg_length);
431 } else {
432 m_ptr = new_ptr;
433 m_alloced_length = static_cast<uint32>(arg_length);
434 }
435 }
436 }
437 bool is_alloced() const { return m_is_alloced; }
439 if (&s != this) {
440 /*
441 It is forbidden to do assignments like
442 some_string = substring_of_that_string
443 */
444 assert(!s.uses_buffer_owned_by(this));
445 mem_free();
446 m_ptr = s.m_ptr;
447 m_length = s.m_length;
450 m_is_alloced = false;
451 }
452 return *this;
453 }
454 String &operator=(String &&s) noexcept {
455 if (&s != this) {
456 /*
457 It is forbidden to do assignments like
458 some_string = substring_of_that_string
459 */
460 assert(!s.uses_buffer_owned_by(this));
461 mem_free();
462 m_ptr = s.m_ptr;
463 m_length = s.m_length;
464 m_alloced_length = s.m_alloced_length;
465 m_charset = s.m_charset;
466 // This is the primary difference between move and copy.
467 m_is_alloced = s.m_is_alloced;
468 s.m_is_alloced = false;
469 }
470 return *this;
471 }
472 /**
473 Takeover the buffer owned by another string.
474 "this" becomes the owner of the buffer and
475 is further responsible to free it.
476 The string "s" is detached from the buffer (cleared).
477
478 @param s - a String object to steal buffer from.
479 */
480 void takeover(String &s) {
481 assert(this != &s);
482 // Make sure buffers of the two Strings do not overlap
483 assert(!s.uses_buffer_owned_by(this));
484 mem_free();
485 m_ptr = s.m_ptr;
486 m_length = s.m_length;
490 s.m_ptr = nullptr;
491 s.m_alloced_length = 0;
492 s.m_length = 0;
493 s.m_is_alloced = false;
494 }
495
496 bool copy(); // Alloc string if not allocated
497 bool copy(const String &s); // Allocate new string
498 bool copy(const char *s, size_t arg_length, const CHARSET_INFO *cs);
499 bool copy(const char *s, size_t arg_length, const CHARSET_INFO *from_cs,
500 const CHARSET_INFO *to_cs, uint *errors);
501
502 static bool needs_conversion(size_t arg_length, const CHARSET_INFO *cs_from,
503 const CHARSET_INFO *cs_to, size_t *offset);
504 bool needs_conversion(const CHARSET_INFO *cs_to) const {
505 size_t offset;
506 return needs_conversion(length(), charset(), cs_to, &offset);
507 }
508 bool is_valid_string(const CHARSET_INFO *cs_to) const {
509 size_t valid_length;
510 bool length_error;
511 return !validate_string(cs_to, ptr(), length(), &valid_length,
512 &length_error);
513 }
514 static bool needs_conversion_on_storage(size_t arg_length,
515 const CHARSET_INFO *cs_from,
516 const CHARSET_INFO *cs_to);
517 bool copy_aligned(const char *s, size_t arg_length, size_t offset,
518 const CHARSET_INFO *cs);
519 bool set_or_copy_aligned(const char *s, size_t arg_length,
520 const CHARSET_INFO *cs);
521 bool append(const String &s);
522 bool append(std::string_view s) { return append(s.data(), s.size()); }
523 bool append(LEX_STRING *ls) { return append(ls->str, ls->length); }
524 bool append(Simple_cstring str) { return append(str.ptr(), str.length()); }
525 bool append(const char *s, size_t arg_length);
526 bool append(const char *s, size_t arg_length, const CHARSET_INFO *cs);
527 bool append_ulonglong(ulonglong val);
528 bool append_longlong(longlong val);
529 bool append_with_prefill(const char *s, size_t arg_length, size_t full_length,
530 char fill_char);
531 bool append_parenthesized(int64_t nr);
532 /**
533 Search for a substring.
534
535 @param search substring to search for
536 @param offset starting point, bytes from the start of the string
537
538 @return byte offset to the substring from the start of this string
539 @retval -1 if the substring is not found starting from the offset
540 */
541 int strstr(const String &search, size_t offset = 0) const;
542 /**
543 Reverse search for a substring.
544
545 @param search substring to search for
546 @param offset starting point, bytes from the start of the string
547
548 @return byte offset to the substring from the start of this string
549 @retval -1 if the substring is not found starting from the offset
550 */
551 int strrstr(const String &search, size_t offset = 0) const;
552 /**
553 * Returns substring of given characters length, starting at given character
554 * offset. Note that parameter indexes are character indexes and not byte
555 * indexes.
556 */
557 String substr(int offset, int count) const;
558
559 bool replace(size_t offset, size_t arg_length, const char *to, size_t length);
560 bool replace(size_t offset, size_t arg_length, const String &to);
561 bool append(char chr) {
563 m_ptr[m_length++] = chr;
564 } else {
565 if (mem_realloc_exp(m_length + 1)) return true;
566 m_ptr[m_length++] = chr;
567 }
568 return false;
569 }
570 bool fill(size_t max_length, char fill);
571 friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
572 friend int stringcmp(const String *a, const String *b);
573 friend String *copy_if_not_alloced(String *to, String *from,
574 size_t from_length);
575 size_t numchars() const;
576 size_t charpos(size_t i, size_t offset = 0) const;
577
578 bool reserve(size_t space_needed) {
581 }
582 return false;
583 }
584 bool reserve(size_t space_needed, size_t grow_by);
585
586 /* Inline (general) functions used by the protocol functions */
587
588 char *prep_append(size_t arg_length, size_t step_alloc) {
589 const size_t new_length = arg_length + m_length;
590 if (new_length > m_alloced_length) {
591 if (mem_realloc(new_length + step_alloc)) return nullptr;
592 }
593 const size_t old_length = m_length;
594 m_length += arg_length;
595 return m_ptr + old_length; /* Area to use */
596 }
597
598 bool append(const char *s, size_t arg_length, size_t step_alloc) {
599 const size_t new_length = arg_length + m_length;
600 if (new_length > m_alloced_length &&
601 mem_realloc_exp(new_length + step_alloc))
602 return true;
603 memcpy(m_ptr + m_length, s, arg_length);
604 m_length += arg_length;
605 return false;
606 }
607 void print(String *print) const;
608
609 /* Swap two string objects. Efficient way to exchange data without memcpy. */
610 void swap(String &s) noexcept;
611
612 bool uses_buffer_owned_by(const String *s) const {
613 return (s->m_is_alloced && m_ptr >= s->m_ptr &&
614 m_ptr < s->m_ptr + s->m_length);
615 }
616 bool is_ascii() const {
617 if (length() == 0) return true;
618 if (charset()->mbminlen > 1) return false;
619 for (const char *c = ptr(), *end = c + length(); c < end; c++) {
620 if (!my_isascii(*c)) return false;
621 }
622 return true;
623 }
624 /**
625 Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT
626
627 @param root MEM_ROOT to allocate the result
628
629 @return allocated string or NULL
630 */
631 char *dup(MEM_ROOT *root) const;
632};
633
634/**
635 Checks that the source string can be just copied to the destination string
636 without conversion.
637
638 @param arg_length Length of string to copy.
639 @param from_cs Character set to copy from
640 @param to_cs Character set to copy to
641 @param[out] offset Returns number of unaligned characters.
642
643 @returns true if conversion is required, false otherwise.
644
645 @note
646 to_cs may be nullptr for "no conversion" if the system variable
647 character_set_results is NULL.
648*/
649
650inline bool String::needs_conversion(size_t arg_length,
651 const CHARSET_INFO *from_cs,
652 const CHARSET_INFO *to_cs,
653 size_t *offset) {
654 *offset = 0;
655 if (to_cs == nullptr || (to_cs == &my_charset_bin) || from_cs == to_cs ||
656 my_charset_same(from_cs, to_cs) ||
657 ((from_cs == &my_charset_bin) &&
658 (0 == (*offset = (arg_length % to_cs->mbminlen)))))
659 return false;
660 return true;
661}
662
663static inline void swap(String &a, String &b) noexcept { a.swap(b); }
664
665static inline std::string to_string(const String &str) {
666 return std::string(str.ptr(), str.length());
667}
668
669/**
670 String class wrapper with a preallocated buffer of size buff_sz
671
672 This class allows to replace sequences of:
673 char buff[12345];
674 String str(buff, sizeof(buff));
675 str.length(0);
676 with a simple equivalent declaration:
677 StringBuffer<12345> str;
678*/
679
680template <size_t buff_sz>
681class StringBuffer : public String {
682 char buff[buff_sz];
683
684 public:
686 explicit StringBuffer(const CHARSET_INFO *cs) : String(buff, buff_sz, cs) {
687 length(0);
688 }
689 StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
690 : String(buff, buff_sz, cs) {
691 set(str, length, cs);
692 }
693};
694
695static inline bool check_if_only_end_space(const CHARSET_INFO *cs,
696 const char *str, const char *end) {
697 return str + cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
698}
699
701 LEX_CSTRING cstr = {s.str, s.length};
702 return cstr;
703}
704
706 LEX_STRING str = {const_cast<char *>(s.str), s.length};
707 return str;
708}
709
710inline LEX_CSTRING to_lex_cstring(const char *s) {
711 LEX_CSTRING cstr = {s, s != nullptr ? strlen(s) : 0};
712 return cstr;
713}
714
715bool append_escaped(String *to_str, const String *from_str);
716
717#endif /* SQL_STRING_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
A wrapper class for null-terminated constant strings.
Definition: sql_string.h:74
Simple_cstring(const LEX_STRING arg)
Definition: sql_string.h:95
const char * m_str
Definition: sql_string.h:76
bool eq_bin(const Simple_cstring other) const
Compare to another Simple_cstring.
Definition: sql_string.h:117
const char * ptr() const
Return string buffer.
Definition: sql_string.h:105
bool is_set() const
Check if m_ptr is set.
Definition: sql_string.h:109
void set(const char *str)
Set to a null-terminated string.
Definition: sql_string.h:101
void reset()
Definition: sql_string.h:97
size_t length() const
Return name length.
Definition: sql_string.h:113
void strcpy(char *buff) const
Copy to the given buffer.
Definition: sql_string.h:124
Simple_cstring(const LEX_CSTRING arg)
Definition: sql_string.h:96
Simple_cstring(const char *str_arg, size_t length_arg)
Definition: sql_string.h:92
size_t m_length
Definition: sql_string.h:77
Simple_cstring()
Definition: sql_string.h:91
void set(const char *str_arg, size_t length_arg)
Initialize from a C string whose length is already known.
Definition: sql_string.h:83
String class wrapper with a preallocated buffer of size buff_sz.
Definition: sql_string.h:681
char buff[buff_sz]
Definition: sql_string.h:682
StringBuffer()
Definition: sql_string.h:685
StringBuffer(const CHARSET_INFO *cs)
Definition: sql_string.h:686
StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
Definition: sql_string.h:689
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
LEX_CSTRING lex_cstring() const
Definition: sql_string.h:297
size_t next_realloc_exp_size(size_t sz)
Definition: sql_string.cc:145
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:741
LEX_STRING lex_string()
Definition: sql_string.h:295
char & operator[](size_t i)
Definition: sql_string.h:244
const CHARSET_INFO * m_charset
Definition: sql_string.h:170
bool append_ulonglong(ulonglong val)
Append an unsigned longlong to the string.
Definition: sql_string.cc:462
bool append_longlong(longlong val)
Append a signed longlong to the string.
Definition: sql_string.cc:472
bool alloc(size_t arg_length)
Definition: sql_string.h:410
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:169
bool append(const String &s)
Definition: sql_string.cc:419
bool append(LEX_STRING *ls)
Definition: sql_string.h:523
bool set_ascii(const char *str, size_t arg_length)
Definition: sql_string.cc:397
bool is_empty() const
Definition: sql_string.h:246
uint32 m_alloced_length
Definition: sql_string.h:172
bool append(Simple_cstring str)
Definition: sql_string.h:524
bool set(longlong num, const CHARSET_INFO *cs)
Definition: sql_string.h:348
void takeover(String &s)
Takeover the buffer owned by another string.
Definition: sql_string.h:480
void set(const char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:330
char * prep_append(size_t arg_length, size_t step_alloc)
Definition: sql_string.h:588
String & operator=(String &&s) noexcept
Definition: sql_string.h:454
bool set(ulonglong num, const CHARSET_INFO *cs)
Definition: sql_string.h:351
void mem_free()
Definition: sql_string.h:400
char * c_ptr_safe()
Returns a pointer to a C-style null-terminated string.
Definition: sql_string.h:288
void chop()
Definition: sql_string.h:389
bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs)
Definition: sql_string.cc:175
bool append_parenthesized(int64_t nr)
Append a parenthesized number to String.
Definition: sql_string.cc:520
char * ptr()
Definition: sql_string.h:250
String(String &&str) noexcept
Definition: sql_string.h:214
char * c_ptr_quick()
Definition: sql_string.h:262
bool needs_conversion(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:504
String(const String &str)
Definition: sql_string.h:208
String(char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:202
String(const char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:196
const CHARSET_INFO * charset() const
Definition: sql_string.h:240
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:650
size_t m_length
Definition: sql_string.h:169
String(size_t length_arg)
Definition: sql_string.h:182
bool is_ascii() const
Definition: sql_string.h:616
const char & operator[](size_t i) const
Definition: sql_string.h:243
bool real_alloc(size_t arg_length)
Definition: sql_string.cc:53
int strrstr(const String &search, size_t offset=0) const
Reverse search for a substring.
Definition: sql_string.cc:574
void set_charset(const CHARSET_INFO *charset_arg)
Definition: sql_string.h:239
const char * ptr() const
Definition: sql_string.h:249
String(const char *str, const CHARSET_INFO *cs)
Definition: sql_string.h:190
void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:339
bool reserve(size_t space_needed)
Definition: sql_string.h:578
String & operator=(const String &s)
Definition: sql_string.h:438
char * m_ptr
Definition: sql_string.h:168
size_t charpos(size_t i, size_t offset=0) const
Definition: sql_string.cc:542
String()
Definition: sql_string.h:176
friend int stringcmp(const String *a, const String *b)
Definition: sql_string.cc:712
bool append(char chr)
Definition: sql_string.h:561
bool fill(size_t max_length, char fill)
Definition: sql_string.cc:408
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:104
bool append(std::string_view s)
Definition: sql_string.h:522
int strstr(const String &search, size_t offset=0) const
Search for a substring.
Definition: sql_string.cc:548
char * dup(MEM_ROOT *root) const
Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT.
Definition: sql_string.cc:1001
bool append(const char *s, size_t arg_length, size_t step_alloc)
Definition: sql_string.h:598
void swap(String &s) noexcept
Definition: sql_string.cc:992
char * c_ptr()
Definition: sql_string.h:251
static bool needs_conversion_on_storage(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to)
Definition: sql_string.cc:265
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.cc:332
void shrink(size_t arg_length)
Definition: sql_string.h:423
bool replace(size_t offset, size_t arg_length, const char *to, size_t length)
Definition: sql_string.cc:620
friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:689
void print(String *print) const
Definition: sql_string.cc:949
~String()
Definition: sql_string.h:237
void mark_as_const()
Definition: sql_string.h:247
bool append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char)
Definition: sql_string.cc:524
size_t length() const
Definition: sql_string.h:241
size_t alloced_length() const
Definition: sql_string.h:242
size_t numchars() const
Definition: sql_string.cc:538
void length(size_t len)
Definition: sql_string.h:245
bool copy()
Definition: sql_string.cc:198
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:302
bool copy_aligned(const char *s, size_t arg_length, size_t offset, const CHARSET_INFO *cs)
Definition: sql_string.cc:309
bool m_is_alloced
Definition: sql_string.h:173
bool is_alloced() const
Definition: sql_string.h:437
void mem_claim(bool claim)
Definition: sql_string.h:394
bool is_valid_string(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:508
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:185
String substr(int offset, int count) const
Returns substring of given characters length, starting at given character offset.
Definition: sql_string.cc:597
void set(char *str, size_t arg_length, const CHARSET_INFO *cs)
Points the internal buffer to the supplied one.
Definition: sql_string.h:323
bool uses_buffer_owned_by(const String *s) const
Definition: sql_string.h:612
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
A better implementation of the UNIX ctype(3) library.
static constexpr int MY_SEQ_SPACES
Definition: m_ctype.h:105
MYSQL_STRINGS_EXPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:509
MYSQL_STRINGS_EXPORT 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, unsigned *errors)
Convert a string between two character sets.
Definition: ctype.cc:912
bool my_isascii(char ch)
Definition: m_ctype.h:557
bool my_charset_same(const CHARSET_INFO *cs1, const CHARSET_INFO *cs2)
Definition: m_ctype.h:516
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:45
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
bool space_needed(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:1557
size_t size(const char *const c)
Definition: base64.h:46
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:741
LEX_CSTRING to_lex_cstring(const LEX_STRING &s)
Definition: sql_string.h:700
PSI_memory_key key_memory_String_value
Definition: sql_string.cc:46
static bool check_if_only_end_space(const CHARSET_INFO *cs, const char *str, const char *end)
Definition: sql_string.h:695
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:790
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663
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:139
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:1025
int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:689
LEX_STRING to_lex_string(const LEX_CSTRING &s)
Definition: sql_string.h:705
static std::string to_string(const String &str)
Definition: sql_string.h:665
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:1082
#define STRING_PSI_MEMORY_KEY
Definition: sql_string.h:58
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:1132
bool append_escaped(String *to_str, const String *from_str)
Appends from_str to to_str, escaping certain characters.
Definition: sql_string.cc:1174
Definition: m_ctype.h:423
unsigned mbminlen
Definition: m_ctype.h:446
Definition: my_sys.h:346
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