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
23
22
/* This file is originally from the mysql distribution. Coded by monty */
25
24
#include <drizzled/common.h>
25
#include <drizzled/util/data_ref.h>
32
#include <drizzled/visibility.h>
31
34
#ifndef NOT_FIXED_DEC
32
35
#define NOT_FIXED_DEC (uint8_t)31
40
extern String my_empty_string;
40
extern DRIZZLED_API String my_empty_string;
41
41
extern const String my_null_string;
42
namespace memory { class Root; }
43
typedef struct charset_info_st CHARSET_INFO;
45
std::string String_to_std_string(String const& s);
46
String* set_String_from_std_string(String* s, std::string const& cs);
48
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
43
int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
49
44
int stringcmp(const String *a,const String *b);
50
45
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
51
size_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
46
size_t well_formed_copy_nchars(const charset_info_st * const to_cs,
52
47
char *to, size_t to_length,
53
const CHARSET_INFO * const from_cs,
48
const charset_info_st * const from_cs,
54
49
const char *from, size_t from_length,
56
51
const char **well_formed_error_pos,
58
53
const char **from_end_pos);
56
class DRIZZLED_API String
64
59
size_t str_length,Alloced_length;
66
const CHARSET_INFO *str_charset;
61
const charset_info_st *str_charset;
70
65
String(size_t length_arg);
71
String(const char *str, const CHARSET_INFO * const cs);
72
String(const char *str, size_t len, const CHARSET_INFO * const cs);
73
String(char *str, size_t len, const CHARSET_INFO * const cs);
74
String(const String &str);
66
String(const char*, const charset_info_st*);
67
String(const char*, size_t, const charset_info_st*);
68
String(char *str, size_t len, const charset_info_st*);
69
String(str_ref, const charset_info_st*);
70
String(const String&);
76
72
static void *operator new(size_t size, memory::Root *mem_root);
77
73
static void operator delete(void *, size_t)
83
inline void set_charset(const CHARSET_INFO * const charset_arg)
79
inline void set_charset(const charset_info_st * const charset_arg)
84
80
{ str_charset= charset_arg; }
85
inline const CHARSET_INFO *charset() const { return str_charset; }
81
inline const charset_info_st *charset() const { return str_charset; }
86
82
inline size_t length() const { return str_length;}
87
83
inline size_t alloced_length() const { return Alloced_length;}
88
84
inline char& operator [] (size_t i) const { return Ptr[i]; }
89
inline void length(size_t len) { str_length=len ; }
90
inline bool is_empty() { return (str_length == 0); }
91
inline void mark_as_const() { Alloced_length= 0;}
85
inline void length(size_t len) { str_length=len; }
86
inline bool empty() const { return str_length == 0; }
87
inline void mark_as_const() { Alloced_length= 0; }
92
88
inline char *ptr() { return Ptr; }
93
89
inline const char *ptr() const { return Ptr; }
94
90
inline char *c_ptr()
96
92
if (str_length == Alloced_length)
97
(void) realloc(str_length);
99
95
Ptr[str_length]= 0;
103
inline char *c_ptr_quick()
105
if (Ptr && str_length < Alloced_length)
109
inline char *c_ptr_safe()
111
if (Ptr && str_length < Alloced_length)
114
(void) realloc(str_length);
119
if (Ptr && str_length < Alloced_length)
122
(void) realloc(str_length);
99
inline const char* begin() const
103
inline const char* end() const
105
return begin() + size();
107
inline const char* data() const
111
inline size_t size() const
115
inline const char* c_str()
117
if (Ptr && str_length < Alloced_length)
125
123
void append_identifier(const char *name, size_t length);
124
void append_identifier(str_ref);
127
126
void set(String &str,size_t offset,size_t arg_length)
135
134
Alloced_length=0;
136
135
str_charset=str.str_charset;
138
inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
137
inline void set(char *str,size_t arg_length, const charset_info_st * const cs)
141
140
Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
144
inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
144
inline void set(const char *str,size_t arg_length, const charset_info_st * const cs)
147
147
Ptr= const_cast<char*>(str);
148
148
str_length=arg_length; Alloced_length=0 ; alloced=0;
151
bool set_ascii(const char *str, size_t arg_length);
152
inline void set_quick(char *str,size_t arg_length, const CHARSET_INFO * const cs)
151
void set_ascii(const char *str, size_t arg_length);
153
inline void set_quick(char *str,size_t arg_length, const charset_info_st * const cs)
160
bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
161
bool set(int64_t num, const CHARSET_INFO * const cs)
162
{ return set_int(num, false, cs); }
163
bool set(uint64_t num, const CHARSET_INFO * const cs)
164
{ return set_int(static_cast<int64_t>(num), true, cs); }
165
bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
162
void set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs);
163
void set(int64_t num, const charset_info_st * const cs)
164
{ set_int(num, false, cs); }
165
void set(uint64_t num, const charset_info_st * const cs)
166
{ set_int(static_cast<int64_t>(num), true, cs); }
167
void set_real(double num,size_t decimals, const charset_info_st* cs);
202
204
str_length=0; /* Safety */
205
inline bool alloc(size_t arg_length)
207
inline void alloc(size_t arg_length)
207
if (arg_length < Alloced_length)
209
return real_alloc(arg_length);
209
if (arg_length >= Alloced_length)
210
real_alloc(arg_length);
211
bool real_alloc(size_t arg_length); // Empties old string
212
bool realloc(size_t arg_length);
212
void real_alloc(size_t arg_length); // Empties old string
213
void realloc(size_t arg_length);
213
214
inline void shrink(size_t arg_length) // Shrink buffer
215
216
if (arg_length < Alloced_length)
247
bool copy(); // Alloc string if not alloced
248
bool copy(const String &s); // Allocate new string
249
bool copy(const char *s,size_t arg_length, const CHARSET_INFO * const cs); // Allocate new string
250
static bool needs_conversion(size_t arg_length,
251
const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
253
bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO * const cs);
254
bool copy(const char*s,size_t arg_length, const CHARSET_INFO * const csfrom,
255
const CHARSET_INFO * const csto, size_t *errors);
256
bool append(const String &s);
257
bool append(const char *s);
258
bool append(const char *s,size_t arg_length);
259
bool append(const char *s,size_t arg_length, const CHARSET_INFO * const cs);
260
bool append_with_prefill(const char *s, size_t arg_length,
261
size_t full_length, char fill_char);
248
void copy(); // Alloc string if not alloced
249
void copy(const String&); // Allocate new string
250
void copy(const std::string&, const charset_info_st*); // Allocate new string
251
void copy(const char*, size_t, const charset_info_st*); // Allocate new string
252
static bool needs_conversion(size_t arg_length, const charset_info_st* cs_from, const charset_info_st* cs_to);
253
void set_or_copy_aligned(const char *s, size_t arg_length, const charset_info_st*);
254
void copy(const char*s,size_t arg_length, const charset_info_st& csto);
255
void append(const char*);
256
void append(const char*, size_t);
257
void append(str_ref);
258
void append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char);
262
259
int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
263
260
int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
264
bool replace(size_t offset,size_t arg_length,const char *to,size_t length);
265
bool replace(size_t offset,size_t arg_length,const String &to);
266
inline bool append(char chr)
261
void replace(size_t offset,size_t arg_length,const char *to,size_t length);
262
void replace(size_t offset,size_t arg_length,const String &to);
264
inline void append(char chr)
268
266
if (str_length < Alloced_length)
274
if (realloc(str_length+1))
272
realloc(str_length+1);
276
273
Ptr[str_length++]=chr;
280
friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
276
friend int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
281
277
friend int stringcmp(const String *a,const String *b);
282
278
friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
284
int charpos(int i,size_t offset=0);
279
size_t numchars() const;
280
int charpos(int i, size_t offset= 0) const;
286
int reserve(size_t space_needed)
282
void reserve(size_t space_needed)
288
return realloc(str_length + space_needed);
284
realloc(str_length + space_needed);
290
int reserve(size_t space_needed, size_t grow_by);
293
The following append operations do NOT check alloced memory
294
q_*** methods writes values of parameters itself
295
qs_*** methods writes string representation of value
297
void q_append(const char c);
298
void q_append(const size_t n);
299
void q_append(double d);
300
void q_append(double *d);
301
void q_append(const char *data, size_t data_len);
302
void write_at_position(int position, size_t value);
304
/* Inline (general) functions used by the protocol functions */
306
inline char *prep_append(size_t arg_length, size_t step_alloc)
286
void reserve(size_t space_needed, size_t grow_by);
288
inline void append(const char *s, size_t arg_length, size_t step_alloc)
308
290
size_t new_length= arg_length + str_length;
309
291
if (new_length > Alloced_length)
311
if (realloc(new_length + step_alloc))
314
size_t old_length= str_length;
315
str_length+= arg_length;
316
return Ptr+ old_length; /* Area to use */
319
inline bool append(const char *s, size_t arg_length, size_t step_alloc)
321
size_t new_length= arg_length + str_length;
322
if (new_length > Alloced_length && realloc(new_length + step_alloc))
292
realloc(new_length + step_alloc);
324
293
memcpy(Ptr+str_length, s, arg_length);
325
294
str_length+= arg_length;
328
void print(String *print);
297
void print(String&) const;
330
299
/* Swap two string objects. Efficient way to exchange data without memcpy. */
331
300
void swap(String &s);
339
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
308
bool check_if_only_end_space(const charset_info_st* const, char *str, char *end);
310
std::ostream& operator<<(std::ostream&, const String&);
342
312
} /* namespace drizzled */
344
314
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
345
315
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
348
#endif /* DRIZZLED_SQL_STRING_H */