1#ifndef SQL_STRING_INCLUDED
2#define SQL_STRING_INCLUDED
58#define STRING_PSI_MEMORY_KEY key_memory_String_value
60#define STRING_PSI_MEMORY_KEY PSI_NOT_INSTRUMENTED
83 void set(
const char *str_arg,
size_t length_arg) {
85 assert(str_arg || length_arg == 0);
87 assert(!str_arg || str_arg[length_arg] ==
'\0');
93 set(str_arg, length_arg);
136 size_t *valid_length,
bool *length_error);
143 return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
147 const char *from,
size_t from_length,
149 const char **well_formed_error_pos,
150 const char **cannot_convert_error_pos,
151 const char **from_end_pos);
220 str.m_is_alloced =
false;
223 const std::nothrow_t &arg
224 [[maybe_unused]] = std::nothrow)
noexcept {
227 static void operator delete(
void *ptr_arg,
size_t size) {
233 static void operator delete(
234 void *,
MEM_ROOT *,
const std::nothrow_t &)
noexcept {
303 assert(&
str !=
this);
308 if (
str.m_alloced_length)
415 bool mem_realloc(
size_t arg_length,
bool force_on_heap =
false);
427 if (!(new_ptr =
static_cast<char *
>(
my_realloc(
460 assert(!s.uses_buffer_owned_by(
this));
468 s.m_is_alloced =
false;
517 bool copy_aligned(
const char *s,
size_t arg_length,
size_t offset,
525 bool append(
const char *s,
size_t arg_length);
541 int strstr(
const String &search,
size_t offset = 0)
const;
559 bool replace(
size_t offset,
size_t arg_length,
const char *to,
size_t length);
560 bool replace(
size_t offset,
size_t arg_length,
const String &to);
570 bool fill(
size_t max_length,
char fill);
576 size_t charpos(
size_t i,
size_t offset = 0)
const;
589 const size_t new_length = arg_length +
m_length;
591 if (
mem_realloc(new_length + step_alloc))
return nullptr;
595 return m_ptr + old_length;
598 bool append(
const char *s,
size_t arg_length,
size_t step_alloc) {
599 const size_t new_length = arg_length +
m_length;
617 if (
length() == 0)
return true;
618 if (
charset()->mbminlen > 1)
return false;
655 if (to_cs ==
nullptr || (to_cs == &
my_charset_bin) || from_cs == to_cs ||
658 (0 == (*offset = (arg_length % to_cs->
mbminlen)))))
666 return std::string(
str.ptr(),
str.length());
680template <
size_t buff_sz>
696 const char *
str,
const char *
end) {
711 LEX_CSTRING cstr = {s, s !=
nullptr ? strlen(s) : 0};
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
A wrapper class for null-terminated constant strings.
Definition: sql_string.h:74
Simple_cstring(const LEX_STRING arg)
Definition: sql_string.h:95
const char * m_str
Definition: sql_string.h:76
bool eq_bin(const Simple_cstring other) const
Compare to another Simple_cstring.
Definition: sql_string.h:117
const char * ptr() const
Return string buffer.
Definition: sql_string.h:105
bool is_set() const
Check if m_ptr is set.
Definition: sql_string.h:109
void set(const char *str)
Set to a null-terminated string.
Definition: sql_string.h:101
void reset()
Definition: sql_string.h:97
size_t length() const
Return name length.
Definition: sql_string.h:113
void strcpy(char *buff) const
Copy to the given buffer.
Definition: sql_string.h:124
Simple_cstring(const LEX_CSTRING arg)
Definition: sql_string.h:96
Simple_cstring(const char *str_arg, size_t length_arg)
Definition: sql_string.h:92
size_t m_length
Definition: sql_string.h:77
Simple_cstring()
Definition: sql_string.h:91
void set(const char *str_arg, size_t length_arg)
Initialize from a C string whose length is already known.
Definition: sql_string.h:83
String class wrapper with a preallocated buffer of size buff_sz.
Definition: sql_string.h:681
char buff[buff_sz]
Definition: sql_string.h:682
StringBuffer()
Definition: sql_string.h:685
StringBuffer(const CHARSET_INFO *cs)
Definition: sql_string.h:686
StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
Definition: sql_string.h:689
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
LEX_CSTRING lex_cstring() const
Definition: sql_string.h:297
size_t next_realloc_exp_size(size_t sz)
Definition: sql_string.cc:145
friend String * copy_if_not_alloced(String *to, String *from, size_t from_length)
Makes a copy of a String's buffer unless it's already heap-allocated.
Definition: sql_string.cc:741
LEX_STRING lex_string()
Definition: sql_string.h:295
char & operator[](size_t i)
Definition: sql_string.h:244
const CHARSET_INFO * m_charset
Definition: sql_string.h:170
bool append_ulonglong(ulonglong val)
Append an unsigned longlong to the string.
Definition: sql_string.cc:462
bool append_longlong(longlong val)
Append a signed longlong to the string.
Definition: sql_string.cc:472
bool alloc(size_t arg_length)
Definition: sql_string.h:410
bool mem_realloc_exp(size_t arg_length)
This function is used by the various append() and replace() member functions, to ensure that function...
Definition: sql_string.cc:169
bool append(const String &s)
Definition: sql_string.cc:419
bool append(LEX_STRING *ls)
Definition: sql_string.h:523
bool set_ascii(const char *str, size_t arg_length)
Definition: sql_string.cc:397
bool is_empty() const
Definition: sql_string.h:246
uint32 m_alloced_length
Definition: sql_string.h:172
bool append(Simple_cstring str)
Definition: sql_string.h:524
bool set(longlong num, const CHARSET_INFO *cs)
Definition: sql_string.h:348
void takeover(String &s)
Takeover the buffer owned by another string.
Definition: sql_string.h:480
void set(const char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:330
char * prep_append(size_t arg_length, size_t step_alloc)
Definition: sql_string.h:588
String & operator=(String &&s) noexcept
Definition: sql_string.h:454
bool set(ulonglong num, const CHARSET_INFO *cs)
Definition: sql_string.h:351
void mem_free()
Definition: sql_string.h:400
char * c_ptr_safe()
Returns a pointer to a C-style null-terminated string.
Definition: sql_string.h:288
void chop()
Definition: sql_string.h:389
bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs)
Definition: sql_string.cc:175
bool append_parenthesized(int64_t nr)
Append a parenthesized number to String.
Definition: sql_string.cc:520
char * ptr()
Definition: sql_string.h:250
String(String &&str) noexcept
Definition: sql_string.h:214
char * c_ptr_quick()
Definition: sql_string.h:262
bool needs_conversion(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:504
String(const String &str)
Definition: sql_string.h:208
String(char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:202
String(const char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:196
const CHARSET_INFO * charset() const
Definition: sql_string.h:240
static bool needs_conversion(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to, size_t *offset)
Checks that the source string can be just copied to the destination string without conversion.
Definition: sql_string.h:650
size_t m_length
Definition: sql_string.h:169
String(size_t length_arg)
Definition: sql_string.h:182
bool is_ascii() const
Definition: sql_string.h:616
const char & operator[](size_t i) const
Definition: sql_string.h:243
bool real_alloc(size_t arg_length)
Definition: sql_string.cc:53
int strrstr(const String &search, size_t offset=0) const
Reverse search for a substring.
Definition: sql_string.cc:574
void set_charset(const CHARSET_INFO *charset_arg)
Definition: sql_string.h:239
const char * ptr() const
Definition: sql_string.h:249
String(const char *str, const CHARSET_INFO *cs)
Definition: sql_string.h:190
void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:339
bool reserve(size_t space_needed)
Definition: sql_string.h:578
String & operator=(const String &s)
Definition: sql_string.h:438
char * m_ptr
Definition: sql_string.h:168
size_t charpos(size_t i, size_t offset=0) const
Definition: sql_string.cc:542
String()
Definition: sql_string.h:176
friend int stringcmp(const String *a, const String *b)
Definition: sql_string.cc:712
bool append(char chr)
Definition: sql_string.h:561
bool fill(size_t max_length, char fill)
Definition: sql_string.cc:408
bool mem_realloc(size_t arg_length, bool force_on_heap=false)
Allocates a new buffer on the heap for this String.
Definition: sql_string.cc:104
bool append(std::string_view s)
Definition: sql_string.h:522
int strstr(const String &search, size_t offset=0) const
Search for a substring.
Definition: sql_string.cc:548
char * dup(MEM_ROOT *root) const
Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT.
Definition: sql_string.cc:1001
bool append(const char *s, size_t arg_length, size_t step_alloc)
Definition: sql_string.h:598
void swap(String &s) noexcept
Definition: sql_string.cc:992
char * c_ptr()
Definition: sql_string.h:251
static bool needs_conversion_on_storage(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to)
Definition: sql_string.cc:265
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.cc:332
void shrink(size_t arg_length)
Definition: sql_string.h:423
bool replace(size_t offset, size_t arg_length, const char *to, size_t length)
Definition: sql_string.cc:620
friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:689
void print(String *print) const
Definition: sql_string.cc:949
~String()
Definition: sql_string.h:237
void mark_as_const()
Definition: sql_string.h:247
bool append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char)
Definition: sql_string.cc:524
size_t length() const
Definition: sql_string.h:241
size_t alloced_length() const
Definition: sql_string.h:242
size_t numchars() const
Definition: sql_string.cc:538
void length(size_t len)
Definition: sql_string.h:245
bool copy()
Definition: sql_string.cc:198
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:302
bool copy_aligned(const char *s, size_t arg_length, size_t offset, const CHARSET_INFO *cs)
Definition: sql_string.cc:309
bool m_is_alloced
Definition: sql_string.h:173
bool is_alloced() const
Definition: sql_string.h:437
void mem_claim(bool claim)
Definition: sql_string.h:394
bool is_valid_string(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:508
bool set_real(double num, uint decimals, const CHARSET_INFO *cs)
Sets the contents of this string to the string representation of the given double value.
Definition: sql_string.cc:185
String substr(int offset, int count) const
Returns substring of given characters length, starting at given character offset.
Definition: sql_string.cc:597
void set(char *str, size_t arg_length, const CHARSET_INFO *cs)
Points the internal buffer to the supplied one.
Definition: sql_string.h:323
bool uses_buffer_owned_by(const String *s) const
Definition: sql_string.h:612
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
A better implementation of the UNIX ctype(3) library.
static constexpr int MY_SEQ_SPACES
Definition: m_ctype.h:105
MYSQL_STRINGS_EXPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:509
MYSQL_STRINGS_EXPORT size_t my_convert(char *to, size_t to_length, const CHARSET_INFO *to_cs, const char *from, size_t from_length, const CHARSET_INFO *from_cs, unsigned *errors)
Convert a string between two character sets.
Definition: ctype.cc:912
bool my_isascii(char ch)
Definition: m_ctype.h:557
bool my_charset_same(const CHARSET_INFO *cs1, const CHARSET_INFO *cs2)
Definition: m_ctype.h:516
Various macros useful for communicating with memory debuggers, such as Valgrind.
void TRASH(void *ptr, size_t length)
Put bad content in memory to be sure it will segfault if dereferenced.
Definition: memory_debugging.h:71
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
long long int longlong
Definition: my_inttypes.h:55
#define MYF(v)
Definition: my_inttypes.h:97
uint32_t uint32
Definition: my_inttypes.h:67
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
Common header for many mysys elements.
static int count
Definition: myisam_ftdump.cc:45
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
Definition: commit_order_queue.h:34
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
bool space_needed(const Json_wrapper *value, bool large, size_t *needed)
How much space is needed for a JSON value when it is stored in the binary format.
Definition: json_binary.cc:1557
size_t size(const char *const c)
Definition: base64.h:46
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Performance schema instrumentation interface.
Performance schema instrumentation interface.
void my_claim(const void *ptr, bool claim)
Definition: my_malloc.cc:458
void * my_realloc(PSI_memory_key key, void *ptr, size_t size, myf_t flags)
Definition: my_malloc.cc:449
String * copy_if_not_alloced(String *to, String *from, size_t from_length)
Makes a copy of a String's buffer unless it's already heap-allocated.
Definition: sql_string.cc:741
LEX_CSTRING to_lex_cstring(const LEX_STRING &s)
Definition: sql_string.h:700
PSI_memory_key key_memory_String_value
Definition: sql_string.cc:46
static bool check_if_only_end_space(const CHARSET_INFO *cs, const char *str, const char *end)
Definition: sql_string.h:695
size_t well_formed_copy_nchars(const CHARSET_INFO *to_cs, char *to, size_t to_length, const CHARSET_INFO *from_cs, const char *from, size_t from_length, size_t nchars, const char **well_formed_error_pos, const char **cannot_convert_error_pos, const char **from_end_pos)
Definition: sql_string.cc:790
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663
size_t copy_and_convert(char *to, size_t to_length, const CHARSET_INFO *to_cs, const char *from, size_t from_length, const CHARSET_INFO *from_cs, uint *errors)
Definition: sql_string.h:139
size_t convert_to_printable(char *to, size_t to_len, const char *from, size_t from_len, const CHARSET_INFO *from_cs, size_t nbytes=0)
Convert string to printable ASCII string.
Definition: sql_string.cc:1025
int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:689
LEX_STRING to_lex_string(const LEX_CSTRING &s)
Definition: sql_string.h:705
static std::string to_string(const String &str)
Definition: sql_string.h:665
size_t bin_to_hex_str(char *to, size_t to_len, const char *from, size_t from_len)
Convert a buffer to printable HEX encoded string For eg: ABCDEF1234.
Definition: sql_string.cc:1082
#define STRING_PSI_MEMORY_KEY
Definition: sql_string.h:58
bool validate_string(const CHARSET_INFO *cs, const char *str, size_t length, size_t *valid_length, bool *length_error)
Check if an input byte sequence is a valid character string of a given charset.
Definition: sql_string.cc:1132
bool append_escaped(String *to_str, const String *from_str)
Appends from_str to to_str, escaping certain characters.
Definition: sql_string.cc:1174
Definition: m_ctype.h:423
unsigned mbminlen
Definition: m_ctype.h:446
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