17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef DRIZZLED_SQL_STRING_H
21
#define DRIZZLED_SQL_STRING_H
22
23
/* This file is originally from the mysql distribution. Coded by monty */
24
25
#include <drizzled/common.h>
31
#include <drizzled/visibility.h>
31
#include "drizzled/visibility.h"
33
33
#ifndef NOT_FIXED_DEC
34
34
#define NOT_FIXED_DEC (uint8_t)31
39
42
extern DRIZZLED_API String my_empty_string;
40
43
extern const String my_null_string;
42
int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
44
namespace memory { class Root; }
45
typedef struct charset_info_st CHARSET_INFO;
47
DRIZZLED_API std::string String_to_std_string(String const& s);
48
DRIZZLED_API String* set_String_from_std_string(String* s, std::string const& cs);
50
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
43
51
int stringcmp(const String *a,const String *b);
44
52
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
45
size_t well_formed_copy_nchars(const charset_info_st * const to_cs,
53
size_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
46
54
char *to, size_t to_length,
47
const charset_info_st * const from_cs,
55
const CHARSET_INFO * const from_cs,
48
56
const char *from, size_t from_length,
50
58
const char **well_formed_error_pos,
58
66
size_t str_length,Alloced_length;
60
const charset_info_st *str_charset;
68
const CHARSET_INFO *str_charset;
64
72
String(size_t length_arg);
65
String(const char *str, const charset_info_st * const cs);
66
String(const char *str, size_t len, const charset_info_st * const cs);
67
String(char *str, size_t len, const charset_info_st * const cs);
73
String(const char *str, const CHARSET_INFO * const cs);
74
String(const char *str, size_t len, const CHARSET_INFO * const cs);
75
String(char *str, size_t len, const CHARSET_INFO * const cs);
68
76
String(const String &str);
70
78
static void *operator new(size_t size, memory::Root *mem_root);
77
inline void set_charset(const charset_info_st * const charset_arg)
85
inline void set_charset(const CHARSET_INFO * const charset_arg)
78
86
{ str_charset= charset_arg; }
79
inline const charset_info_st *charset() const { return str_charset; }
87
inline const CHARSET_INFO *charset() const { return str_charset; }
80
88
inline size_t length() const { return str_length;}
81
89
inline size_t alloced_length() const { return Alloced_length;}
82
90
inline char& operator [] (size_t i) const { return Ptr[i]; }
129
137
Alloced_length=0;
130
138
str_charset=str.str_charset;
132
inline void set(char *str,size_t arg_length, const charset_info_st * const cs)
140
inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
135
143
Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
138
inline void set(const char *str,size_t arg_length, const charset_info_st * const cs)
146
inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
141
149
Ptr= const_cast<char*>(str);
142
150
str_length=arg_length; Alloced_length=0 ; alloced=0;
145
void set_ascii(const char *str, size_t arg_length);
146
inline void set_quick(char *str,size_t arg_length, const charset_info_st * const cs)
153
bool set_ascii(const char *str, size_t arg_length);
154
inline void set_quick(char *str,size_t arg_length, const CHARSET_INFO * const cs)
154
void set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs);
155
void set(int64_t num, const charset_info_st * const cs)
156
{ set_int(num, false, cs); }
157
void set(uint64_t num, const charset_info_st * const cs)
158
{ set_int(static_cast<int64_t>(num), true, cs); }
159
void set_real(double num,size_t decimals, const charset_info_st* cs);
162
bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
163
bool set(int64_t num, const CHARSET_INFO * const cs)
164
{ return set_int(num, false, cs); }
165
bool set(uint64_t num, const CHARSET_INFO * const cs)
166
{ return set_int(static_cast<int64_t>(num), true, cs); }
167
bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
196
204
str_length=0; /* Safety */
199
inline void alloc(size_t arg_length)
207
inline bool alloc(size_t arg_length)
201
if (arg_length >= Alloced_length)
202
real_alloc(arg_length);
209
if (arg_length < Alloced_length)
211
return real_alloc(arg_length);
204
void real_alloc(size_t arg_length); // Empties old string
205
void realloc(size_t arg_length);
213
bool real_alloc(size_t arg_length); // Empties old string
214
bool realloc(size_t arg_length);
206
215
inline void shrink(size_t arg_length) // Shrink buffer
208
217
if (arg_length < Alloced_length)
240
void copy(); // Alloc string if not alloced
241
void copy(const String&); // Allocate new string
242
void copy(const std::string&, const charset_info_st*); // Allocate new string
243
void copy(const char*, size_t, const charset_info_st*); // Allocate new string
249
bool copy(); // Alloc string if not alloced
250
bool copy(const String &s); // Allocate new string
251
bool copy(const std::string&, const CHARSET_INFO * const cs); // Allocate new string
252
bool copy(const char *s,size_t arg_length, const CHARSET_INFO * const cs); // Allocate new string
244
253
static bool needs_conversion(size_t arg_length,
245
const charset_info_st* cs_from, const charset_info_st* cs_to,
254
const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
247
void set_or_copy_aligned(const char *s, size_t arg_length, const charset_info_st*);
248
void copy(const char*s,size_t arg_length, const charset_info_st& csto);
249
void append(const String &s);
250
void append(const char *s);
251
void append(const char *s,size_t arg_length);
252
void append(const char *s,size_t arg_length, const charset_info_st * const cs);
253
void append_with_prefill(const char *s, size_t arg_length,
256
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO * const cs);
257
bool copy(const char*s,size_t arg_length, const CHARSET_INFO * const csfrom,
258
const CHARSET_INFO * const csto, size_t *errors);
259
bool append(const String &s);
260
bool append(const char *s);
261
bool append(const char *s,size_t arg_length);
262
bool append(const char *s,size_t arg_length, const CHARSET_INFO * const cs);
263
bool append_with_prefill(const char *s, size_t arg_length,
254
264
size_t full_length, char fill_char);
255
265
int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
256
266
int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
257
void replace(size_t offset,size_t arg_length,const char *to,size_t length);
258
void replace(size_t offset,size_t arg_length,const String &to);
260
inline void append(char chr)
267
bool replace(size_t offset,size_t arg_length,const char *to,size_t length);
268
bool replace(size_t offset,size_t arg_length,const String &to);
269
inline bool append(char chr)
262
271
if (str_length < Alloced_length)
268
realloc(str_length+1);
277
if (realloc(str_length+1))
269
279
Ptr[str_length++]=chr;
272
friend int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
283
friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
273
284
friend int stringcmp(const String *a,const String *b);
274
285
friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
275
286
size_t numchars();
276
287
int charpos(int i,size_t offset=0);
278
void reserve(size_t space_needed)
289
int reserve(size_t space_needed)
280
realloc(str_length + space_needed);
291
return realloc(str_length + space_needed);
282
void reserve(size_t space_needed, size_t grow_by);
293
int reserve(size_t space_needed, size_t grow_by);
285
296
The following append operations do NOT check alloced memory
300
311
size_t new_length= arg_length + str_length;
301
312
if (new_length > Alloced_length)
302
realloc(new_length + step_alloc);
314
if (realloc(new_length + step_alloc))
303
317
size_t old_length= str_length;
304
318
str_length+= arg_length;
305
319
return Ptr+ old_length; /* Area to use */
308
inline void append(const char *s, size_t arg_length, size_t step_alloc)
322
inline bool append(const char *s, size_t arg_length, size_t step_alloc)
310
324
size_t new_length= arg_length + str_length;
311
if (new_length > Alloced_length)
312
realloc(new_length + step_alloc);
325
if (new_length > Alloced_length && realloc(new_length + step_alloc))
313
327
memcpy(Ptr+str_length, s, arg_length);
314
328
str_length+= arg_length;
317
331
void print(String *print);
319
333
/* Swap two string objects. Efficient way to exchange data without memcpy. */