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