1 #ifndef SQL_STRING_INCLUDED
2 #define SQL_STRING_INCLUDED
46 void set(
const char *str_arg,
size_t length_arg)
49 DBUG_ASSERT(str_arg || length_arg == 0);
51 DBUG_ASSERT(!str_arg || str_arg[length_arg] ==
'\0');
62 set(str_arg, length_arg);
66 set(arg.str, arg.length);
75 void set(
const char *str)
77 set(str, str ? strlen(str) : 0);
82 const char *
ptr()
const {
return m_str; }
86 bool is_set()
const {
return m_str != NULL; }
90 size_t length()
const {
return m_length; }
96 return m_length == other.m_length &&
97 memcmp(m_str, other.m_str, m_length) == 0;
104 memcpy(buff, m_str, m_length);
105 buff[m_length]=
'\0';
117 inline uint32 copy_and_convert(
char *
to, uint32 to_length,
119 const char *from, uint32 from_length,
122 return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
124 uint32 well_formed_copy_nchars(
const CHARSET_INFO *to_cs,
125 char *
to, uint to_length,
127 const char *from, uint from_length,
129 const char **well_formed_error_pos,
130 const char **cannot_convert_error_pos,
131 const char **from_end_pos);
132 size_t my_copy_with_hex_escaping(
const CHARSET_INFO *cs,
133 char *dst,
size_t dstlen,
134 const char *src,
size_t srclen);
135 uint convert_to_printable(
char *
to,
size_t to_len,
136 const char *from,
size_t from_len,
142 uint32 str_length,Alloced_length;
148 Ptr=0; str_length=Alloced_length=0; alloced=0;
149 str_charset= &my_charset_bin;
153 alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
154 str_charset= &my_charset_bin;
158 Ptr=(
char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
163 Ptr=(
char*) str; str_length=len; Alloced_length=0; alloced=0;
168 Ptr=(
char*) str; Alloced_length=str_length=len; alloced=0;
173 Ptr=str.Ptr ; str_length=str.str_length ;
174 Alloced_length=str.Alloced_length; alloced=0;
175 str_charset=str.str_charset;
177 static void *
operator new(
size_t size,
MEM_ROOT *mem_root)
throw ()
178 {
return (
void*) alloc_root(mem_root, (uint)
size); }
179 static void operator delete(
void *ptr_arg,
size_t size)
183 TRASH(ptr_arg,
size);
185 static void operator delete(
void *,
MEM_ROOT *)
189 inline void set_charset(
const CHARSET_INFO *charset_arg)
190 { str_charset= charset_arg; }
191 inline const CHARSET_INFO *charset()
const {
return str_charset; }
192 inline uint32 length()
const {
return str_length;}
193 inline uint32 alloced_length()
const {
return Alloced_length;}
194 inline char& operator [] (uint32
i)
const {
return Ptr[
i]; }
195 inline void length(uint32 len) { str_length=len ; }
196 inline bool is_empty()
const {
return (str_length == 0); }
197 inline void mark_as_const() { Alloced_length= 0;}
198 inline const char *ptr()
const {
return Ptr; }
201 DBUG_ASSERT(!alloced || !
Ptr || !Alloced_length ||
202 (Alloced_length >= (str_length + 1)));
204 if (!
Ptr ||
Ptr[str_length])
208 inline char *c_ptr_quick()
210 if (
Ptr && str_length < Alloced_length)
214 inline char *c_ptr_safe()
216 if (
Ptr && str_length < Alloced_length)
224 LEX_STRING lex_string = { (
char*) ptr(), length() };
230 DBUG_ASSERT(&str !=
this);
232 Ptr=(
char*) str.ptr()+
offset; str_length=arg_length; alloced=0;
233 if (str.Alloced_length)
234 Alloced_length=str.Alloced_length-
offset;
237 str_charset=str.str_charset;
252 Ptr=(
char*) str; str_length=Alloced_length=arg_length ; alloced=0;
255 inline void set(
const char *str,uint32 arg_length,
const CHARSET_INFO *cs)
258 Ptr=(
char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
261 bool set_ascii(
const char *str, uint32 arg_length);
262 inline void set_quick(
char *str,uint32 arg_length,
const CHARSET_INFO *cs)
266 Ptr=(
char*) str; str_length=Alloced_length=arg_length;
270 bool set_int(longlong num,
bool unsigned_flag,
const CHARSET_INFO *cs);
272 {
return set_int(num,
false, cs); }
274 {
return set_int((longlong)num,
true, cs); }
275 bool set_real(
double num,uint decimals,
const CHARSET_INFO *cs);
302 Ptr[str_length]=
'\0';
303 DBUG_ASSERT(strlen(
Ptr) == str_length);
317 inline bool alloc(uint32 arg_length)
319 if (arg_length < Alloced_length)
321 return real_alloc(arg_length);
323 bool real_alloc(uint32 arg_length);
324 bool realloc(uint32 arg_length);
327 inline void shrink(uint32 arg_length)
331 if (arg_length < Alloced_length)
334 if (!(new_ptr=(
char*) my_realloc(
Ptr,arg_length,MYF(0))))
337 real_alloc(arg_length);
342 Alloced_length=arg_length;
346 bool is_alloced()
const {
return alloced; }
355 DBUG_ASSERT(!s.uses_buffer_owned_by(
this));
357 Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
358 str_charset=s.str_charset;
373 DBUG_ASSERT(
this != &s);
375 DBUG_ASSERT(!s.uses_buffer_owned_by(
this));
378 str_length= s.str_length;
379 Alloced_length= s.Alloced_length;
381 str_charset= s.str_charset;
389 bool copy(
const String &s);
391 bool copy(
const char *s,uint32 arg_length,
const CHARSET_INFO *cs);
392 static bool needs_conversion(uint32 arg_length,
395 bool copy_aligned(
const char *s, uint32 arg_length, uint32
offset,
397 bool set_or_copy_aligned(
const char *s, uint32 arg_length,
399 bool copy(
const char*s,uint32 arg_length,
const CHARSET_INFO *csfrom,
401 bool append(
const String &s);
402 bool append(
const char *s);
405 return append(ls->str, (uint32) ls->length);
409 return append(str.
ptr(),
static_cast<uint
>(str.
length()));
411 bool append(
const char *s, uint32 arg_length);
412 bool append(
const char *s, uint32 arg_length,
const CHARSET_INFO *cs);
413 bool append_ulonglong(ulonglong val);
415 bool append_with_prefill(
const char *s, uint32 arg_length,
416 uint32 full_length,
char fill_char);
420 bool replace(uint32
offset,uint32 arg_length,
const char *
to,uint32 length);
422 inline bool append(
char chr)
424 if (str_length < Alloced_length)
426 Ptr[str_length++]=chr;
432 Ptr[str_length++]=chr;
436 bool fill(uint32 max_length,
char fill);
441 uint32 numchars()
const;
442 int charpos(
int i,uint32
offset=0);
444 int reserve(uint32 space_needed)
446 return realloc(str_length + space_needed);
448 int reserve(uint32 space_needed, uint32 grow_by);
454 void q_append(
const char c)
456 Ptr[str_length++] = c;
458 void q_append(
const uint32
n)
460 int4store(
Ptr + str_length, n);
463 void q_append(
double d)
465 float8store(
Ptr + str_length, d);
468 void q_append(
double *d)
470 float8store(
Ptr + str_length, *d);
473 void q_append(
const char *data, uint32 data_len)
475 memcpy(
Ptr + str_length, data, data_len);
476 str_length += data_len;
479 void write_at_position(
int position, uint32 value)
481 int4store(
Ptr + position,value);
484 void qs_append(
const char *str, uint32 len);
485 void qs_append(
double d);
486 void qs_append(
double *d);
487 inline void qs_append(
const char c)
492 void qs_append(
int i);
493 void qs_append(uint
i);
497 inline char *prep_append(uint32 arg_length, uint32 step_alloc)
499 uint32 new_length= arg_length + str_length;
500 if (new_length > Alloced_length)
502 if (
realloc(new_length + step_alloc))
505 uint32 old_length= str_length;
506 str_length+= arg_length;
507 return Ptr+ old_length;
510 inline bool append(
const char *s, uint32 arg_length, uint32 step_alloc)
512 uint32 new_length= arg_length + str_length;
513 if (new_length > Alloced_length &&
realloc(new_length + step_alloc))
515 memcpy(
Ptr+str_length, s, arg_length);
516 str_length+= arg_length;
519 void print(
String *print);
524 inline bool uses_buffer_owned_by(
const String *s)
const
528 bool is_ascii()
const
532 if (charset()->mbminlen > 1)
534 for (
const char *c= ptr(), *end= c + length(); c < end; c++)
550 if (str_length > 0 &&
Ptr[str_length - 1] == 0)
551 return static_cast<char *
>(memdup_root(root,
Ptr, str_length));
553 char *
ret=
static_cast<char*
>(alloc_root(root, str_length + 1));
556 memcpy(ret,
Ptr, str_length);
575 template<
size_t buff_sz>
587 :
String(buff, buff_sz, cs)
589 set(str, length, cs);
594 static inline bool check_if_only_end_space(
const CHARSET_INFO *cs,
char *str,
597 return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;