1 #ifndef CLIENT_SQL_STRING_INCLUDED
2 #define CLIENT_SQL_STRING_INCLUDED
27 inline uint32 copy_and_convert(
char *
to, uint32 to_length,
29 const char *from, uint32 from_length,
32 return my_convert(to, to_length, to_cs, from, from_length, from_cs, errors);
38 uint32 str_length,Alloced_length;
44 Ptr=0; str_length=Alloced_length=0; alloced=0;
45 str_charset= &my_charset_bin;
49 alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
50 str_charset= &my_charset_bin;
54 Ptr=(
char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
59 Ptr=(
char*) str; str_length=len; Alloced_length=0; alloced=0;
64 Ptr=(
char*) str; Alloced_length=str_length=len; alloced=0;
69 Ptr=str.Ptr ; str_length=str.str_length ;
70 Alloced_length=str.Alloced_length; alloced=0;
71 str_charset=str.str_charset;
73 static void *
operator new(
size_t size,
MEM_ROOT *mem_root)
74 {
return (
void*) alloc_root(mem_root, (uint)
size); }
75 static void operator delete(
void *ptr_arg,
size_t size)
81 static void operator delete(
void *,
MEM_ROOT *)
86 { str_charset= charset_arg; }
87 inline const CHARSET_INFO *charset()
const {
return str_charset; }
88 inline uint32 length()
const {
return str_length;}
89 inline uint32 alloced_length()
const {
return Alloced_length;}
90 inline char& operator [] (uint32
i)
const {
return Ptr[
i]; }
91 inline void length(uint32 len) { str_length=len ; }
92 inline bool is_empty() {
return (str_length == 0); }
93 inline void mark_as_const() { Alloced_length= 0;}
94 inline const char *ptr()
const {
return Ptr; }
97 DBUG_ASSERT(!alloced || !
Ptr || !Alloced_length ||
98 (Alloced_length >= (str_length + 1)));
100 if (!
Ptr ||
Ptr[str_length])
104 inline char *c_ptr_quick()
106 if (
Ptr && str_length < Alloced_length)
110 inline char *c_ptr_safe()
112 if (
Ptr && str_length < Alloced_length)
121 DBUG_ASSERT(&str !=
this);
123 Ptr=(
char*) str.ptr()+
offset; str_length=arg_length; alloced=0;
124 if (str.Alloced_length)
125 Alloced_length=str.Alloced_length-
offset;
128 str_charset=str.str_charset;
130 inline void set(
char *str,uint32 arg_length,
const CHARSET_INFO *cs)
133 Ptr=(
char*) str; str_length=Alloced_length=arg_length ; alloced=0;
136 inline void set(
const char *str,uint32 arg_length,
const CHARSET_INFO *cs)
139 Ptr=(
char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
142 bool set_ascii(
const char *str, uint32 arg_length);
143 inline void set_quick(
char *str,uint32 arg_length,
const CHARSET_INFO *cs)
147 Ptr=(
char*) str; str_length=Alloced_length=arg_length;
153 bool set(
double num,uint decimals,
const CHARSET_INFO *cs);
179 Ptr[str_length--]=
'\0';
193 inline bool alloc(uint32 arg_length)
195 if (arg_length < Alloced_length)
197 return real_alloc(arg_length);
199 bool real_alloc(uint32 arg_length);
200 bool realloc(uint32 arg_length);
203 inline void shrink(uint32 arg_length)
207 if (arg_length < Alloced_length)
210 if (!(new_ptr=(
char*) my_realloc(
Ptr,arg_length,MYF(0))))
213 real_alloc(arg_length);
218 Alloced_length=arg_length;
222 bool is_alloced()
const {
return alloced; }
231 DBUG_ASSERT(!s.uses_buffer_owned_by(
this));
233 Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
234 str_charset=s.str_charset;
241 bool copy(
const String &s);
243 bool copy(
const char *s,uint32 arg_length,
const CHARSET_INFO *cs);
244 static bool needs_conversion(uint32 arg_length,
247 bool copy_aligned(
const char *s, uint32 arg_length, uint32 offset,
249 bool set_or_copy_aligned(
const char *s, uint32 arg_length,
251 bool copy(
const char*s,uint32 arg_length,
const CHARSET_INFO *csfrom,
253 bool append(
const String &s);
254 bool append(
const char *s);
255 bool append(
const char *s,uint32 arg_length);
256 bool append(
const char *s,uint32 arg_length,
const CHARSET_INFO *cs);
258 bool append_with_prefill(
const char *s, uint32 arg_length,
259 uint32 full_length,
char fill_char);
260 int strstr(
const String &search,uint32 offset=0);
261 int strrstr(
const String &search,uint32 offset=0);
262 bool replace(uint32 offset,uint32 arg_length,
const char *
to,uint32 length);
263 bool replace(uint32 offset,uint32 arg_length,
const String &to);
264 inline bool append(
char chr)
266 if (str_length < Alloced_length)
268 Ptr[str_length++]=chr;
274 Ptr[str_length++]=chr;
278 bool fill(uint32 max_length,
char fill);
283 uint32 numchars()
const;
284 int charpos(
int i,uint32 offset=0);
286 int reserve(uint32 space_needed)
288 return realloc(str_length + space_needed);
290 int reserve(uint32 space_needed, uint32 grow_by);
297 void q_append(
const char c)
299 Ptr[str_length++] = c;
301 void q_append(
const uint32
n)
303 int4store(
Ptr + str_length, n);
306 void q_append(
double d)
308 float8store(
Ptr + str_length, d);
311 void q_append(
double *d)
313 float8store(
Ptr + str_length, *d);
316 void q_append(
const char *data, uint32 data_len)
318 memcpy(
Ptr + str_length, data, data_len);
319 str_length += data_len;
322 void write_at_position(
int position, uint32 value)
324 int4store(
Ptr + position,value);
327 void qs_append(
const char *str, uint32 len);
328 void qs_append(
double d);
329 void qs_append(
double *d);
330 inline void qs_append(
const char c)
335 void qs_append(
int i);
336 void qs_append(uint i);
340 inline char *prep_append(uint32 arg_length, uint32 step_alloc)
342 uint32 new_length= arg_length + str_length;
343 if (new_length > Alloced_length)
345 if (
realloc(new_length + step_alloc))
348 uint32 old_length= str_length;
349 str_length+= arg_length;
350 return Ptr+ old_length;
353 inline bool append(
const char *s, uint32 arg_length, uint32 step_alloc)
355 uint32 new_length= arg_length + str_length;
356 if (new_length > Alloced_length &&
realloc(new_length + step_alloc))
358 memcpy(
Ptr+str_length, s, arg_length);
359 str_length+= arg_length;
362 void print(
String *print);
367 inline bool uses_buffer_owned_by(
const String *s)
const