1 #ifndef SQL_STRING_INCLUDED
2 #define SQL_STRING_INCLUDED
3
4 /*
5 Copyright (c) 2000, 2013, Oracle and/or its affiliates.
6 Copyright (c) 2008, 2020, MariaDB Corporation.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
20
21 /* This file is originally from the mysql distribution. Coded by monty */
22
23 #ifdef USE_PRAGMA_INTERFACE
24 #pragma interface /* gcc class implementation */
25 #endif
26
27 #include "m_ctype.h" /* my_charset_bin */
28 #include <my_sys.h> /* alloc_root, my_free, my_realloc */
29 #include "m_string.h" /* TRASH */
30 #include "sql_list.h"
31
32 class String;
33 typedef struct st_io_cache IO_CACHE;
34 typedef struct st_mem_root MEM_ROOT;
35
36 #include "pack.h"
37 int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
38 String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
copy_and_convert(char * to,size_t to_length,CHARSET_INFO * to_cs,const char * from,size_t from_length,CHARSET_INFO * from_cs,uint * errors)39 inline uint32 copy_and_convert(char *to, size_t to_length,
40 CHARSET_INFO *to_cs,
41 const char *from, size_t from_length,
42 CHARSET_INFO *from_cs, uint *errors)
43 {
44 return my_convert(to, (uint)to_length, to_cs, from, (uint)from_length, from_cs, errors);
45 }
46
47
48 class String_copy_status: protected MY_STRCOPY_STATUS
49 {
50 public:
source_end_pos()51 const char *source_end_pos() const
52 { return m_source_end_pos; }
well_formed_error_pos()53 const char *well_formed_error_pos() const
54 { return m_well_formed_error_pos; }
55 };
56
57
58 class Well_formed_prefix_status: public String_copy_status
59 {
60 public:
Well_formed_prefix_status(CHARSET_INFO * cs,const char * str,const char * end,size_t nchars)61 Well_formed_prefix_status(CHARSET_INFO *cs,
62 const char *str, const char *end, size_t nchars)
63 { cs->cset->well_formed_char_length(cs, str, end, nchars, this); }
64 };
65
66
67 class Well_formed_prefix: public Well_formed_prefix_status
68 {
69 const char *m_str; // The beginning of the string
70 public:
Well_formed_prefix(CHARSET_INFO * cs,const char * str,const char * end,size_t nchars)71 Well_formed_prefix(CHARSET_INFO *cs, const char *str, const char *end,
72 size_t nchars)
73 :Well_formed_prefix_status(cs, str, end, nchars), m_str(str)
74 { }
Well_formed_prefix(CHARSET_INFO * cs,const char * str,size_t length,size_t nchars)75 Well_formed_prefix(CHARSET_INFO *cs, const char *str, size_t length,
76 size_t nchars)
77 :Well_formed_prefix_status(cs, str, str + length, nchars), m_str(str)
78 { }
Well_formed_prefix(CHARSET_INFO * cs,const char * str,size_t length)79 Well_formed_prefix(CHARSET_INFO *cs, const char *str, size_t length)
80 :Well_formed_prefix_status(cs, str, str + length, length), m_str(str)
81 { }
Well_formed_prefix(CHARSET_INFO * cs,LEX_CSTRING str,size_t nchars)82 Well_formed_prefix(CHARSET_INFO *cs, LEX_CSTRING str, size_t nchars)
83 :Well_formed_prefix_status(cs, str.str, str.str + str.length, nchars),
84 m_str(str.str)
85 { }
length()86 size_t length() const { return m_source_end_pos - m_str; }
87 };
88
89
90 class String_copier: public String_copy_status,
91 protected MY_STRCONV_STATUS
92 {
93 public:
cannot_convert_error_pos()94 const char *cannot_convert_error_pos() const
95 { return m_cannot_convert_error_pos; }
most_important_error_pos()96 const char *most_important_error_pos() const
97 {
98 return well_formed_error_pos() ? well_formed_error_pos() :
99 cannot_convert_error_pos();
100 }
101 /*
102 Convert a string between character sets.
103 "dstcs" and "srccs" cannot be &my_charset_bin.
104 */
convert_fix(CHARSET_INFO * dstcs,char * dst,size_t dst_length,CHARSET_INFO * srccs,const char * src,size_t src_length,size_t nchars)105 size_t convert_fix(CHARSET_INFO *dstcs, char *dst, size_t dst_length,
106 CHARSET_INFO *srccs, const char *src, size_t src_length, size_t nchars)
107 {
108 return my_convert_fix(dstcs, dst, dst_length,
109 srccs, src, src_length, nchars, this, this);
110 }
111 /*
112 Copy a string. Fix bad bytes/characters to '?'.
113 */
114 uint well_formed_copy(CHARSET_INFO *to_cs, char *to, size_t to_length,
115 CHARSET_INFO *from_cs, const char *from, size_t from_length, size_t nchars);
116 // Same as above, but without the "nchars" limit.
well_formed_copy(CHARSET_INFO * to_cs,char * to,size_t to_length,CHARSET_INFO * from_cs,const char * from,size_t from_length)117 uint well_formed_copy(CHARSET_INFO *to_cs, char *to, size_t to_length,
118 CHARSET_INFO *from_cs, const char *from, size_t from_length)
119 {
120 return well_formed_copy(to_cs, to, to_length,
121 from_cs, from, from_length,
122 from_length /* No limit on "nchars"*/);
123 }
124 };
125
126
127 size_t my_copy_with_hex_escaping(CHARSET_INFO *cs,
128 char *dst, size_t dstlen,
129 const char *src, size_t srclen);
130 uint convert_to_printable(char *to, size_t to_len,
131 const char *from, size_t from_len,
132 CHARSET_INFO *from_cs, size_t nbytes= 0);
133
134 class Charset
135 {
136 CHARSET_INFO *m_charset;
137 public:
Charset()138 Charset() :m_charset(&my_charset_bin) { }
Charset(CHARSET_INFO * cs)139 Charset(CHARSET_INFO *cs) :m_charset(cs) { }
140
charset()141 CHARSET_INFO *charset() const { return m_charset; }
142 /*
143 Collation name without the character set name.
144 For example, in case of "latin1_swedish_ci",
145 this method returns "_swedish_ci".
146 */
147 LEX_CSTRING collation_specific_name() const;
148 bool encoding_allows_reinterpret_as(CHARSET_INFO *cs) const;
149 bool eq_collation_specific_names(CHARSET_INFO *cs) const;
150 };
151
152 class String : public Sql_alloc
153 {
154 char *Ptr;
155 uint32 str_length,Alloced_length, extra_alloc;
156 bool alloced,thread_specific;
157 CHARSET_INFO *str_charset;
158 public:
String()159 String()
160 {
161 Ptr=0; str_length=Alloced_length=extra_alloc=0;
162 alloced= thread_specific= 0;
163 str_charset= &my_charset_bin;
164 }
String(size_t length_arg)165 String(size_t length_arg)
166 {
167 alloced= thread_specific= 0;
168 Alloced_length= extra_alloc= 0; (void) real_alloc(length_arg);
169 str_charset= &my_charset_bin;
170 }
String(const char * str,CHARSET_INFO * cs)171 String(const char *str, CHARSET_INFO *cs)
172 {
173 Ptr=(char*) str; str_length= (uint32) strlen(str);
174 Alloced_length= extra_alloc= 0;
175 alloced= thread_specific= 0;
176 str_charset=cs;
177 }
178 /*
179 NOTE: If one intend to use the c_ptr() method, the following two
180 contructors need the size of memory for STR to be at least LEN+1 (to make
181 room for zero termination).
182 */
String(const char * str,size_t len,CHARSET_INFO * cs)183 String(const char *str,size_t len, CHARSET_INFO *cs)
184 {
185 Ptr=(char*) str; str_length=(uint32)len; Alloced_length= extra_alloc=0;
186 alloced= thread_specific= 0;
187 str_charset=cs;
188 }
String(char * str,size_t len,CHARSET_INFO * cs)189 String(char *str,size_t len, CHARSET_INFO *cs)
190 {
191 Ptr=(char*) str; Alloced_length=str_length=(uint32)len; extra_alloc= 0;
192 alloced= thread_specific= 0;
193 str_charset=cs;
194 }
String(const String & str)195 String(const String &str)
196 {
197 Ptr=str.Ptr ; str_length=str.str_length ;
198 Alloced_length=str.Alloced_length; extra_alloc= 0;
199 alloced= thread_specific= 0;
200 str_charset=str.str_charset;
201 }
~String()202 ~String() { free(); }
203
204 /* Mark variable thread specific it it's not allocated already */
set_thread_specific()205 inline void set_thread_specific()
206 {
207 if (!alloced)
208 thread_specific= 1;
209 }
set_charset(CHARSET_INFO * charset_arg)210 inline void set_charset(CHARSET_INFO *charset_arg)
211 { str_charset= charset_arg; }
charset()212 inline CHARSET_INFO *charset() const { return str_charset; }
length()213 inline uint32 length() const { return str_length;}
alloced_length()214 inline uint32 alloced_length() const { return Alloced_length;}
extra_allocation()215 inline uint32 extra_allocation() const { return extra_alloc;}
216 inline char& operator [] (size_t i) const { return Ptr[i]; }
length(size_t len)217 inline void length(size_t len) { str_length=(uint32)len ; }
extra_allocation(size_t len)218 inline void extra_allocation(size_t len) { extra_alloc= (uint32)len; }
is_empty()219 inline bool is_empty() const { return (str_length == 0); }
mark_as_const()220 inline void mark_as_const() { Alloced_length= 0;}
ptr()221 inline const char *ptr() const { return Ptr; }
end()222 inline const char *end() const { return Ptr + str_length; }
c_ptr()223 inline char *c_ptr()
224 {
225 DBUG_ASSERT(!alloced || !Ptr || !Alloced_length ||
226 (Alloced_length >= (str_length + 1)));
227
228 if (!Ptr || Ptr[str_length]) /* Should be safe */
229 (void) realloc(str_length);
230 return Ptr;
231 }
c_ptr_quick()232 inline char *c_ptr_quick()
233 {
234 if (Ptr && str_length < Alloced_length)
235 Ptr[str_length]=0;
236 return Ptr;
237 }
c_ptr_safe()238 inline char *c_ptr_safe()
239 {
240 if (Ptr && str_length < Alloced_length)
241 Ptr[str_length]=0;
242 else
243 (void) realloc(str_length);
244 return Ptr;
245 }
lex_string()246 LEX_STRING lex_string() const
247 {
248 LEX_STRING str = { (char*) ptr(), length() };
249 return str;
250 }
lex_cstring()251 LEX_CSTRING lex_cstring() const
252 {
253 LEX_CSTRING skr = { ptr(), length() };
254 return skr;
255 }
256
lengthsp()257 size_t lengthsp() const
258 {
259 return str_charset->cset->lengthsp(str_charset, Ptr, str_length);
260 }
261
set(String & str,size_t offset,size_t arg_length)262 void set(String &str,size_t offset,size_t arg_length)
263 {
264 DBUG_ASSERT(&str != this);
265 free();
266 Ptr=(char*) str.ptr()+offset; str_length=(uint32)arg_length;
267 if (str.Alloced_length)
268 Alloced_length=(uint32)(str.Alloced_length-offset);
269 str_charset=str.str_charset;
270 }
271
272
273 /**
274 Points the internal buffer to the supplied one. The old buffer is freed.
275 @param str Pointer to the new buffer.
276 @param arg_length Length of the new buffer in characters, excluding any
277 null character.
278 @param cs Character set to use for interpreting string data.
279 @note The new buffer will not be null terminated.
280 */
set(char * str,size_t arg_length,CHARSET_INFO * cs)281 inline void set(char *str,size_t arg_length, CHARSET_INFO *cs)
282 {
283 free();
284 Ptr=(char*) str; str_length=Alloced_length=(uint32)arg_length;
285 str_charset=cs;
286 }
set(const char * str,size_t arg_length,CHARSET_INFO * cs)287 inline void set(const char *str,size_t arg_length, CHARSET_INFO *cs)
288 {
289 free();
290 Ptr=(char*) str; str_length=(uint32)arg_length;
291 str_charset=cs;
292 }
293 bool set_ascii(const char *str, size_t arg_length);
set_quick(char * str,size_t arg_length,CHARSET_INFO * cs)294 inline void set_quick(char *str,size_t arg_length, CHARSET_INFO *cs)
295 {
296 if (!alloced)
297 {
298 Ptr=(char*) str; str_length=Alloced_length=(uint32)arg_length;
299 }
300 str_charset=cs;
301 }
302 bool set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs);
set(int num,CHARSET_INFO * cs)303 bool set(int num, CHARSET_INFO *cs) { return set_int(num, false, cs); }
set(uint num,CHARSET_INFO * cs)304 bool set(uint num, CHARSET_INFO *cs) { return set_int(num, true, cs); }
set(long num,CHARSET_INFO * cs)305 bool set(long num, CHARSET_INFO *cs) { return set_int(num, false, cs); }
set(ulong num,CHARSET_INFO * cs)306 bool set(ulong num, CHARSET_INFO *cs) { return set_int(num, true, cs); }
set(longlong num,CHARSET_INFO * cs)307 bool set(longlong num, CHARSET_INFO *cs) { return set_int(num, false, cs); }
set(ulonglong num,CHARSET_INFO * cs)308 bool set(ulonglong num, CHARSET_INFO *cs) { return set_int((longlong)num, true, cs); }
309 bool set_real(double num,uint decimals, CHARSET_INFO *cs);
310
311 bool set_hex(ulonglong num);
312 bool set_hex(const char *str, uint32 len);
313
314 /* Take over handling of buffer from some other object */
reset(char * ptr_arg,size_t length_arg,size_t alloced_length_arg,CHARSET_INFO * cs)315 void reset(char *ptr_arg, size_t length_arg, size_t alloced_length_arg,
316 CHARSET_INFO *cs)
317 {
318 free();
319 Ptr= ptr_arg;
320 str_length= (uint32)length_arg;
321 Alloced_length= (uint32)alloced_length_arg;
322 str_charset= cs;
323 alloced= ptr_arg != 0;
324 }
325
326 /* Forget about the buffer, let some other object handle it */
release()327 char *release()
328 {
329 char *old= Ptr;
330 Ptr=0; str_length= Alloced_length= extra_alloc= 0;
331 alloced= thread_specific= 0;
332 return old;
333 }
334
335 /*
336 PMG 2004.11.12
337 This is a method that works the same as perl's "chop". It simply
338 drops the last character of a string. This is useful in the case
339 of the federated storage handler where I'm building a unknown
340 number, list of values and fields to be used in a sql insert
341 statement to be run on the remote server, and have a comma after each.
342 When the list is complete, I "chop" off the trailing comma
343
344 ex.
345 String stringobj;
346 stringobj.append("VALUES ('foo', 'fi', 'fo',");
347 stringobj.chop();
348 stringobj.append(")");
349
350 In this case, the value of string was:
351
352 VALUES ('foo', 'fi', 'fo',
353 VALUES ('foo', 'fi', 'fo'
354 VALUES ('foo', 'fi', 'fo')
355
356 */
chop()357 inline void chop()
358 {
359 str_length--;
360 Ptr[str_length]= '\0';
361 DBUG_ASSERT(strlen(Ptr) == str_length);
362 }
363
free()364 inline void free()
365 {
366 if (alloced)
367 {
368 alloced=0;
369 my_free(Ptr);
370 }
371 Alloced_length= extra_alloc= 0;
372 Ptr=0;
373 str_length=0; /* Safety */
374 }
alloc(size_t arg_length)375 inline bool alloc(size_t arg_length)
376 {
377 if (arg_length < Alloced_length)
378 return 0;
379 return real_alloc(arg_length);
380 }
381 bool real_alloc(size_t arg_length); // Empties old string
382 bool realloc_raw(size_t arg_length);
realloc(size_t arg_length)383 bool realloc(size_t arg_length)
384 {
385 if (realloc_raw(arg_length))
386 return TRUE;
387 Ptr[arg_length]=0; // This make other funcs shorter
388 return FALSE;
389 }
realloc_with_extra(size_t arg_length)390 bool realloc_with_extra(size_t arg_length)
391 {
392 if (extra_alloc < 4096)
393 extra_alloc= extra_alloc*2+128;
394 if (realloc_raw(arg_length + extra_alloc))
395 return TRUE;
396 Ptr[arg_length]=0; // This make other funcs shorter
397 return FALSE;
398 }
realloc_with_extra_if_needed(size_t arg_length)399 bool realloc_with_extra_if_needed(size_t arg_length)
400 {
401 if (arg_length < Alloced_length)
402 {
403 Ptr[arg_length]=0; // behave as if realloc was called.
404 return 0;
405 }
406 return realloc_with_extra(arg_length);
407 }
408 // Shrink the buffer, but only if it is allocated on the heap.
shrink(size_t arg_length)409 inline void shrink(size_t arg_length)
410 {
411 if (!is_alloced())
412 return;
413 if (ALIGN_SIZE(arg_length+1) < Alloced_length)
414 {
415 char *new_ptr;
416 if (unlikely(!(new_ptr=(char*)
417 my_realloc(Ptr,
418 arg_length,MYF((thread_specific ?
419 MY_THREAD_SPECIFIC : 0))))))
420 {
421 Alloced_length = 0;
422 real_alloc(arg_length);
423 }
424 else
425 {
426 Ptr=new_ptr;
427 Alloced_length=(uint32)arg_length;
428 }
429 }
430 }
is_alloced()431 bool is_alloced() const { return alloced; }
432 inline String& operator = (const String &s)
433 {
434 if (&s != this)
435 {
436 /*
437 It is forbidden to do assignments like
438 some_string = substring_of_that_string
439 */
440 DBUG_ASSERT(!s.uses_buffer_owned_by(this));
441 free();
442 Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
443 str_charset=s.str_charset;
444 }
445 return *this;
446 }
447
448 bool copy(); // Alloc string if not alloced
449 bool copy(const String &s); // Allocate new string
450 bool copy(const char *s,size_t arg_length, CHARSET_INFO *cs); // Allocate new string
451 bool copy_or_move(const char *s,size_t arg_length, CHARSET_INFO *cs);
452 static bool needs_conversion(size_t arg_length,
453 CHARSET_INFO *cs_from, CHARSET_INFO *cs_to,
454 uint32 *offset);
455 static bool needs_conversion_on_storage(size_t arg_length,
456 CHARSET_INFO *cs_from,
457 CHARSET_INFO *cs_to);
458 bool copy_aligned(const char *s, size_t arg_length, size_t offset,
459 CHARSET_INFO *cs);
460 bool set_or_copy_aligned(const char *s, size_t arg_length, CHARSET_INFO *cs);
461 bool copy(const char*s, size_t arg_length, CHARSET_INFO *csfrom,
462 CHARSET_INFO *csto, uint *errors);
copy(const String * str,CHARSET_INFO * tocs,uint * errors)463 bool copy(const String *str, CHARSET_INFO *tocs, uint *errors)
464 {
465 return copy(str->ptr(), str->length(), str->charset(), tocs, errors);
466 }
copy(CHARSET_INFO * tocs,CHARSET_INFO * fromcs,const char * src,size_t src_length,size_t nchars,String_copier * copier)467 bool copy(CHARSET_INFO *tocs,
468 CHARSET_INFO *fromcs, const char *src, size_t src_length,
469 size_t nchars, String_copier *copier)
470 {
471 if (unlikely(alloc(tocs->mbmaxlen * src_length)))
472 return true;
473 str_length= copier->well_formed_copy(tocs, Ptr, Alloced_length,
474 fromcs, src, (uint)src_length, (uint)nchars);
475 str_charset= tocs;
476 return false;
477 }
move(String & s)478 void move(String &s)
479 {
480 free();
481 Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
482 extra_alloc= s.extra_alloc;
483 alloced= s.alloced;
484 thread_specific= s.thread_specific;
485 s.alloced= 0;
486 }
487 bool append(const String &s);
488 bool append(const char *s);
append(const LEX_STRING * ls)489 bool append(const LEX_STRING *ls)
490 {
491 DBUG_ASSERT(ls->length < UINT_MAX32 &&
492 ((ls->length == 0 && !ls->str) ||
493 ls->length == strlen(ls->str)));
494 return append(ls->str, (uint32) ls->length);
495 }
append(const LEX_CSTRING * ls)496 bool append(const LEX_CSTRING *ls)
497 {
498 DBUG_ASSERT(ls->length < UINT_MAX32 &&
499 ((ls->length == 0 && !ls->str) ||
500 ls->length == strlen(ls->str)));
501 return append(ls->str, (uint32) ls->length);
502 }
append(const LEX_CSTRING & ls)503 bool append(const LEX_CSTRING &ls)
504 {
505 return append(&ls);
506 }
507 bool append(const char *s, size_t size);
508 bool append(const char *s, size_t arg_length, CHARSET_INFO *cs);
append(const LEX_CSTRING & s,CHARSET_INFO * cs)509 bool append(const LEX_CSTRING &s, CHARSET_INFO *cs)
510 {
511 return append(s.str, s.length, cs);
512 }
513 bool append_ulonglong(ulonglong val);
514 bool append_longlong(longlong val);
515 bool append(IO_CACHE* file, uint32 arg_length);
516 bool append_with_prefill(const char *s, uint32 arg_length,
517 uint32 full_length, char fill_char);
518 bool append_parenthesized(long nr, int radix= 10);
519 int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
520 int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
521 bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length);
522 bool replace(uint32 offset,uint32 arg_length,const String &to);
append(char chr)523 inline bool append(char chr)
524 {
525 if (str_length < Alloced_length)
526 {
527 Ptr[str_length++]=chr;
528 }
529 else
530 {
531 if (unlikely(realloc_with_extra(str_length + 1)))
532 return 1;
533 Ptr[str_length++]=chr;
534 }
535 return 0;
536 }
append_hex(const char * src,uint32 srclen)537 bool append_hex(const char *src, uint32 srclen)
538 {
539 for (const char *src_end= src + srclen ; src != src_end ; src++)
540 {
541 if (unlikely(append(_dig_vec_lower[((uchar) *src) >> 4])) ||
542 unlikely(append(_dig_vec_lower[((uchar) *src) & 0x0F])))
543 return true;
544 }
545 return false;
546 }
append_hex(const uchar * src,uint32 srclen)547 bool append_hex(const uchar *src, uint32 srclen)
548 {
549 return append_hex((const char*)src, srclen);
550 }
551 bool fill(uint32 max_length,char fill);
552 void strip_sp();
553 friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
554 friend int stringcmp(const String *a,const String *b);
555 friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
556 friend class Field;
557 uint32 numchars() const;
558 int charpos(longlong i,uint32 offset=0);
559
reserve(size_t space_needed)560 int reserve(size_t space_needed)
561 {
562 return realloc(str_length + space_needed);
563 }
564 int reserve(size_t space_needed, size_t grow_by);
565
566 /*
567 The following append operations do NOT check alloced memory
568 q_*** methods writes values of parameters itself
569 qs_*** methods writes string representation of value
570 */
q_append(const char c)571 void q_append(const char c)
572 {
573 Ptr[str_length++] = c;
574 }
q_append2b(const uint32 n)575 void q_append2b(const uint32 n)
576 {
577 int2store(Ptr + str_length, n);
578 str_length += 2;
579 }
q_append(const uint32 n)580 void q_append(const uint32 n)
581 {
582 int4store(Ptr + str_length, n);
583 str_length += 4;
584 }
q_append(double d)585 void q_append(double d)
586 {
587 float8store(Ptr + str_length, d);
588 str_length += 8;
589 }
q_append(double * d)590 void q_append(double *d)
591 {
592 float8store(Ptr + str_length, *d);
593 str_length += 8;
594 }
q_append(const char * data,size_t data_len)595 void q_append(const char *data, size_t data_len)
596 {
597 if (data_len)
598 memcpy(Ptr + str_length, data, data_len);
599 DBUG_ASSERT(str_length <= UINT_MAX32 - data_len);
600 str_length += (uint)data_len;
601 }
q_append(const LEX_CSTRING * ls)602 void q_append(const LEX_CSTRING *ls)
603 {
604 DBUG_ASSERT(ls->length < UINT_MAX32 &&
605 ((ls->length == 0 && !ls->str) ||
606 ls->length == strlen(ls->str)));
607 q_append(ls->str, (uint32) ls->length);
608 }
609
write_at_position(int position,uint32 value)610 void write_at_position(int position, uint32 value)
611 {
612 int4store(Ptr + position,value);
613 }
614
qs_append(const char * str)615 void qs_append(const char *str)
616 {
617 qs_append(str, (uint32)strlen(str));
618 }
qs_append(const LEX_CSTRING * ls)619 void qs_append(const LEX_CSTRING *ls)
620 {
621 DBUG_ASSERT(ls->length < UINT_MAX32 &&
622 ((ls->length == 0 && !ls->str) ||
623 ls->length == strlen(ls->str)));
624 qs_append(ls->str, (uint32)ls->length);
625 }
626 void qs_append(const char *str, size_t len);
627 void qs_append_hex(const char *str, uint32 len);
628 void qs_append(double d);
629 void qs_append(double *d);
qs_append(const char c)630 inline void qs_append(const char c)
631 {
632 Ptr[str_length]= c;
633 str_length++;
634 }
635 void qs_append(int i);
qs_append(uint i)636 void qs_append(uint i)
637 {
638 qs_append((ulonglong)i);
639 }
qs_append(ulong i)640 void qs_append(ulong i)
641 {
642 qs_append((ulonglong)i);
643 }
644 void qs_append(ulonglong i);
qs_append(longlong i,int radix)645 void qs_append(longlong i, int radix)
646 {
647 char *buff= Ptr + str_length;
648 char *end= ll2str(i, buff, radix, 0);
649 str_length+= uint32(end-buff);
650 }
651
652 /* Inline (general) functions used by the protocol functions */
653
prep_append(uint32 arg_length,uint32 step_alloc)654 inline char *prep_append(uint32 arg_length, uint32 step_alloc)
655 {
656 uint32 new_length= arg_length + str_length;
657 if (new_length > Alloced_length)
658 {
659 if (unlikely(realloc(new_length + step_alloc)))
660 return 0;
661 }
662 uint32 old_length= str_length;
663 str_length+= arg_length;
664 return Ptr+ old_length; /* Area to use */
665 }
666
667
append(const char * s,uint32 arg_length,uint32 step_alloc)668 inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
669 {
670 uint32 new_length= arg_length + str_length;
671 if (new_length > Alloced_length &&
672 unlikely(realloc(new_length + step_alloc)))
673 return TRUE;
674 memcpy(Ptr+str_length, s, arg_length);
675 str_length+= arg_length;
676 return FALSE;
677 }
678 void print(String *to) const;
679 void print_with_conversion(String *to, CHARSET_INFO *cs) const;
print(String * to,CHARSET_INFO * cs)680 void print(String *to, CHARSET_INFO *cs) const
681 {
682 if (my_charset_same(charset(), cs))
683 print(to);
684 else
685 print_with_conversion(to, cs);
686 }
687
688 bool append_for_single_quote(const char *st, size_t len);
append_for_single_quote(const String * s)689 bool append_for_single_quote(const String *s)
690 {
691 return append_for_single_quote(s->ptr(), s->length());
692 }
append_for_single_quote(const char * st)693 bool append_for_single_quote(const char *st)
694 {
695 size_t len= strlen(st);
696 DBUG_ASSERT(len < UINT_MAX32);
697 return append_for_single_quote(st, (uint32) len);
698 }
699
700 /* Swap two string objects. Efficient way to exchange data without memcpy. */
701 void swap(String &s);
702
uses_buffer_owned_by(const String * s)703 inline bool uses_buffer_owned_by(const String *s) const
704 {
705 return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
706 }
well_formed_length()707 uint well_formed_length() const
708 {
709 return (uint) Well_formed_prefix(charset(), ptr(), length()).length();
710 }
is_ascii()711 bool is_ascii() const
712 {
713 if (length() == 0)
714 return TRUE;
715 if (charset()->mbminlen > 1)
716 return FALSE;
717 for (const char *c= ptr(), *c_end= c + length(); c < c_end; c++)
718 {
719 if (!my_isascii(*c))
720 return FALSE;
721 }
722 return TRUE;
723 }
bin_eq(const String * other)724 bool bin_eq(const String *other) const
725 {
726 return length() == other->length() &&
727 !memcmp(ptr(), other->ptr(), length());
728 }
eq(const String * other,CHARSET_INFO * cs)729 bool eq(const String *other, CHARSET_INFO *cs) const
730 {
731 return !sortcmp(this, other, cs);
732 }
q_net_store_length(ulonglong length)733 void q_net_store_length(ulonglong length)
734 {
735 DBUG_ASSERT(Alloced_length >= (str_length + net_length_size(length)));
736 char *pos= (char *) net_store_length((uchar *)(Ptr + str_length), length);
737 str_length= uint32(pos - Ptr);
738 }
q_net_store_data(const uchar * from,size_t length)739 void q_net_store_data(const uchar *from, size_t length)
740 {
741 DBUG_ASSERT(length < UINT_MAX32);
742 DBUG_ASSERT(Alloced_length >= (str_length + length +
743 net_length_size(length)));
744 q_net_store_length(length);
745 q_append((const char *)from, (uint32) length);
746 }
747 };
748
749
750 // The following class is a backport from MySQL 5.6:
751 /**
752 String class wrapper with a preallocated buffer of size buff_sz
753
754 This class allows to replace sequences of:
755 char buff[12345];
756 String str(buff, sizeof(buff));
757 str.length(0);
758 with a simple equivalent declaration:
759 StringBuffer<12345> str;
760 */
761
762 template<size_t buff_sz>
763 class StringBuffer : public String
764 {
765 char buff[buff_sz];
766
767 public:
StringBuffer()768 StringBuffer() : String(buff, buff_sz, &my_charset_bin) { length(0); }
StringBuffer(CHARSET_INFO * cs)769 explicit StringBuffer(CHARSET_INFO *cs) : String(buff, buff_sz, cs)
770 {
771 length(0);
772 }
773 };
774
775
check_if_only_end_space(CHARSET_INFO * cs,const char * str,const char * end)776 static inline bool check_if_only_end_space(CHARSET_INFO *cs,
777 const char *str,
778 const char *end)
779 {
780 return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
781 }
782
783 int append_query_string(CHARSET_INFO *csinfo, String *to,
784 const char *str, size_t len, bool no_backslash);
785
786 #endif /* SQL_STRING_INCLUDED */
787