1#ifndef SQL_STRING_INCLUDED
2#define SQL_STRING_INCLUDED
59#define STRING_PSI_MEMORY_KEY key_memory_String_value
61#define STRING_PSI_MEMORY_KEY PSI_NOT_INSTRUMENTED
84 void set(
const char *str_arg,
size_t length_arg) {
86 assert(str_arg || length_arg == 0);
88 assert(!str_arg || str_arg[length_arg] ==
'\0');
94 set(str_arg, length_arg);
137 size_t *valid_length,
bool *length_error);
144 return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
148 const char *from,
size_t from_length,
150 const char **well_formed_error_pos,
151 const char **cannot_convert_error_pos,
152 const char **from_end_pos);
221 str.m_is_alloced =
false;
224 const std::nothrow_t &arg
225 [[maybe_unused]] = std::nothrow)
noexcept {
228 static void operator delete(
void *ptr_arg,
size_t size) {
231 TRASH(ptr_arg, size);
234 static void operator delete(
235 void *,
MEM_ROOT *,
const std::nothrow_t &)
noexcept {
282 assert(&
str !=
this);
287 if (
str.m_alloced_length)
394 bool mem_realloc(
size_t arg_length,
bool force_on_heap =
false);
406 if (!(new_ptr =
static_cast<char *
>(
my_realloc(
439 assert(!s.uses_buffer_owned_by(
this));
447 s.m_is_alloced =
false;
494 bool copy_aligned(
const char *s,
size_t arg_length,
size_t offset,
504 bool append(
const char *s,
size_t arg_length);
520 int strstr(
const String &search,
size_t offset = 0)
const;
538 bool replace(
size_t offset,
size_t arg_length,
const char *to,
size_t length);
539 bool replace(
size_t offset,
size_t arg_length,
const String &to);
549 bool fill(
size_t max_length,
char fill);
555 size_t charpos(
size_t i,
size_t offset = 0)
const;
568 size_t new_length = arg_length +
m_length;
570 if (
mem_realloc(new_length + step_alloc))
return nullptr;
574 return m_ptr + old_length;
577 bool append(
const char *s,
size_t arg_length,
size_t step_alloc) {
578 size_t new_length = arg_length +
m_length;
596 if (
length() == 0)
return true;
597 if (
charset()->mbminlen > 1)
return false;
634 if (to_cs ==
nullptr || (to_cs == &
my_charset_bin) || from_cs == to_cs ||
637 (0 == (*offset = (arg_length % to_cs->
mbminlen)))))
645 return std::string(
str.ptr(),
str.length());
659template <
size_t buff_sz>
675 const char *
str,
const char *
end) {
690 LEX_CSTRING cstr = {s, s !=
nullptr ? strlen(s) : 0};
A wrapper class for null-terminated constant strings.
Definition: sql_string.h:75
Simple_cstring(const LEX_STRING arg)
Definition: sql_string.h:96
const char * m_str
Definition: sql_string.h:77
bool eq_bin(const Simple_cstring other) const
Compare to another Simple_cstring.
Definition: sql_string.h:118
const char * ptr() const
Return string buffer.
Definition: sql_string.h:106
bool is_set() const
Check if m_ptr is set.
Definition: sql_string.h:110
void set(const char *str)
Set to a null-terminated string.
Definition: sql_string.h:102
void reset()
Definition: sql_string.h:98
size_t length() const
Return name length.
Definition: sql_string.h:114
void strcpy(char *buff) const
Copy to the given buffer.
Definition: sql_string.h:125
Simple_cstring(const LEX_CSTRING arg)
Definition: sql_string.h:97
Simple_cstring(const char *str_arg, size_t length_arg)
Definition: sql_string.h:93
size_t m_length
Definition: sql_string.h:78
Simple_cstring()
Definition: sql_string.h:92
void set(const char *str_arg, size_t length_arg)
Initialize from a C string whose length is already known.
Definition: sql_string.h:84
String class wrapper with a preallocated buffer of size buff_sz.
Definition: sql_string.h:660
char buff[buff_sz]
Definition: sql_string.h:661
StringBuffer()
Definition: sql_string.h:664
StringBuffer(const CHARSET_INFO *cs)
Definition: sql_string.h:665
StringBuffer(const char *str, size_t length, const CHARSET_INFO *cs)
Definition: sql_string.h:668
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
LEX_CSTRING lex_cstring() const
Definition: sql_string.h:276
size_t next_realloc_exp_size(size_t sz)
Definition: sql_string.cc:139
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:734
LEX_STRING lex_string()
Definition: sql_string.h:274
char & operator[](size_t i)
Definition: sql_string.h:245
const CHARSET_INFO * m_charset
Definition: sql_string.h:171
bool append_ulonglong(ulonglong val)
Append an unsigned longlong to the string.
Definition: sql_string.cc:456
bool append_longlong(longlong val)
Append a signed longlong to the string.
Definition: sql_string.cc:466
bool alloc(size_t arg_length)
Definition: sql_string.h:389
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:163
bool append(const String &s)
Definition: sql_string.cc:413
bool append(LEX_STRING *ls)
Definition: sql_string.h:502
bool set_ascii(const char *str, size_t arg_length)
Definition: sql_string.cc:391
bool is_empty() const
Definition: sql_string.h:247
uint32 m_alloced_length
Definition: sql_string.h:173
bool append(Simple_cstring str)
Definition: sql_string.h:503
bool set(longlong num, const CHARSET_INFO *cs)
Definition: sql_string.h:327
void takeover(String &s)
Takeover the buffer owned by another string.
Definition: sql_string.h:459
void set(const char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:309
char * prep_append(size_t arg_length, size_t step_alloc)
Definition: sql_string.h:567
String & operator=(String &&s) noexcept
Definition: sql_string.h:433
bool set(ulonglong num, const CHARSET_INFO *cs)
Definition: sql_string.h:330
void mem_free()
Definition: sql_string.h:379
char * c_ptr_safe()
Definition: sql_string.h:267
void chop()
Definition: sql_string.h:368
bool set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs)
Definition: sql_string.cc:169
bool append_parenthesized(int64_t nr)
Append a parenthesized number to String.
Definition: sql_string.cc:514
char * ptr()
Definition: sql_string.h:251
String(String &&str) noexcept
Definition: sql_string.h:215
char * c_ptr_quick()
Definition: sql_string.h:263
bool needs_conversion(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:481
String(const String &str)
Definition: sql_string.h:209
String(char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:203
String(const char *str, size_t len, const CHARSET_INFO *cs)
Definition: sql_string.h:197
const CHARSET_INFO * charset() const
Definition: sql_string.h:241
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:629
size_t m_length
Definition: sql_string.h:170
String(size_t length_arg)
Definition: sql_string.h:183
bool is_ascii() const
Definition: sql_string.h:595
const char & operator[](size_t i) const
Definition: sql_string.h:244
bool real_alloc(size_t arg_length)
Definition: sql_string.cc:47
int strrstr(const String &search, size_t offset=0) const
Reverse search for a substring.
Definition: sql_string.cc:568
void set_charset(const CHARSET_INFO *charset_arg)
Definition: sql_string.h:240
const char * ptr() const
Definition: sql_string.h:250
String(const char *str, const CHARSET_INFO *cs)
Definition: sql_string.h:191
void set_quick(char *str, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.h:318
bool reserve(size_t space_needed)
Definition: sql_string.h:557
String & operator=(const String &s)
Definition: sql_string.h:417
char * m_ptr
Definition: sql_string.h:169
size_t charpos(size_t i, size_t offset=0) const
Definition: sql_string.cc:536
String()
Definition: sql_string.h:177
friend int stringcmp(const String *a, const String *b)
Definition: sql_string.cc:705
bool append(char chr)
Definition: sql_string.h:540
bool fill(size_t max_length, char fill)
Definition: sql_string.cc:402
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:98
bool append(std::string_view s)
Definition: sql_string.h:501
int strstr(const String &search, size_t offset=0) const
Search for a substring.
Definition: sql_string.cc:542
char * dup(MEM_ROOT *root) const
Make a zero-terminated copy of our value,allocated in the specified MEM_ROOT.
Definition: sql_string.cc:994
bool append(const char *s, size_t arg_length, size_t step_alloc)
Definition: sql_string.h:577
void swap(String &s) noexcept
Definition: sql_string.cc:985
char * c_ptr()
Definition: sql_string.h:252
static bool needs_conversion_on_storage(size_t arg_length, const CHARSET_INFO *cs_from, const CHARSET_INFO *cs_to)
Definition: sql_string.cc:259
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO *cs)
Definition: sql_string.cc:326
void shrink(size_t arg_length)
Definition: sql_string.h:402
bool replace(size_t offset, size_t arg_length, const char *to, size_t length)
Definition: sql_string.cc:614
friend int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:682
void print(String *print) const
Definition: sql_string.cc:942
~String()
Definition: sql_string.h:238
void mark_as_const()
Definition: sql_string.h:248
bool append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char)
Definition: sql_string.cc:518
size_t length() const
Definition: sql_string.h:242
size_t alloced_length() const
Definition: sql_string.h:243
size_t numchars() const
Definition: sql_string.cc:532
void length(size_t len)
Definition: sql_string.h:246
bool copy()
Definition: sql_string.cc:192
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:281
bool copy_aligned(const char *s, size_t arg_length, size_t offset, const CHARSET_INFO *cs)
Definition: sql_string.cc:303
bool m_is_alloced
Definition: sql_string.h:174
bool is_alloced() const
Definition: sql_string.h:416
void mem_claim(bool claim)
Definition: sql_string.h:373
bool is_valid_string(const CHARSET_INFO *cs_to) const
Definition: sql_string.h:485
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:179
String substr(int offset, int count) const
Returns substring of given characters length, starting at given character offset.
Definition: sql_string.cc:591
void set(char *str, size_t arg_length, const CHARSET_INFO *cs)
Points the internal buffer to the supplied one.
Definition: sql_string.h:302
bool uses_buffer_owned_by(const String *s) const
Definition: sql_string.h:591
static MEM_ROOT mem_root
Definition: client_plugin.cc:110
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
A better implementation of the UNIX ctype(3) library.
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:909
#define my_isascii(c)
Definition: m_ctype.h:686
MYSQL_PLUGIN_IMPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:511
bool my_charset_same(const CHARSET_INFO *cs1, const CHARSET_INFO *cs2)
Definition: m_ctype.h:647
#define MY_SEQ_SPACES
Definition: m_ctype.h:107
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:43
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
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 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
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:734
LEX_CSTRING to_lex_cstring(const LEX_STRING &s)
Definition: sql_string.h:679
PSI_memory_key key_memory_String_value
Definition: sql_string.cc:40
static bool check_if_only_end_space(const CHARSET_INFO *cs, const char *str, const char *end)
Definition: sql_string.h:674
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:783
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:642
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:140
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:1018
int sortcmp(const String *a, const String *b, const CHARSET_INFO *cs)
Definition: sql_string.cc:682
LEX_STRING to_lex_string(const LEX_CSTRING &s)
Definition: sql_string.h:684
static std::string to_string(const String &str)
Definition: sql_string.h:644
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:1075
#define STRING_PSI_MEMORY_KEY
Definition: sql_string.h:59
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:1125
bool append_escaped(String *to_str, const String *from_str)
Appends from_str to to_str, escaping certain characters.
Definition: sql_string.cc:1167
Definition: m_ctype.h:385
uint mbminlen
Definition: m_ctype.h:408
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
unsigned int uint
Definition: uca9-dump.cc:75