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
20
#ifndef DRIZZLE_SERVER_SQL_STRING_H
21
#define DRIZZLE_SERVER_SQL_STRING_H
23
23
/* This file is originally from the mysql distribution. Coded by monty */
27
#define NOT_FIXED_DEC 31
25
30
#include <drizzled/common.h>
32
#include "drizzled/visibility.h"
35
#define NOT_FIXED_DEC (uint8_t)31
31
#include <mysys/iocache.h>
43
extern DRIZZLED_API String my_empty_string;
44
extern const String my_null_string;
45
namespace memory { class Root; }
46
typedef struct charset_info_st CHARSET_INFO;
48
DRIZZLED_API std::string String_to_std_string(String const& s);
49
DRIZZLED_API String* set_String_from_std_string(String* s, std::string const& cs);
51
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
52
int stringcmp(const String *a,const String *b);
53
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
54
size_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
55
char *to, size_t to_length,
56
const CHARSET_INFO * const from_cs,
57
const char *from, size_t from_length,
59
const char **well_formed_error_pos,
60
const char **cannot_convert_error_pos,
61
const char **from_end_pos);
64
class DRIZZLED_API String
37
#if defined(__cplusplus)
41
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
42
int stringcmp(const String *a,const String *b);
43
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
44
uint32_t copy_and_convert(char *to, uint32_t to_length,
45
const CHARSET_INFO * const to_cs,
46
const char *from, uint32_t from_length,
47
const CHARSET_INFO * const from_cs,
49
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
50
char *to, uint32_t to_length,
51
const CHARSET_INFO * const from_cs,
52
const char *from, uint32_t from_length,
54
const char **well_formed_error_pos,
55
const char **cannot_convert_error_pos,
56
const char **from_end_pos);
57
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
58
char *dst, size_t dstlen,
59
const char *src, size_t srclen);
61
#if defined(__cplusplus)
67
size_t str_length,Alloced_length;
68
uint32_t str_length,Alloced_length;
69
70
const CHARSET_INFO *str_charset;
73
String(size_t length_arg);
74
String(const char *str, const CHARSET_INFO * const cs);
75
String(const char *str, size_t len, const CHARSET_INFO * const cs);
76
String(char *str, size_t len, const CHARSET_INFO * const cs);
77
String(const String &str);
79
static void *operator new(size_t size, memory::Root *mem_root);
75
Ptr=0; str_length=Alloced_length=0; alloced=0;
76
str_charset= &my_charset_bin;
78
String(uint32_t length_arg)
80
alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
81
str_charset= &my_charset_bin;
83
String(const char *str, const CHARSET_INFO * const cs)
85
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
88
String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
90
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
93
String(char *str,uint32_t len, const CHARSET_INFO * const cs)
95
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
98
String(const String &str)
100
Ptr=str.Ptr ; str_length=str.str_length ;
101
Alloced_length=str.Alloced_length; alloced=0;
102
str_charset=str.str_charset;
104
static void *operator new(size_t size, MEM_ROOT *mem_root)
105
{ return (void*) alloc_root(mem_root, (uint) size); }
80
106
static void operator delete(void *, size_t)
82
static void operator delete(void *, memory::Root *)
107
{ TRASH(ptr_arg, size); }
108
static void operator delete(void *, MEM_ROOT *)
109
{ /* never called */ }
110
~String() { free(); }
86
112
inline void set_charset(const CHARSET_INFO * const charset_arg)
87
113
{ str_charset= charset_arg; }
88
114
inline const CHARSET_INFO *charset() const { return str_charset; }
89
inline size_t length() const { return str_length;}
90
inline size_t alloced_length() const { return Alloced_length;}
91
inline char& operator [] (size_t i) const { return Ptr[i]; }
92
inline void length(size_t len) { str_length=len ; }
115
inline uint32_t length() const { return str_length;}
116
inline uint32_t alloced_length() const { return Alloced_length;}
117
inline char& operator [] (uint32_t i) const { return Ptr[i]; }
118
inline void length(uint32_t len) { str_length=len ; }
93
119
inline bool is_empty() { return (str_length == 0); }
94
120
inline void mark_as_const() { Alloced_length= 0;}
95
121
inline char *ptr() { return Ptr; }
96
122
inline const char *ptr() const { return Ptr; }
97
123
inline char *c_ptr()
99
if (str_length == Alloced_length)
125
if (!Ptr || Ptr[str_length]) /* Should be safe */
100
126
(void) realloc(str_length);
106
129
inline char *c_ptr_quick()
117
140
(void) realloc(str_length);
122
if (Ptr && str_length < Alloced_length)
125
(void) realloc(str_length);
128
void append_identifier(const char *name, size_t length);
143
void append_identifier(const char *name, uint32_t length);
130
void set(String &str,size_t offset,size_t arg_length)
145
void set(String &str,uint32_t offset,uint32_t arg_length)
132
147
assert(&str != this);
134
Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
149
Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
135
150
if (str.Alloced_length)
136
151
Alloced_length=str.Alloced_length-offset;
138
153
Alloced_length=0;
139
154
str_charset=str.str_charset;
141
inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
144
Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
147
inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
150
Ptr= const_cast<char*>(str);
151
str_length=arg_length; Alloced_length=0 ; alloced=0;
154
bool set_ascii(const char *str, size_t arg_length);
155
inline void set_quick(char *str,size_t arg_length, const CHARSET_INFO * const cs)
156
inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
159
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
162
inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
165
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
168
bool set_ascii(const char *str, uint32_t arg_length);
169
inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
159
Ptr= str; str_length= Alloced_length= arg_length;
173
Ptr=(char*) str; str_length=Alloced_length=arg_length;
163
177
bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
164
178
bool set(int64_t num, const CHARSET_INFO * const cs)
165
179
{ return set_int(num, false, cs); }
166
180
bool set(uint64_t num, const CHARSET_INFO * const cs)
167
{ return set_int(static_cast<int64_t>(num), true, cs); }
168
bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
181
{ return set_int((int64_t)num, true, cs); }
182
bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
250
264
bool copy(); // Alloc string if not alloced
251
265
bool copy(const String &s); // Allocate new string
252
bool copy(const std::string&, const CHARSET_INFO * const cs); // Allocate new string
253
bool copy(const char *s,size_t arg_length, const CHARSET_INFO * const cs); // Allocate new string
254
static bool needs_conversion(size_t arg_length,
266
bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs); // Allocate new string
267
static bool needs_conversion(uint32_t arg_length,
255
268
const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
257
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO * const cs);
258
bool copy(const char*s,size_t arg_length, const CHARSET_INFO * const csfrom,
259
const CHARSET_INFO * const csto, size_t *errors);
270
bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
271
const CHARSET_INFO * const cs);
272
bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
273
bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
274
const CHARSET_INFO * const csto, uint32_t *errors);
260
275
bool append(const String &s);
261
276
bool append(const char *s);
262
bool append(const char *s,size_t arg_length);
263
bool append(const char *s,size_t arg_length, const CHARSET_INFO * const cs);
264
bool append_with_prefill(const char *s, size_t arg_length,
265
size_t full_length, char fill_char);
266
int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
267
int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
268
bool replace(size_t offset,size_t arg_length,const char *to,size_t length);
269
bool replace(size_t offset,size_t arg_length,const String &to);
277
bool append(const char *s,uint32_t arg_length);
278
bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
279
bool append(IO_CACHE* file, uint32_t arg_length);
280
bool append_with_prefill(const char *s, uint32_t arg_length,
281
uint32_t full_length, char fill_char);
282
int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
283
int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
284
bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
285
bool replace(uint32_t offset,uint32_t arg_length,const String &to);
270
286
inline bool append(char chr)
272
288
if (str_length < Alloced_length)
278
294
if (realloc(str_length+1))
280
296
Ptr[str_length++]=chr;
300
bool fill(uint32_t max_length,char fill);
284
301
friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
285
302
friend int stringcmp(const String *a,const String *b);
286
friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
288
int charpos(int i,size_t offset=0);
303
friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
305
int charpos(int i,uint32_t offset=0);
290
int reserve(size_t space_needed)
307
int reserve(uint32_t space_needed)
292
309
return realloc(str_length + space_needed);
294
int reserve(size_t space_needed, size_t grow_by);
311
int reserve(uint32_t space_needed, uint32_t grow_by);
297
314
The following append operations do NOT check alloced memory
298
315
q_*** methods writes values of parameters itself
299
316
qs_*** methods writes string representation of value
301
void q_append(const char c);
302
void q_append(const size_t n);
303
void q_append(double d);
304
void q_append(double *d);
305
void q_append(const char *data, size_t data_len);
306
void write_at_position(int position, size_t value);
318
void q_append(const char c)
320
Ptr[str_length++] = c;
322
void q_append(const uint32_t n)
324
int4store(Ptr + str_length, n);
327
void q_append(double d)
329
float8store(Ptr + str_length, d);
332
void q_append(double *d)
334
float8store(Ptr + str_length, *d);
337
void q_append(const char *data, uint32_t data_len)
339
memcpy(Ptr + str_length, data, data_len);
340
str_length += data_len;
343
void write_at_position(int position, uint32_t value)
345
int4store(Ptr + position,value);
348
void qs_append(const char *str, uint32_t len);
349
void qs_append(double d);
350
void qs_append(double *d);
351
inline void qs_append(const char c)
356
void qs_append(int i);
357
void qs_append(uint32_t i);
308
359
/* Inline (general) functions used by the protocol functions */
310
inline char *prep_append(size_t arg_length, size_t step_alloc)
361
inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
312
size_t new_length= arg_length + str_length;
363
uint32_t new_length= arg_length + str_length;
313
364
if (new_length > Alloced_length)
315
366
if (realloc(new_length + step_alloc))
318
size_t old_length= str_length;
369
uint32_t old_length= str_length;
319
370
str_length+= arg_length;
320
371
return Ptr+ old_length; /* Area to use */
323
inline bool append(const char *s, size_t arg_length, size_t step_alloc)
374
inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
325
size_t new_length= arg_length + str_length;
376
uint32_t new_length= arg_length + str_length;
326
377
if (new_length > Alloced_length && realloc(new_length + step_alloc))
328
379
memcpy(Ptr+str_length, s, arg_length);