MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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, 2025, 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 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 Copy assignment has been disabled for this reason, use one of the set()
165 functions instead.
166 (Move constructiong and assignment is safe, though.) In general, it is
167 probably better not to use this class at all if you can avoid it.
168*/
169class String {
170 char *m_ptr;
171 size_t m_length;
173 uint32
174 m_alloced_length; // should be size_t, but kept uint32 for size reasons
176
177 public:
179 : m_ptr(nullptr),
180 m_length(0),
183 m_is_alloced(false) {}
184 explicit String(size_t length_arg)
185 : m_ptr(nullptr),
186 m_length(0),
189 m_is_alloced(false) {
190 (void)real_alloc(length_arg);
191 }
192 String(const char *str, const CHARSET_INFO *cs)
193 : m_ptr(const_cast<char *>(str)),
194 m_length(strlen(str)),
195 m_charset(cs),
197 m_is_alloced(false) {}
198 String(const char *str, size_t len, const CHARSET_INFO *cs)
199 : m_ptr(const_cast<char *>(str)),
200 m_length(len),
201 m_charset(cs),
203 m_is_alloced(false) {}
204 String(char *str, size_t len, const CHARSET_INFO *cs)
205 : m_ptr(str),
206 m_length(len),
207 m_charset(cs),
208 m_alloced_length(static_cast<uint32>(len)),
209 m_is_alloced(false) {}
211 : m_ptr(str.m_ptr),
215 m_is_alloced(false) {}
216 String(String &&str) noexcept
217 : m_ptr(str.m_ptr),
218 m_length(str.m_length),
219 m_charset(str.m_charset),
220 m_alloced_length(str.m_alloced_length),
221 m_is_alloced(str.m_is_alloced) {
222 str.m_is_alloced = false;
223 }
224 static void *operator new(size_t size, MEM_ROOT *mem_root,
225 const std::nothrow_t &arg
226 [[maybe_unused]] = std::nothrow) noexcept {
227 return mem_root->Alloc(size);
228 }
229 static void operator delete(void *ptr_arg, size_t size) {
230 (void)ptr_arg;
231 (void)size;
232 TRASH(ptr_arg, size);
233 }
234
235 static void operator delete(
236 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* never called */
237 }
238
240
241 void set_charset(const CHARSET_INFO *charset_arg) { m_charset = charset_arg; }
242 const CHARSET_INFO *charset() const { return m_charset; }
243 size_t length() const { return m_length; }
244 size_t alloced_length() const { return m_alloced_length; }
245 const char &operator[](size_t i) const { return m_ptr[i]; }
246 char &operator[](size_t i) { return m_ptr[i]; }
247 void length(size_t len) { m_length = len; }
248 bool is_empty() const { return (m_length == 0); }
250 /* Returns a pointer to data, may not include NULL terminating character. */
251 const char *ptr() const { return m_ptr; }
252 char *ptr() { return m_ptr; }
253 char *c_ptr() {
254 assert(!m_is_alloced || !m_ptr || !m_alloced_length ||
255 (m_alloced_length >= (m_length + 1)));
256
257 /*
258 Should be safe, but in case valgrind complains on this line, it means
259 there is a misuse of c_ptr(). Please prefer <ptr(), length()> instead.
260 */
261 if (!m_ptr || m_ptr[m_length]) (void)mem_realloc(m_length);
262 return m_ptr;
263 }
264 char *c_ptr_quick() {
266 return m_ptr;
267 }
268
269 /**
270 Returns a pointer to a C-style null-terminated string. The function is
271 "safe" in the sense that the returned string is guaranteed to be
272 null-terminated (as opposed to c_ptr_quick()). However, in terms of memory
273 safety, this function might perform a heap allocation, so using it
274 necessitates manual cleanup.
275
276 @note One potential pitfall when using this function happens when calling
277 c_ptr_safe() on a String object from the parser. In this case the internal
278 m_ptr string is already allocated on a MEM_ROOT and is cleaned up as part of
279 the query lifecycle. This cleanup is performed by simply clearing the
280 MEM_ROOT (freeing its allocated memory) and does not involve destructing the
281 String object. So in the case when the parser works with (ptr, length)-style
282 strings that are placed in String objects and m_length = m_allocated_length
283 we get a heap allocation when calling c_ptr_safe(), even if just to read the
284 contents of the string, and this newly allocated memory is not on the same
285 MEM_ROOT as the original string, and thus we get a memory leak if we do not
286 make sure to free it.
287
288 @return Pointer to null-terminated string.
289 */
290 char *c_ptr_safe() {
292 m_ptr[m_length] = 0;
293 else
294 (void)mem_realloc(m_length);
295 return m_ptr;
296 }
298
301 return lex_cstring;
302 }
303
304 void set(String &str, size_t offset, size_t arg_length) {
305 assert(&str != this);
306 mem_free();
307 m_ptr = str.ptr() + offset;
308 m_length = arg_length;
309 m_is_alloced = false;
310 if (str.m_alloced_length)
311 m_alloced_length = str.m_alloced_length - static_cast<uint32>(offset);
312 else
314 m_charset = str.m_charset;
315 }
316
317 /**
318 Points the internal buffer to the supplied one. The old buffer is freed.
319 @param str Pointer to the new buffer.
320 @param arg_length Length of the new buffer in characters, excluding any
321 null character.
322 @param cs Character set to use for interpreting string data.
323 @note The new buffer will not be null terminated.
324 */
325 void set(char *str, size_t arg_length, const CHARSET_INFO *cs) {
326 mem_free();
327 m_ptr = str;
328 m_length = m_alloced_length = static_cast<uint32>(arg_length);
329 m_is_alloced = false;
330 m_charset = cs;
331 }
332 void set(const char *str, size_t arg_length, const CHARSET_INFO *cs) {
333 mem_free();
334 m_ptr = const_cast<char *>(str);
335 m_length = arg_length;
337 m_is_alloced = false;
338 m_charset = cs;
339 }
340 bool set_ascii(const char *str, size_t arg_length);
341 void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs) {
342 if (!m_is_alloced) {
343 m_ptr = str;
344 m_length = arg_length;
345 m_alloced_length = static_cast<uint32>(arg_length);
346 }
347 m_charset = cs;
348 }
349 bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs);
350 bool set(longlong num, const CHARSET_INFO *cs) {
351 return set_int(num, false, cs);
352 }
353 bool set(ulonglong num, const CHARSET_INFO *cs) {
354 return set_int((longlong)num, true, cs);
355 }
356
357 /**
358 Sets the contents of this string to the string representation of the given
359 double value.
360
361 @param num the double value
362 @param decimals the number of decimals
363 @param cs the character set of the string
364 @return false on success, true on error
365 */
366 bool set_real(double num, uint decimals, const CHARSET_INFO *cs);
367
368 /*
369 PMG 2004.11.12
370 This is a method that works the same as perl's "chop". It simply
371 drops the last byte of a string. This is useful in the case
372 of the federated storage handler where I'm building a unknown
373 number, list of values and fields to be used in a sql insert
374 statement to be run on the remote server, and have a comma after each.
375 When the list is complete, I "chop" off the trailing comma
376
377 ex.
378 String stringobj;
379 stringobj.append("VALUES ('foo', 'fi', 'fo',");
380 stringobj.chop();
381 stringobj.append(")");
382
383 In this case, the value of string was:
384
385 VALUES ('foo', 'fi', 'fo',
386 VALUES ('foo', 'fi', 'fo'
387 VALUES ('foo', 'fi', 'fo')
388
389 This is not safe to call when the string ends in a multi-byte character!
390 */
391 void chop() {
392 m_length--;
393 m_ptr[m_length] = '\0';
394 }
395
396 void mem_claim(bool claim) {
397 if (m_is_alloced) {
398 my_claim(m_ptr, claim);
399 }
400 }
401
402 void mem_free() {
403 if (m_is_alloced) {
404 m_is_alloced = false;
406 my_free(m_ptr);
407 m_ptr = nullptr;
408 m_length = 0; /* Safety */
409 }
410 }
411
412 bool alloc(size_t arg_length) {
413 if (arg_length < m_alloced_length) return false;
414 return real_alloc(arg_length);
415 }
416 bool real_alloc(size_t arg_length); // Empties old string
417 bool mem_realloc(size_t arg_length, bool force_on_heap = false);
418
419 private:
420 size_t next_realloc_exp_size(size_t sz);
421 bool mem_realloc_exp(size_t arg_length);
422
423 public:
424 // Shrink the buffer, but only if it is allocated on the heap.
425 void shrink(size_t arg_length) {
426 if (!is_alloced()) return;
427 if (arg_length < m_alloced_length) {
428 char *new_ptr;
429 if (!(new_ptr = static_cast<char *>(my_realloc(
430 STRING_PSI_MEMORY_KEY, m_ptr, arg_length, MYF(0))))) {
432 real_alloc(arg_length);
433 } else {
434 m_ptr = new_ptr;
435 m_alloced_length = static_cast<uint32>(arg_length);
436 }
437 }
438 }
439 bool is_alloced() const { return m_is_alloced; }
440 String &operator=(const String &s) = delete;
441 String &operator=(String &&s) noexcept {
442 if (&s != this) {
443 /*
444 It is forbidden to do assignments like
445 some_string = substring_of_that_string
446 */
447 assert(!s.uses_buffer_owned_by(this));
448 mem_free();
449 m_ptr = s.m_ptr;
450 m_length = s.m_length;
451 m_alloced_length = s.m_alloced_length;
452 m_charset = s.m_charset;
453 // This is the primary difference between move and copy.
454 m_is_alloced = s.m_is_alloced;
455 s.m_is_alloced = false;
456 }
457 return *this;
458 }
459 /**
460 Takeover the buffer owned by another string.
461 "this" becomes the owner of the buffer and
462 is further responsible to free it.
463 The string "s" is detached from the buffer (cleared).
464
465 @param s - a String object to steal buffer from.
466 */
467 void takeover(String &s) {
468 assert(this != &s);
469 // Make sure buffers of the two Strings do not overlap
470 assert(!s.uses_buffer_owned_by(this));
471 mem_free();
472 m_ptr = s.m_ptr;
473 m_length = s.m_length;
477 s.m_ptr = nullptr;
478 s.m_alloced_length = 0;
479 s.m_length = 0;
480 s.m_is_alloced = false;
481 }
482
483 bool copy(); // Alloc string if not allocated
484 bool copy(const String &s); // Allocate new string
485 bool copy(const char *s, size_t arg_length, const CHARSET_INFO *cs);
486 bool copy(const char *s, size_t arg_length, const CHARSET_INFO *from_cs,
487 const CHARSET_INFO *to_cs, uint *errors);
488
489 static bool needs_conversion(size_t arg_length, const CHARSET_INFO *cs_from,
490 const CHARSET_INFO *cs_to, size_t *offset);
491 bool needs_conversion(const CHARSET_INFO *cs_to) const {
492 size_t offset;
493 return needs_conversion(length(), charset(), cs_to, &offset);
494 }
495 bool is_valid_string(const CHARSET_INFO *cs_to) const {
496 size_t valid_length;
497 bool length_error;
498 return !validate_string(cs_to, ptr(), length(), &valid_length,
499 &length_error);
500 }
501 static bool needs_conversion_on_storage(size_t arg_length,
502 const CHARSET_INFO *cs_from,
503 const CHARSET_INFO *cs_to);
504 bool copy_aligned(const char *s, size_t arg_length, size_t offset,
505 const CHARSET_INFO *cs);
506 bool set_or_copy_aligned(const char *s, size_t arg_length,
507 const CHARSET_INFO *cs);
508 bool append(const String &s);
509 bool append(std::string_view s) { return append(s.data(), s.size()); }
510 bool append(LEX_STRING *ls) { return append(ls->str, ls->length); }
511 bool append(Simple_cstring str) { return append(str.ptr(), str.length()); }
512 bool append(const char *s, size_t arg_length);
513 bool append(const char *s, size_t arg_length, const CHARSET_INFO *cs);
514 bool append_ulonglong(ulonglong val);
515 bool append_longlong(longlong val);
516 bool append_with_prefill(const char *s, size_t arg_length, size_t full_length,
517 char fill_char);
518 bool append_parenthesized(int64_t nr);
519 /**
520 Search for a substring.
521
522 @param search substring to search for
523 @param offset starting point, bytes from the start of the string
524
525 @return byte offset to the substring from the start of this string
526 @retval -1 if the substring is not found starting from the offset
527 */
528 int strstr(const String &search, size_t offset = 0) const;
529 /**
530 Reverse search for a substring.
531
532 @param search substring to search for
533 @param offset starting point, bytes from the start of the string
534
535 @return byte offset to the substring from the start of this string
536 @retval -1 if the substring is not found starting from the offset
537 */
538 int strrstr(const String &search, size_t offset = 0) const;
539 /**
540 * Returns substring of given characters length, starting at given character
541 * offset. Note that parameter indexes are character indexes and not byte
542 * indexes.
543 */
544 String substr(int offset, int count) const;
545
546 bool replace(size_t offset, size_t arg_length, const char *to, size_t length);
547 bool replace(size_t offset, size_t arg_length, const String &to);
548 bool append(char chr) {
550 m_ptr[m_length++] = chr;
551 } else {
552 if (mem_realloc_exp(m_length + 1)) return true;
553 m_ptr[m_length++] = chr;
554 }
555 return false;
556 }
557 bool fill(size_t max_length, char fill);
558 friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
559 friend int stringcmp(const String *a, const String *b);
560 friend String *copy_if_not_alloced(String *to, String *from,
561 size_t from_length);
562 size_t numchars() const;
563 size_t charpos(size_t i, size_t offset = 0) const;
564
565 bool reserve(size_t space_needed) {
568 }
569 return false;
570 }
571 bool reserve(size_t space_needed, size_t grow_by);
572
573 /* Inline (general) functions used by the protocol functions */
574
575 char *prep_append(size_t arg_length, size_t step_alloc) {
576 const size_t new_length = arg_length + m_length;
577 if (new_length > m_alloced_length) {
578 if (mem_realloc(new_length + step_alloc)) return nullptr;
579 }
580 const size_t old_length = m_length;
581 m_length += arg_length;
582 return m_ptr + old_length; /* Area to use */
583 }
584
585 bool append(const char *s, size_t arg_length, size_t step_alloc) {
586 const size_t new_length = arg_length + m_length;
587 if (new_length > m_alloced_length &&
588 mem_realloc_exp(new_length + step_alloc))
589 return true;
590 memcpy(m_ptr + m_length, s, arg_length);
591 m_length += arg_length;
592 return false;
593 }
594 void print(String *print) const;
595
596 /* Swap two string objects. Efficient way to exchange data without memcpy. */
597 void swap(String &s) noexcept;
598
599 bool uses_buffer_owned_by(const String *s) const {
600 return (s->m_is_alloced && m_ptr >= s->m_ptr &&
601 m_ptr < s->m_ptr + s->m_length);
602 }
603 bool is_ascii() const {
604 if (length() == 0) return true;
605 if (charset()->mbminlen > 1) return false;
606 for (const char *c = ptr(), *end = c + length(); c < end; c++) {
607 if (!my_isascii(*c)) return false;
608 }
609 return true;
610 }
611 /**
612 Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT
613
614 @param root MEM_ROOT to allocate the result
615
616 @return allocated string or NULL
617 */
618 char *dup(MEM_ROOT *root) const;
619};
620
621/**
622 Checks that the source string can be just copied to the destination string
623 without conversion.
624
625 @param arg_length Length of string to copy.
626 @param from_cs Character set to copy from
627 @param to_cs Character set to copy to
628 @param[out] offset Returns number of unaligned characters.
629
630 @returns true if conversion is required, false otherwise.
631
632 @note
633 to_cs may be nullptr for "no conversion" if the system variable
634 character_set_results is NULL.
635*/
636
637inline bool String::needs_conversion(size_t arg_length,
638 const CHARSET_INFO *from_cs,
639 const CHARSET_INFO *to_cs,
640 size_t *offset) {
641 *offset = 0;
642 if (to_cs == nullptr || (to_cs == &my_charset_bin) || from_cs == to_cs ||
643 my_charset_same(from_cs, to_cs) ||
644 ((from_cs == &my_charset_bin) &&
645 (0 == (*offset = (arg_length % to_cs->mbminlen)))))
646 return false;
647 return true;
648}
649
650static inline void swap(String &a, String &b) noexcept { a.swap(b); }
651
652inline std::string_view to_string_view(const String &str) {
653 return {str.ptr(), str.length()};
654}
655
656inline std::string to_string(const String &str) {
657 return std::string{to_string_view(str)};
658}
659
660/**
661 String class wrapper with a preallocated buffer of size buff_sz
662
663 This class allows to replace sequences of:
664 char buff[12345];
665 String str(buff, sizeof(buff));
666 str.length(0);
667 with a simple equivalent declaration:
668 StringBuffer<12345> str;
669*/
670
671template <size_t buff_sz>
672class StringBuffer : public String {
673 char buff[buff_sz];
674
675 public:
677 explicit StringBuffer(const CHARSET_INFO *cs) : String(buff, buff_sz, cs) {
678 length(0);
679 }
680 StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
681 : String(buff, buff_sz, cs) {
682 set(str, length, cs);
683 }
684};
685
686static inline bool check_if_only_end_space(const CHARSET_INFO *cs,
687 const char *str, const char *end) {
688 return str + cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
689}
690
692 LEX_CSTRING cstr = {s.str, s.length};
693 return cstr;
694}
695
697 LEX_STRING str = {const_cast<char *>(s.str), s.length};
698 return str;
699}
700
701inline LEX_CSTRING to_lex_cstring(const char *s) {
702 LEX_CSTRING cstr = {s, s != nullptr ? strlen(s) : 0};
703 return cstr;
704}
705
706bool append_escaped(String *to_str, const String *from_str);
707
708#endif /* SQL_STRING_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
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:672
char buff[buff_sz]
Definition: sql_string.h:673
StringBuffer()
Definition: sql_string.h:676
StringBuffer(const CHARSET_INFO *cs)
Definition: sql_string.h:677
StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
Definition: sql_string.h:680
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
LEX_CSTRING lex_cstring() const
Definition: sql_string.h:299
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:297
char & operator[](size_t i)
Definition: sql_string.h:246
const CHARSET_INFO * m_charset
Definition: sql_string.h:172
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:412
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:510
bool set_ascii(const char *str, size_t arg_length)
Definition: sql_string.cc:397
bool is_empty() const
Definition: sql_string.h:248
uint32 m_alloced_length
Definition: sql_string.h:174
bool append(Simple_cstring str)
Definition: sql_string.h:511
bool set(longlong num, const CHARSET_INFO *cs)
Definition: sql_string.h:350
void takeover(String &s)
Takeover the buffer owned by another string.
Definition: sql_string.h:467
void set(const char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:332
char * prep_append(size_t arg_length, size_t step_alloc)
Definition: sql_string.h:575
String & operator=(String &&s) noexcept
Definition: sql_string.h:441
bool set(ulonglong num, const CHARSET_INFO *cs)
Definition: sql_string.h:353
void mem_free()
Definition: sql_string.h:402
char * c_ptr_safe()
Returns a pointer to a C-style null-terminated string.
Definition: sql_string.h:290
void chop()
Definition: sql_string.h:391
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:252
String(String &&str) noexcept
Definition: sql_string.h:216
char * c_ptr_quick()
Definition: sql_string.h:264
bool needs_conversion(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:491
String(const String &str)
Definition: sql_string.h:210
String(char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:204
String(const char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:198
const CHARSET_INFO * charset() const
Definition: sql_string.h:242
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:637
size_t m_length
Definition: sql_string.h:171
String(size_t length_arg)
Definition: sql_string.h:184
bool is_ascii() const
Definition: sql_string.h:603
const char & operator[](size_t i) const
Definition: sql_string.h:245
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:241
const char * ptr() const
Definition: sql_string.h:251
String(const char *str, const CHARSET_INFO *cs)
Definition: sql_string.h:192
void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:341
bool reserve(size_t space_needed)
Definition: sql_string.h:565
char * m_ptr
Definition: sql_string.h:170
size_t charpos(size_t i, size_t offset=0) const
Definition: sql_string.cc:542
String()
Definition: sql_string.h:178
friend int stringcmp(const String *a, const String *b)
Definition: sql_string.cc:712
bool append(char chr)
Definition: sql_string.h:548
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:509
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:585
void swap(String &s) noexcept
Definition: sql_string.cc:992
char * c_ptr()
Definition: sql_string.h:253
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:425
String & operator=(const String &s)=delete
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:239
void mark_as_const()
Definition: sql_string.h:249
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:243
size_t alloced_length() const
Definition: sql_string.h:244
size_t numchars() const
Definition: sql_string.cc:538
void length(size_t len)
Definition: sql_string.h:247
bool copy()
Definition: sql_string.cc:198
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:304
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:175
bool is_alloced() const
Definition: sql_string.h:439
void mem_claim(bool claim)
Definition: sql_string.h:396
bool is_valid_string(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:495
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:325
bool uses_buffer_owned_by(const String *s) const
Definition: sql_string.h:599
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:499
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:932
bool my_isascii(char ch)
Definition: m_ctype.h:555
bool my_charset_same(const CHARSET_INFO *cs1, const CHARSET_INFO *cs2)
Definition: m_ctype.h:514
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:1084
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
const char * cstr(const char *str)
Definition: generic.h:36
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:1532
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:691
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:686
std::string_view to_string_view(const String &str)
Definition: sql_string.h:652
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:650
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:696
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
std::string to_string(const String &str)
Definition: sql_string.h:656
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:1131
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:421
unsigned mbminlen
Definition: m_ctype.h:444
Definition: my_sys.h:337
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