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);
36
uint32 str_length,Alloced_length;
38
CHARSET_INFO *str_charset;
42
Ptr=0; str_length=Alloced_length=0; alloced=0;
43
str_charset= &my_charset_bin;
45
String(uint32 length_arg)
47
alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
48
str_charset= &my_charset_bin;
50
String(const char *str, CHARSET_INFO *cs)
52
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
55
String(const char *str,uint32 len, CHARSET_INFO *cs)
57
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
60
String(char *str,uint32 len, CHARSET_INFO *cs)
62
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
65
String(const String &str)
67
Ptr=str.Ptr ; str_length=str.str_length ;
68
Alloced_length=str.Alloced_length; alloced=0;
69
str_charset=str.str_charset;
71
static void *operator new(size_t size, MEM_ROOT *mem_root)
72
{ return (void*) alloc_root(mem_root, (uint) size); }
73
static void operator delete(void *ptr_arg,size_t size)
74
{ TRASH(ptr_arg, size); }
75
static void operator delete(void *ptr_arg, MEM_ROOT *mem_root)
76
{ /* never called */ }
79
inline void set_charset(CHARSET_INFO *charset_arg)
80
{ str_charset= charset_arg; }
81
inline CHARSET_INFO *charset() const { return str_charset; }
82
inline uint32 length() const { return str_length;}
83
inline uint32 alloced_length() const { return Alloced_length;}
84
inline char& operator [] (uint32 i) const { return Ptr[i]; }
85
inline void length(uint32 len) { str_length=len ; }
86
inline bool is_empty() { return (str_length == 0); }
87
inline void mark_as_const() { Alloced_length= 0;}
88
inline const char *ptr() const { return Ptr; }
91
if (!Ptr || Ptr[str_length]) /* Should be safe */
92
(void) realloc(str_length);
95
inline char *c_ptr_quick()
97
if (Ptr && str_length < Alloced_length)
101
inline char *c_ptr_safe()
103
if (Ptr && str_length < Alloced_length)
106
(void) realloc(str_length);
110
void set(String &str,uint32 offset,uint32 arg_length)
112
DBUG_ASSERT(&str != this);
114
Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
115
if (str.Alloced_length)
116
Alloced_length=str.Alloced_length-offset;
119
str_charset=str.str_charset;
121
inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
124
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
127
inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
130
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
133
bool set_ascii(const char *str, uint32 arg_length);
134
inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
138
Ptr=(char*) str; str_length=Alloced_length=arg_length;
142
bool set(longlong num, CHARSET_INFO *cs);
143
bool set(ulonglong num, CHARSET_INFO *cs);
144
bool set(double num,uint decimals, CHARSET_INFO *cs);
148
This is a method that works the same as perl's "chop". It simply
149
drops the last character of a string. This is useful in the case
150
of the federated storage handler where I'm building a unknown
151
number, list of values and fields to be used in a sql insert
152
statement to be run on the remote server, and have a comma after each.
153
When the list is complete, I "chop" off the trailing comma
157
stringobj.append("VALUES ('foo', 'fi', 'fo',");
159
stringobj.append(")");
161
In this case, the value of string was:
163
VALUES ('foo', 'fi', 'fo',
164
VALUES ('foo', 'fi', 'fo'
165
VALUES ('foo', 'fi', 'fo')
170
Ptr[str_length--]= '\0';
181
str_length=0; /* Safety */
184
inline bool alloc(uint32 arg_length)
186
if (arg_length < Alloced_length)
188
return real_alloc(arg_length);
190
bool real_alloc(uint32 arg_length); // Empties old string
191
bool realloc(uint32 arg_length);
192
inline void shrink(uint32 arg_length) // Shrink buffer
194
if (arg_length < Alloced_length)
197
if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
200
real_alloc(arg_length);
205
Alloced_length=arg_length;
209
bool is_alloced() { return alloced; }
210
inline String& operator = (const String &s)
215
It is forbidden to do assignments like
216
some_string = substring_of_that_string
218
DBUG_ASSERT(!s.uses_buffer_owned_by(this));
220
Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
226
bool copy(); // Alloc string if not alloced
227
bool copy(const String &s); // Allocate new string
228
bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
229
static bool needs_conversion(uint32 arg_length,
230
CHARSET_INFO *cs_from, CHARSET_INFO *cs_to,
232
bool copy_aligned(const char *s, uint32 arg_length, uint32 offset,
234
bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs);
235
bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom,
236
CHARSET_INFO *csto, uint *errors);
237
bool append(const String &s);
238
bool append(const char *s);
239
bool append(const char *s,uint32 arg_length);
240
bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs);
241
bool append(IO_CACHE* file, uint32 arg_length);
242
bool append_with_prefill(const char *s, uint32 arg_length,
243
uint32 full_length, char fill_char);
244
int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
245
int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
246
bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length);
247
bool replace(uint32 offset,uint32 arg_length,const String &to);
248
inline bool append(char chr)
250
if (str_length < Alloced_length)
252
Ptr[str_length++]=chr;
256
if (realloc(str_length+1))
258
Ptr[str_length++]=chr;
262
bool fill(uint32 max_length,char fill);
264
friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
265
friend int stringcmp(const String *a,const String *b);
266
friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
268
int charpos(int i,uint32 offset=0);
270
int reserve(uint32 space_needed)
272
return realloc(str_length + space_needed);
274
int reserve(uint32 space_needed, uint32 grow_by);
277
The following append operations do NOT check alloced memory
278
q_*** methods writes values of parameters itself
279
qs_*** methods writes string representation of value
281
void q_append(const char c)
283
Ptr[str_length++] = c;
285
void q_append(const uint32 n)
287
int4store(Ptr + str_length, n);
290
void q_append(double d)
292
float8store(Ptr + str_length, d);
295
void q_append(double *d)
297
float8store(Ptr + str_length, *d);
300
void q_append(const char *data, uint32 data_len)
302
memcpy(Ptr + str_length, data, data_len);
303
str_length += data_len;
306
void write_at_position(int position, uint32 value)
308
int4store(Ptr + position,value);
311
void qs_append(const char *str, uint32 len);
312
void qs_append(double d);
313
void qs_append(double *d);
314
inline void qs_append(const char c)
319
void qs_append(int i);
320
void qs_append(uint i);
322
/* Inline (general) functions used by the protocol functions */
324
inline char *prep_append(uint32 arg_length, uint32 step_alloc)
326
uint32 new_length= arg_length + str_length;
327
if (new_length > Alloced_length)
329
if (realloc(new_length + step_alloc))
332
uint32 old_length= str_length;
333
str_length+= arg_length;
334
return Ptr+ old_length; /* Area to use */
337
inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
339
uint32 new_length= arg_length + str_length;
340
if (new_length > Alloced_length && realloc(new_length + step_alloc))
342
memcpy(Ptr+str_length, s, arg_length);
343
str_length+= arg_length;
346
void print(String *print);
348
/* Swap two string objects. Efficient way to exchange data without memcpy. */
349
void swap(String &s);
351
inline bool uses_buffer_owned_by(const String *s) const
353
return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);