MySQL  8.0.18
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, 2019, 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  Simple_cstring(const LEX_CSTRING arg) { set(arg.str, arg.length); }
93  void reset() { set(NULL, 0); }
94  /**
95  Set to a null-terminated string.
96  */
97  void set(const char *str) { set(str, str ? strlen(str) : 0); }
98  /**
99  Return string buffer.
100  */
101  const char *ptr() const { return m_str; }
102  /**
103  Check if m_ptr is set.
104  */
105  bool is_set() const { return m_str != NULL; }
106  /**
107  Return name length.
108  */
109  size_t length() const { return m_length; }
110  /**
111  Compare to another Simple_cstring.
112  */
113  bool eq_bin(const Simple_cstring other) const {
114  return m_length == other.m_length &&
115  memcmp(m_str, other.m_str, m_length) == 0;
116  }
117  /**
118  Copy to the given buffer
119  */
120  void strcpy(char *buff) const {
121  memcpy(buff, m_str, m_length);
122  buff[m_length] = '\0';
123  }
124 };
125 
126 class String;
127 
128 struct CHARSET_INFO;
129 struct IO_CACHE;
130 
131 int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
132 String *copy_if_not_alloced(String *to, String *from, size_t from_length);
133 inline size_t copy_and_convert(char *to, size_t to_length,
134  const CHARSET_INFO *to_cs, const char *from,
135  size_t from_length, const CHARSET_INFO *from_cs,
136  uint *errors) {
137  return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
138 }
139 size_t well_formed_copy_nchars(const CHARSET_INFO *to_cs, char *to,
140  size_t to_length, const CHARSET_INFO *from_cs,
141  const char *from, size_t from_length,
142  size_t nchars,
143  const char **well_formed_error_pos,
144  const char **cannot_convert_error_pos,
145  const char **from_end_pos);
146 size_t convert_to_printable(char *to, size_t to_len, const char *from,
147  size_t from_len, const CHARSET_INFO *from_cs,
148  size_t nbytes = 0);
149 
150 size_t bin_to_hex_str(char *to, size_t to_len, const char *from,
151  size_t from_len);
152 
153 /**
154  Using this class is fraught with peril, and you need to be very careful
155  when doing so. In particular, copy construction and assignment does not
156  do a deep _nor_ a shallow copy; instead, it makes a _reference_ to the
157  original string that will be invalid as soon as that string goes out of scope.
158  (Move constructiong and assignment is safe, though.) In general, it is
159  probably better not to use this class at all if you can avoid it.
160 */
161 class String {
162  char *m_ptr;
163  size_t m_length;
165  uint32
166  m_alloced_length; // should be size_t, but kept uint32 for size reasons
168 
169  public:
171  : m_ptr(NULL),
172  m_length(0),
174  m_alloced_length(0),
175  m_is_alloced(false) {}
176  explicit String(size_t length_arg)
177  : m_ptr(NULL),
178  m_length(0),
180  m_alloced_length(0),
182  (void)real_alloc(length_arg);
183  }
184  String(const char *str, const CHARSET_INFO *cs)
185  : m_ptr(const_cast<char *>(str)),
186  m_length(strlen(str)),
187  m_charset(cs),
188  m_alloced_length(0),
189  m_is_alloced(false) {}
190  String(const char *str, size_t len, const CHARSET_INFO *cs)
191  : m_ptr(const_cast<char *>(str)),
192  m_length(len),
193  m_charset(cs),
194  m_alloced_length(0),
195  m_is_alloced(false) {}
196  String(char *str, size_t len, const CHARSET_INFO *cs)
197  : m_ptr(str),
198  m_length(len),
199  m_charset(cs),
200  m_alloced_length(static_cast<uint32>(len)),
201  m_is_alloced(false) {}
202  String(const String &str)
203  : m_ptr(str.m_ptr),
204  m_length(str.m_length),
205  m_charset(str.m_charset),
206  m_alloced_length(static_cast<uint32>(str.m_alloced_length)),
207  m_is_alloced(false) {}
208  String(String &&str) noexcept
209  : m_ptr(str.m_ptr),
210  m_length(str.m_length),
211  m_charset(str.m_charset),
212  m_alloced_length(str.m_alloced_length),
213  m_is_alloced(str.m_is_alloced) {
214  str.m_is_alloced = false;
215  }
216  static void *operator new(size_t size, MEM_ROOT *mem_root,
217  const std::nothrow_t &arg MY_ATTRIBUTE((unused)) =
218  std::nothrow) noexcept {
219  return mem_root->Alloc(size);
220  }
221  static void operator delete(void *ptr_arg, size_t size) {
222  (void)ptr_arg;
223  (void)size;
224  TRASH(ptr_arg, size);
225  }
226 
227  static void operator delete(
228  void *, MEM_ROOT *, const std::nothrow_t &)noexcept { /* never called */
229  }
230 
231  ~String() { mem_free(); }
232 
233  void set_charset(const CHARSET_INFO *charset_arg) { m_charset = charset_arg; }
234  const CHARSET_INFO *charset() const { return m_charset; }
235  size_t length() const { return m_length; }
236  size_t alloced_length() const { return m_alloced_length; }
237  const char &operator[](size_t i) const { return m_ptr[i]; }
238  char &operator[](size_t i) { return m_ptr[i]; }
239  void length(size_t len) { m_length = len; }
240  bool is_empty() const { return (m_length == 0); }
242  /* Returns a pointer to data, may not include NULL terminating character. */
243  const char *ptr() const { return m_ptr; }
244  char *ptr() { return m_ptr; }
245  char *c_ptr() {
247  (m_alloced_length >= (m_length + 1)));
248 
249  /*
250  Should be safe, but in case valgrind complains on this line, it means
251  there is a misuse of c_ptr(). Please prefer <ptr(), length()> instead.
252  */
253  if (!m_ptr || m_ptr[m_length]) (void)mem_realloc(m_length);
254  return m_ptr;
255  }
256  char *c_ptr_quick() {
258  return m_ptr;
259  }
260  char *c_ptr_safe() {
262  m_ptr[m_length] = 0;
263  else
264  (void)mem_realloc(m_length);
265  return m_ptr;
266  }
267  LEX_STRING lex_string() { return {m_ptr, length()}; }
268 
271  return lex_cstring;
272  }
273 
274  void set(String &str, size_t offset, size_t arg_length) {
275  DBUG_ASSERT(&str != this);
276  mem_free();
277  m_ptr = str.ptr() + offset;
278  m_length = arg_length;
279  m_is_alloced = false;
280  if (str.m_alloced_length)
281  m_alloced_length = str.m_alloced_length - static_cast<uint32>(offset);
282  else
283  m_alloced_length = 0;
284  m_charset = str.m_charset;
285  }
286 
287  /**
288  Points the internal buffer to the supplied one. The old buffer is freed.
289  @param str Pointer to the new buffer.
290  @param arg_length Length of the new buffer in characters, excluding any
291  null character.
292  @param cs Character set to use for interpreting string data.
293  @note The new buffer will not be null terminated.
294  */
295  void set(char *str, size_t arg_length, const CHARSET_INFO *cs) {
296  mem_free();
297  m_ptr = str;
298  m_length = m_alloced_length = static_cast<uint32>(arg_length);
299  m_is_alloced = false;
300  m_charset = cs;
301  }
302  void set(const char *str, size_t arg_length, const CHARSET_INFO *cs) {
303  mem_free();
304  m_ptr = const_cast<char *>(str);
305  m_length = arg_length;
306  m_alloced_length = 0;
307  m_is_alloced = false;
308  m_charset = cs;
309  }
310  bool set_ascii(const char *str, size_t arg_length);
311  void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs) {
312  if (!m_is_alloced) {
313  m_ptr = str;
314  m_length = arg_length;
315  m_alloced_length = static_cast<uint32>(arg_length);
316  }
317  m_charset = cs;
318  }
319  bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs);
320  bool set(longlong num, const CHARSET_INFO *cs) {
321  return set_int(num, false, cs);
322  }
323  bool set(ulonglong num, const CHARSET_INFO *cs) {
324  return set_int((longlong)num, true, cs);
325  }
326 
327  /**
328  Sets the contents of this string to the string representation of the given
329  double value.
330 
331  @param num the double value
332  @param decimals the number of decimals
333  @param cs the character set of the string
334  @return false on success, true on error
335  */
336  bool set_real(double num, uint decimals, const CHARSET_INFO *cs);
337 
338  /*
339  PMG 2004.11.12
340  This is a method that works the same as perl's "chop". It simply
341  drops the last byte of a string. This is useful in the case
342  of the federated storage handler where I'm building a unknown
343  number, list of values and fields to be used in a sql insert
344  statement to be run on the remote server, and have a comma after each.
345  When the list is complete, I "chop" off the trailing comma
346 
347  ex.
348  String stringobj;
349  stringobj.append("VALUES ('foo', 'fi', 'fo',");
350  stringobj.chop();
351  stringobj.append(")");
352 
353  In this case, the value of string was:
354 
355  VALUES ('foo', 'fi', 'fo',
356  VALUES ('foo', 'fi', 'fo'
357  VALUES ('foo', 'fi', 'fo')
358 
359  This is not safe to call when the string ends in a multi-byte character!
360  */
361  void chop() {
362  m_length--;
363  m_ptr[m_length] = '\0';
364  }
365 
366  void mem_claim() {
367  if (m_is_alloced) {
368  my_claim(m_ptr);
369  }
370  }
371 
372  void mem_free() {
373  if (m_is_alloced) {
374  m_is_alloced = false;
375  m_alloced_length = 0;
376  my_free(m_ptr);
377  m_ptr = NULL;
378  m_length = 0; /* Safety */
379  }
380  }
381 
382  bool alloc(size_t arg_length) {
383  if (arg_length < m_alloced_length) return false;
384  return real_alloc(arg_length);
385  }
386  bool real_alloc(size_t arg_length); // Empties old string
387  bool mem_realloc(size_t arg_length, bool force_on_heap = false);
388 
389  private:
390  size_t next_realloc_exp_size(size_t sz);
391  bool mem_realloc_exp(size_t arg_length);
392 
393  public:
394  // Shrink the buffer, but only if it is allocated on the heap.
395  void shrink(size_t arg_length) {
396  if (!is_alloced()) return;
397  if (arg_length < m_alloced_length) {
398  char *new_ptr;
399  if (!(new_ptr = static_cast<char *>(my_realloc(
400  STRING_PSI_MEMORY_KEY, m_ptr, arg_length, MYF(0))))) {
401  m_alloced_length = 0;
402  real_alloc(arg_length);
403  } else {
404  m_ptr = new_ptr;
405  m_alloced_length = static_cast<uint32>(arg_length);
406  }
407  }
408  }
409  bool is_alloced() const { return m_is_alloced; }
410  String &operator=(const String &s) {
411  if (&s != this) {
412  /*
413  It is forbidden to do assignments like
414  some_string = substring_of_that_string
415  */
417  mem_free();
418  m_ptr = s.m_ptr;
419  m_length = s.m_length;
421  m_charset = s.m_charset;
422  m_is_alloced = false;
423  }
424  return *this;
425  }
426  String &operator=(String &&s) noexcept {
427  if (&s != this) {
428  /*
429  It is forbidden to do assignments like
430  some_string = substring_of_that_string
431  */
432  DBUG_ASSERT(!s.uses_buffer_owned_by(this));
433  mem_free();
434  m_ptr = s.m_ptr;
435  m_length = s.m_length;
436  m_alloced_length = s.m_alloced_length;
437  m_charset = s.m_charset;
438  // This is the primary difference between move and copy.
439  m_is_alloced = s.m_is_alloced;
440  s.m_is_alloced = false;
441  }
442  return *this;
443  }
444  /**
445  Takeover the buffer owned by another string.
446  "this" becames the owner of the buffer and
447  is further responsible to free it.
448  The string "s" is detouched from the buffer (cleared).
449 
450  @param s - a String object to steal buffer from.
451  */
452  void takeover(String &s) {
453  DBUG_ASSERT(this != &s);
454  // Make sure buffers of the two Strings do not overlap
456  mem_free();
457  m_ptr = s.m_ptr;
458  m_length = s.m_length;
461  m_charset = s.m_charset;
462  s.m_ptr = NULL;
463  s.m_alloced_length = 0;
464  s.m_length = 0;
465  s.m_is_alloced = false;
466  }
467 
468  bool copy(); // Alloc string if not alloced
469  bool copy(const String &s); // Allocate new string
470  // Allocate new string
471  bool copy(const char *s, size_t arg_length, const CHARSET_INFO *cs);
472  static bool needs_conversion(size_t arg_length, const CHARSET_INFO *cs_from,
473  const CHARSET_INFO *cs_to, size_t *offset);
474  static bool needs_conversion_on_storage(size_t arg_length,
475  const CHARSET_INFO *cs_from,
476  const CHARSET_INFO *cs_to);
477  bool copy_aligned(const char *s, size_t arg_length, size_t offset,
478  const CHARSET_INFO *cs);
479  bool set_or_copy_aligned(const char *s, size_t arg_length,
480  const CHARSET_INFO *cs);
481  bool copy(const char *s, size_t arg_length, const CHARSET_INFO *csfrom,
482  const CHARSET_INFO *csto, uint *errors);
483  bool append(const String &s);
484  bool append(const char *s);
485  bool append(LEX_STRING *ls) { return append(ls->str, ls->length); }
486  bool append(Simple_cstring str) { return append(str.ptr(), str.length()); }
487  bool append(const char *s, size_t arg_length);
488  bool append(const char *s, size_t arg_length, const CHARSET_INFO *cs);
489  bool append_ulonglong(ulonglong val);
490  bool append_longlong(longlong val);
491  bool append(IO_CACHE *file, size_t arg_length);
492  bool append_with_prefill(const char *s, size_t arg_length, size_t full_length,
493  char fill_char);
494  bool append_parenthesized(long nr, int radix = 10);
495  /**
496  Search for a substring.
497 
498  @param search substring to search for
499  @param offset starting point, bytes from the start of the string
500 
501  @return byte offset to the substring from the start of this string
502  @retval -1 if the substring is not found starting from the offset
503  */
504  int strstr(const String &search, size_t offset = 0) const;
505  /**
506  Reverse search for a substring.
507 
508  @param search substring to search for
509  @param offset starting point, bytes from the start of the string
510 
511  @return byte offset to the substring from the start of this string
512  @retval -1 if the substring is not found starting from the offset
513  */
514  int strrstr(const String &search, size_t offset = 0) const;
515  /**
516  * Returns substring of given characters lenght, starting at given character
517  * offset. Note that parameter indexes are character indexes and not byte
518  * indexes.
519  */
520  String substr(int offset, int count) const;
521 
522  bool replace(size_t offset, size_t arg_length, const char *to, size_t length);
523  bool replace(size_t offset, size_t arg_length, const String &to);
524  bool append(char chr) {
525  if (m_length < m_alloced_length) {
526  m_ptr[m_length++] = chr;
527  } else {
528  if (mem_realloc_exp(m_length + 1)) return 1;
529  m_ptr[m_length++] = chr;
530  }
531  return 0;
532  }
533  bool fill(size_t max_length, char fill);
534  void strip_sp();
535  friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs);
536  friend int stringcmp(const String *a, const String *b);
537  friend String *copy_if_not_alloced(String *to, String *from,
538  size_t from_length);
539  size_t numchars() const;
540  size_t charpos(size_t i, size_t offset = 0) const;
541 
542  bool reserve(size_t space_needed) {
544  }
545  bool reserve(size_t space_needed, size_t grow_by);
546 
547  /* Inline (general) functions used by the protocol functions */
548 
549  char *prep_append(size_t arg_length, size_t step_alloc) {
550  size_t new_length = arg_length + m_length;
551  if (new_length > m_alloced_length) {
552  if (mem_realloc(new_length + step_alloc)) return NULL;
553  }
554  size_t old_length = m_length;
555  m_length += arg_length;
556  return m_ptr + old_length; /* Area to use */
557  }
558 
559  bool append(const char *s, size_t arg_length, size_t step_alloc) {
560  size_t new_length = arg_length + m_length;
561  if (new_length > m_alloced_length &&
562  mem_realloc_exp(new_length + step_alloc))
563  return true;
564  memcpy(m_ptr + m_length, s, arg_length);
565  m_length += arg_length;
566  return false;
567  }
568  void print(String *print) const;
569 
570  /* Swap two string objects. Efficient way to exchange data without memcpy. */
571  void swap(String &s) noexcept;
572 
573  bool uses_buffer_owned_by(const String *s) const {
574  return (s->m_is_alloced && m_ptr >= s->m_ptr &&
575  m_ptr < s->m_ptr + s->m_length);
576  }
577  bool is_ascii() const {
578  if (length() == 0) return true;
579  if (charset()->mbminlen > 1) return false;
580  for (const char *c = ptr(), *end = c + length(); c < end; c++) {
581  if (!my_isascii(*c)) return false;
582  }
583  return true;
584  }
585  /**
586  Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT
587 
588  @param root MEM_ROOT to allocate the result
589 
590  @return allocated string or NULL
591  */
592  char *dup(MEM_ROOT *root) const;
593 };
594 
595 static inline void swap(String &a, String &b) noexcept { a.swap(b); }
596 
597 static inline std::string to_string(const String &str) {
598  return std::string(str.ptr(), str.length());
599 }
600 
601 /**
602  String class wrapper with a preallocated buffer of size buff_sz
603 
604  This class allows to replace sequences of:
605  char buff[12345];
606  String str(buff, sizeof(buff));
607  str.length(0);
608  with a simple equivalent declaration:
609  StringBuffer<12345> str;
610 */
611 
612 template <size_t buff_sz>
613 class StringBuffer : public String {
614  char buff[buff_sz];
615 
616  public:
617  StringBuffer() : String(buff, buff_sz, &my_charset_bin) { length(0); }
618  explicit StringBuffer(const CHARSET_INFO *cs) : String(buff, buff_sz, cs) {
619  length(0);
620  }
621  StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
622  : String(buff, buff_sz, cs) {
623  set(str, length, cs);
624  }
625 };
626 
627 static inline bool check_if_only_end_space(const CHARSET_INFO *cs,
628  const char *str, const char *end) {
629  return str + cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
630 }
631 
633  LEX_CSTRING cstr = {s.str, s.length};
634  return cstr;
635 }
636 
638  LEX_STRING str = {const_cast<char *>(s.str), s.length};
639  return str;
640 }
641 
642 inline LEX_CSTRING to_lex_cstring(const char *s) {
643  LEX_CSTRING cstr = {s, s != NULL ? strlen(s) : 0};
644  return cstr;
645 }
646 
647 bool validate_string(const CHARSET_INFO *cs, const char *str, size_t length,
648  size_t *valid_length, bool *length_error);
649 
650 bool append_escaped(String *to_str, const String *from_str);
651 
652 #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:382
unsigned long long int ulonglong
Definition: my_inttypes.h:55
void shrink(size_t arg_length)
Definition: sql_string.h:395
void chop()
Definition: sql_string.h:361
char * str
Definition: mysql_lex_string.h:35
Definition: mysql_lex_string.h:34
ssize_t count
Definition: memcached.c:386
char * c_ptr_safe()
Definition: sql_string.h:260
bool reserve(size_t space_needed)
Definition: sql_string.h:542
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:763
bool space_needed(const THD *thd, const Json_wrapper *value, bool large, size_t *needed)
How much space is needed for a JSON value when it is stored in the binary format. ...
Definition: json_binary.cc:1538
Some integer typedefs for easier portability.
void takeover(String &s)
Takeover the buffer owned by another string.
Definition: sql_string.h:452
String(char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:196
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:443
char & operator[](size_t i)
Definition: sql_string.h:238
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:792
void swap(String &s) noexcept
Definition: sql_string.cc:1044
bool copy()
Definition: sql_string.cc:189
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:266
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:597
Definition: mysql_lex_string.h:39
String & operator=(String &&s) noexcept
Definition: sql_string.h:426
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:614
int strrstr(const String &search, size_t offset=0) const
Reverse search for a substring.
Definition: sql_string.cc:624
char * c_ptr()
Definition: sql_string.h:245
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
bool set_ascii(const char *str, size_t arg_length)
Definition: sql_string.cc:421
size_t numchars() const
Definition: sql_string.cc:588
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
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:113
bool m_is_alloced
Definition: sql_string.h:167
String(const char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:190
Performance schema instrumentation interface.
String(const char *str, const CHARSET_INFO *cs)
Definition: sql_string.h:184
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:1134
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:566
static bool needs_conversion_on_storage(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to)
Definition: sql_string.cc:289
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
Simple_cstring(const LEX_CSTRING arg)
Definition: sql_string.h:92
bool is_alloced() const
Definition: sql_string.h:409
String class wrapper with a preallocated buffer of size buff_sz.
Definition: sql_string.h:613
String(const String &str)
Definition: sql_string.h:202
char * ptr()
Definition: sql_string.h:244
#define STRING_PSI_MEMORY_KEY
Definition: sql_string.h:54
bool append(const String &s)
Definition: sql_string.cc:447
bool append(Simple_cstring str)
Definition: sql_string.h:486
StringBuffer(const CHARSET_INFO *cs)
Definition: sql_string.h:618
void reset()
Definition: sql_string.h:93
bool is_ascii() const
Definition: sql_string.h:577
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
size_t length
Definition: mysql_lex_string.h:41
void set_charset(const CHARSET_INFO *charset_arg)
Definition: sql_string.h:233
Definition: my_sys.h:360
#define MY_SEQ_SPACES
Definition: m_ctype.h:106
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:841
Header for compiler-dependent features.
#define my_isascii(c)
Definition: m_ctype.h:653
void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:311
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
const char & operator[](size_t i) const
Definition: sql_string.h:237
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:176
long long int longlong
Definition: my_inttypes.h:54
#define MYF(v)
Definition: my_inttypes.h:114
bool uses_buffer_owned_by(const String *s) const
Definition: sql_string.h:573
size_t(* scan)(const CHARSET_INFO *, const char *b, const char *e, int sq)
Definition: m_ctype.h:351
uint32_t uint32
Definition: my_inttypes.h:66
LEX_CSTRING lex_cstring() const
Definition: sql_string.h:269
A wrapper class for null-terminated constant strings.
Definition: sql_string.h:70
Definition: m_ctype.h:359
bool append(char chr)
Definition: sql_string.h:524
size_t m_length
Definition: sql_string.h:73
size_t charpos(size_t i, size_t offset=0) const
Definition: sql_string.cc:592
int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:740
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:101
String()
Definition: sql_string.h:170
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:120
bool replace(size_t offset, size_t arg_length, const char *to, size_t length)
Definition: sql_string.cc:670
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
uint32 m_alloced_length
Definition: sql_string.h:166
bool append(LEX_STRING *ls)
Definition: sql_string.h:485
String(String &&str) noexcept
Definition: sql_string.h:208
void print(String *print) const
Definition: sql_string.cc:1001
StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
Definition: sql_string.h:621
char * dup(MEM_ROOT *root) const
Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT.
Definition: sql_string.cc:1053
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:239
bool append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char)
Definition: sql_string.cc:574
Performance schema instrumentation interface.
~String()
Definition: sql_string.h:231
static bool check_if_only_end_space(const CHARSET_INFO *cs, const char *str, const char *end)
Definition: sql_string.h:627
const char * m_str
Definition: sql_string.h:72
void mem_free()
Definition: sql_string.h:372
char * m_ptr
Definition: sql_string.h:162
#define NULL
Definition: types.h:55
friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:740
char * prep_append(size_t arg_length, size_t step_alloc)
Definition: sql_string.h:549
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:595
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:134
const CHARSET_INFO * charset() const
Definition: sql_string.h:234
A better implementation of the UNIX ctype(3) library.
void mem_claim()
Definition: sql_string.h:366
MYSQL_PLUGIN_IMPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:510
const CHARSET_INFO * m_charset
Definition: sql_string.h:164
LEX_STRING lex_string()
Definition: sql_string.h:267
void mark_as_const()
Definition: sql_string.h:241
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:496
bool is_empty() const
Definition: sql_string.h:240
const char * ptr() const
Definition: sql_string.h:243
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:506
size_t length() const
Return name length.
Definition: sql_string.h:109
bool is_set() const
Check if m_ptr is set.
Definition: sql_string.h:105
MY_CHARSET_HANDLER * cset
Definition: m_ctype.h:391
LEX_CSTRING to_lex_cstring(const LEX_STRING &s)
Definition: sql_string.h:632
size_t m_length
Definition: sql_string.h:163
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:792
String substr(int offset, int count) const
Returns substring of given characters lenght, starting at given character offset. ...
Definition: sql_string.cc:647
bool copy_aligned(const char *s, size_t arg_length, size_t offset, const CHARSET_INFO *cs)
Definition: sql_string.cc:333
bool append(const char *s, size_t arg_length, size_t step_alloc)
Definition: sql_string.h:559
bool fill(size_t max_length, char fill)
Definition: sql_string.cc:432
LEX_STRING to_lex_string(const LEX_CSTRING &s)
Definition: sql_string.h:637
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:133
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:235
String & operator=(const String &s)
Definition: sql_string.h:410
char * c_ptr_quick()
Definition: sql_string.h:256
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:1077
size_t alloced_length() const
Definition: sql_string.h:236
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.cc:356
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:176
StringBuffer()
Definition: sql_string.h:617
int strstr(const String &search, size_t offset=0) const
Search for a substring.
Definition: sql_string.cc:598