1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* This file is originally from the mysql distribution. Coded by monty */
18
#ifdef USE_PRAGMA_INTERFACE
19
#pragma interface /* gcc class implementation */
23
#define NOT_FIXED_DEC 31
27
int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
28
String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
29
uint32 copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
30
const char *from, uint32 from_length,
31
CHARSET_INFO *from_cs, uint *errors);
32
uint32 well_formed_copy_nchars(CHARSET_INFO *to_cs,
33
char *to, uint to_length,
34
CHARSET_INFO *from_cs,
35
const char *from, uint from_length,
37
const char **well_formed_error_pos,
38
const char **cannot_convert_error_pos,
39
const char **from_end_pos);
40
size_t my_copy_with_hex_escaping(CHARSET_INFO *cs,
41
char *dst, size_t dstlen,
42
const char *src, size_t srclen);
47
uint32 str_length,Alloced_length;
49
CHARSET_INFO *str_charset;
53
Ptr=0; str_length=Alloced_length=0; alloced=0;
54
str_charset= &my_charset_bin;
56
String(uint32 length_arg)
58
alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
59
str_charset= &my_charset_bin;
61
String(const char *str, CHARSET_INFO *cs)
63
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
66
String(const char *str,uint32 len, CHARSET_INFO *cs)
68
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
71
String(char *str,uint32 len, CHARSET_INFO *cs)
73
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
76
String(const String &str)
78
Ptr=str.Ptr ; str_length=str.str_length ;
79
Alloced_length=str.Alloced_length; alloced=0;
80
str_charset=str.str_charset;
82
static void *operator new(size_t size, MEM_ROOT *mem_root)
83
{ return (void*) alloc_root(mem_root, (uint) size); }
84
static void operator delete(void *ptr_arg,size_t size)
85
{ TRASH(ptr_arg, size); }
86
static void operator delete(void *ptr_arg, MEM_ROOT *mem_root)
87
{ /* never called */ }
90
inline void set_charset(CHARSET_INFO *charset_arg)
91
{ str_charset= charset_arg; }
92
inline CHARSET_INFO *charset() const { return str_charset; }
93
inline uint32 length() const { return str_length;}
94
inline uint32 alloced_length() const { return Alloced_length;}
95
inline char& operator [] (uint32 i) const { return Ptr[i]; }
96
inline void length(uint32 len) { str_length=len ; }
97
inline bool is_empty() { return (str_length == 0); }
98
inline void mark_as_const() { Alloced_length= 0;}
99
inline const char *ptr() const { return Ptr; }
102
if (!Ptr || Ptr[str_length]) /* Should be safe */
103
(void) realloc(str_length);
106
inline char *c_ptr_quick()
108
if (Ptr && str_length < Alloced_length)
112
inline char *c_ptr_safe()
114
if (Ptr && str_length < Alloced_length)
117
(void) realloc(str_length);
121
void set(String &str,uint32 offset,uint32 arg_length)
123
DBUG_ASSERT(&str != this);
125
Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
126
if (str.Alloced_length)
127
Alloced_length=str.Alloced_length-offset;
130
str_charset=str.str_charset;
132
inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
135
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
138
inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
141
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
144
bool set_ascii(const char *str, uint32 arg_length);
145
inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
149
Ptr=(char*) str; str_length=Alloced_length=arg_length;
153
bool set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs);
154
bool set(longlong num, CHARSET_INFO *cs)
155
{ return set_int(num, false, cs); }
156
bool set(ulonglong num, CHARSET_INFO *cs)
157
{ return set_int((longlong)num, true, cs); }
158
bool set_real(double num,uint decimals, CHARSET_INFO *cs);
162
This is a method that works the same as perl's "chop". It simply
163
drops the last character of a string. This is useful in the case
164
of the federated storage handler where I'm building a unknown
165
number, list of values and fields to be used in a sql insert
166
statement to be run on the remote server, and have a comma after each.
167
When the list is complete, I "chop" off the trailing comma
171
stringobj.append("VALUES ('foo', 'fi', 'fo',");
173
stringobj.append(")");
175
In this case, the value of string was:
177
VALUES ('foo', 'fi', 'fo',
178
VALUES ('foo', 'fi', 'fo'
179
VALUES ('foo', 'fi', 'fo')
184
Ptr[str_length--]= '\0';
195
str_length=0; /* Safety */
198
inline bool alloc(uint32 arg_length)
200
if (arg_length < Alloced_length)
202
return real_alloc(arg_length);
204
bool real_alloc(uint32 arg_length); // Empties old string
205
bool realloc(uint32 arg_length);
206
inline void shrink(uint32 arg_length) // Shrink buffer
208
if (arg_length < Alloced_length)
211
if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
214
real_alloc(arg_length);
219
Alloced_length=arg_length;
223
bool is_alloced() { return alloced; }
224
inline String& operator = (const String &s)
229
It is forbidden to do assignments like
230
some_string = substring_of_that_string
232
DBUG_ASSERT(!s.uses_buffer_owned_by(this));
234
Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
240
bool copy(); // Alloc string if not alloced
241
bool copy(const String &s); // Allocate new string
242
bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
243
static bool needs_conversion(uint32 arg_length,
244
CHARSET_INFO *cs_from, CHARSET_INFO *cs_to,
246
bool copy_aligned(const char *s, uint32 arg_length, uint32 offset,
248
bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs);
249
bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom,
250
CHARSET_INFO *csto, uint *errors);
251
bool append(const String &s);
252
bool append(const char *s);
253
bool append(const char *s,uint32 arg_length);
254
bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs);
255
bool append(IO_CACHE* file, uint32 arg_length);
256
bool append_with_prefill(const char *s, uint32 arg_length,
257
uint32 full_length, char fill_char);
258
int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
259
int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
260
bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length);
261
bool replace(uint32 offset,uint32 arg_length,const String &to);
262
inline bool append(char chr)
264
if (str_length < Alloced_length)
266
Ptr[str_length++]=chr;
270
if (realloc(str_length+1))
272
Ptr[str_length++]=chr;
276
bool fill(uint32 max_length,char fill);
278
friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
279
friend int stringcmp(const String *a,const String *b);
280
friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
282
int charpos(int i,uint32 offset=0);
284
int reserve(uint32 space_needed)
286
return realloc(str_length + space_needed);
288
int reserve(uint32 space_needed, uint32 grow_by);
291
The following append operations do NOT check alloced memory
292
q_*** methods writes values of parameters itself
293
qs_*** methods writes string representation of value
295
void q_append(const char c)
297
Ptr[str_length++] = c;
299
void q_append(const uint32 n)
301
int4store(Ptr + str_length, n);
304
void q_append(double d)
306
float8store(Ptr + str_length, d);
309
void q_append(double *d)
311
float8store(Ptr + str_length, *d);
314
void q_append(const char *data, uint32 data_len)
316
memcpy(Ptr + str_length, data, data_len);
317
str_length += data_len;
320
void write_at_position(int position, uint32 value)
322
int4store(Ptr + position,value);
325
void qs_append(const char *str, uint32 len);
326
void qs_append(double d);
327
void qs_append(double *d);
328
inline void qs_append(const char c)
333
void qs_append(int i);
334
void qs_append(uint i);
336
/* Inline (general) functions used by the protocol functions */
338
inline char *prep_append(uint32 arg_length, uint32 step_alloc)
340
uint32 new_length= arg_length + str_length;
341
if (new_length > Alloced_length)
343
if (realloc(new_length + step_alloc))
346
uint32 old_length= str_length;
347
str_length+= arg_length;
348
return Ptr+ old_length; /* Area to use */
351
inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
353
uint32 new_length= arg_length + str_length;
354
if (new_length > Alloced_length && realloc(new_length + step_alloc))
356
memcpy(Ptr+str_length, s, arg_length);
357
str_length+= arg_length;
360
void print(String *print);
362
/* Swap two string objects. Efficient way to exchange data without memcpy. */
363
void swap(String &s);
365
inline bool uses_buffer_owned_by(const String *s) const
367
return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
371
static inline bool check_if_only_end_space(CHARSET_INFO *cs, char *str,
374
return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
378
bool operator==(const String &s1, const String &s2)
380
return stringcmp(&s1,&s2) == 0;
384
bool operator!=(const String &s1, const String &s2)