~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Mark Atwood
  • Date: 2011-08-12 04:08:33 UTC
  • mfrom: (2385.2.17 refactor5)
  • Revision ID: me@mark.atwood.name-20110812040833-u6j85nc6ahuc0dtz
mergeĀ lp:~olafvdspek/drizzle/refactor5

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#ifndef DRIZZLED_SQL_STRING_H
21
 
#define DRIZZLED_SQL_STRING_H
 
20
#pragma once
22
21
 
23
22
/* This file is originally from the mysql distribution. Coded by monty */
24
23
 
25
24
#include <drizzled/common.h>
 
25
#include <drizzled/util/data_ref.h>
26
26
 
27
27
#include <cassert>
28
28
#include <cstdlib>
35
35
#define NOT_FIXED_DEC                   (uint8_t)31
36
36
#endif
37
37
 
38
 
namespace drizzled
39
 
{
40
 
 
41
 
class String;
 
38
namespace drizzled {
42
39
 
43
40
extern DRIZZLED_API String my_empty_string;
44
41
extern const String my_null_string;
45
 
namespace memory { class Root; }
46
 
typedef struct charset_info_st CHARSET_INFO;
47
 
 
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);
50
 
 
51
 
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
42
 
 
43
int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
52
44
int stringcmp(const String *a,const String *b);
53
45
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,
 
46
size_t well_formed_copy_nchars(const charset_info_st * const to_cs,
55
47
                                 char *to, size_t to_length,
56
 
                                 const CHARSET_INFO * const from_cs,
 
48
                                 const charset_info_st * const from_cs,
57
49
                                 const char *from, size_t from_length,
58
50
                                 size_t nchars,
59
51
                                 const char **well_formed_error_pos,
66
58
  char *Ptr;
67
59
  size_t str_length,Alloced_length;
68
60
  bool alloced;
69
 
  const CHARSET_INFO *str_charset;
 
61
  const charset_info_st *str_charset;
70
62
 
71
63
public:
72
64
  String();
73
65
  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);
 
66
  String(const char *str, const charset_info_st * const cs);
 
67
  String(const char *str, size_t len, const charset_info_st * const cs);
 
68
  String(char *str, size_t len, const charset_info_st * const cs);
77
69
  String(const String &str);
78
70
 
79
71
  static void *operator new(size_t size, memory::Root *mem_root);
83
75
  { }
84
76
  ~String();
85
77
 
86
 
  inline void set_charset(const CHARSET_INFO * const charset_arg)
 
78
  inline void set_charset(const charset_info_st * const charset_arg)
87
79
  { str_charset= charset_arg; }
88
 
  inline const CHARSET_INFO *charset() const { return str_charset; }
 
80
  inline const charset_info_st *charset() const { return str_charset; }
89
81
  inline size_t length() const { return str_length;}
90
82
  inline size_t alloced_length() const { return Alloced_length;}
91
83
  inline char& operator [] (size_t i) const { return Ptr[i]; }
92
 
  inline void length(size_t len) { str_length=len ; }
93
 
  inline bool is_empty() { return (str_length == 0); }
94
 
  inline void mark_as_const() { Alloced_length= 0;}
 
84
  inline void length(size_t len) { str_length=len; }
 
85
  inline bool empty() const { return str_length == 0; }
 
86
  inline void mark_as_const() { Alloced_length= 0; }
95
87
  inline char *ptr() { return Ptr; }
96
88
  inline const char *ptr() const { return Ptr; }
97
89
  inline char *c_ptr()
98
90
  {
99
91
    if (str_length == Alloced_length)
100
 
      (void) realloc(str_length);
 
92
      realloc(str_length);
101
93
    else
102
94
      Ptr[str_length]= 0;
103
95
 
104
96
    return Ptr;
105
97
  }
106
 
  inline char *c_ptr_quick()
107
 
  {
108
 
    if (Ptr && str_length < Alloced_length)
109
 
      Ptr[str_length]=0;
110
 
    return Ptr;
111
 
  }
112
 
  inline char *c_ptr_safe()
113
 
  {
114
 
    if (Ptr && str_length < Alloced_length)
115
 
      Ptr[str_length]=0;
116
 
    else
117
 
      (void) realloc(str_length);
118
 
    return Ptr;
119
 
  }
120
 
  inline char *c_str()
121
 
  {
122
 
    if (Ptr && str_length < Alloced_length)
123
 
      Ptr[str_length]=0;
124
 
    else
125
 
      (void) realloc(str_length);
 
98
  inline const char* begin() const
 
99
  {
 
100
    return Ptr;
 
101
  }
 
102
  inline const char* end() const
 
103
  {
 
104
    return begin() + size();
 
105
  }
 
106
  inline const char* data() const
 
107
  {
 
108
    return Ptr;
 
109
  }
 
110
  inline size_t size() const
 
111
  {
 
112
    return length();
 
113
  }
 
114
  inline const char* c_str()
 
115
  {
 
116
    if (Ptr && str_length < Alloced_length)
 
117
      Ptr[str_length]=0;
 
118
    else
 
119
      realloc(str_length);
126
120
    return Ptr;
127
121
  }
128
122
  void append_identifier(const char *name, size_t length);
 
123
  void append_identifier(str_ref);
129
124
 
130
125
  void set(String &str,size_t offset,size_t arg_length)
131
126
  {
138
133
      Alloced_length=0;
139
134
    str_charset=str.str_charset;
140
135
  }
141
 
  inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
136
  inline void set(char *str,size_t arg_length, const charset_info_st * const cs)
142
137
  {
143
138
    free();
144
139
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
145
140
    str_charset=cs;
146
141
  }
147
 
  inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
142
 
 
143
  inline void set(const char *str,size_t arg_length, const charset_info_st * const cs)
148
144
  {
149
145
    free();
150
146
    Ptr= const_cast<char*>(str);
151
147
    str_length=arg_length; Alloced_length=0 ; alloced=0;
152
148
    str_charset=cs;
153
149
  }
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)
 
150
  void set_ascii(const char *str, size_t arg_length);
 
151
 
 
152
  inline void set_quick(char *str,size_t arg_length, const charset_info_st * const cs)
156
153
  {
157
154
    if (!alloced)
158
155
    {
160
157
    }
161
158
    str_charset= cs;
162
159
  }
163
 
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
164
 
  bool set(int64_t num, const CHARSET_INFO * const cs)
165
 
  { return set_int(num, false, cs); }
166
 
  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);
 
160
 
 
161
  void set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs);
 
162
  void set(int64_t num, const charset_info_st * const cs)
 
163
  { set_int(num, false, cs); }
 
164
  void set(uint64_t num, const charset_info_st * const cs)
 
165
  { set_int(static_cast<int64_t>(num), true, cs); }
 
166
  void set_real(double num,size_t decimals, const charset_info_st* cs);
169
167
 
170
168
  /*
171
169
    PMG 2004.11.12
205
203
      str_length=0;                             /* Safety */
206
204
    }
207
205
  }
208
 
  inline bool alloc(size_t arg_length)
 
206
  inline void alloc(size_t arg_length)
209
207
  {
210
 
    if (arg_length < Alloced_length)
211
 
      return 0;
212
 
    return real_alloc(arg_length);
 
208
    if (arg_length >= Alloced_length)
 
209
      real_alloc(arg_length);
213
210
  }
214
 
  bool real_alloc(size_t arg_length);                   // Empties old string
215
 
  bool realloc(size_t arg_length);
 
211
  void real_alloc(size_t arg_length);                   // Empties old string
 
212
  void realloc(size_t arg_length);
216
213
  inline void shrink(size_t arg_length)         // Shrink buffer
217
214
  {
218
215
    if (arg_length < Alloced_length)
230
227
      }
231
228
    }
232
229
  }
233
 
  bool is_alloced() { return alloced; }
 
230
  bool is_alloced() { return alloced; } const
234
231
  inline String& operator = (const String &s)
235
232
  {
236
233
    if (&s != this)
247
244
    return *this;
248
245
  }
249
246
 
250
 
  bool copy();                                  // Alloc string if not alloced
251
 
  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,
255
 
                               const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
256
 
                               size_t *offset);
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);
260
 
  bool append(const String &s);
261
 
  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);
 
247
  void copy();                                  // Alloc string if not alloced
 
248
  void copy(const String&);                     // Allocate new string
 
249
  void copy(const std::string&, const charset_info_st*);        // Allocate new string
 
250
  void copy(const char*, size_t, const charset_info_st*); // Allocate new string
 
251
  static bool needs_conversion(size_t arg_length, const charset_info_st* cs_from, const charset_info_st* cs_to);
 
252
  void set_or_copy_aligned(const char *s, size_t arg_length, const charset_info_st*);
 
253
  void copy(const char*s,size_t arg_length, const charset_info_st& csto);
 
254
  void append(const char*);
 
255
  void append(const char*, size_t);
 
256
  void append(str_ref);
 
257
  void append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char);
266
258
  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
267
259
  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);
270
 
  inline bool append(char chr)
 
260
  void replace(size_t offset,size_t arg_length,const char *to,size_t length);
 
261
  void replace(size_t offset,size_t arg_length,const String &to);
 
262
 
 
263
  inline void append(char chr)
271
264
  {
272
265
    if (str_length < Alloced_length)
273
266
    {
275
268
    }
276
269
    else
277
270
    {
278
 
      if (realloc(str_length+1))
279
 
        return 1;
 
271
      realloc(str_length+1);
280
272
      Ptr[str_length++]=chr;
281
273
    }
282
 
    return 0;
283
274
  }
284
 
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
275
  friend int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
285
276
  friend int stringcmp(const String *a,const String *b);
286
277
  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
287
 
  size_t numchars();
288
 
  int charpos(int i,size_t offset=0);
 
278
  size_t numchars() const;
 
279
  int charpos(int i, size_t offset= 0) const;
289
280
 
290
 
  int reserve(size_t space_needed)
 
281
  void reserve(size_t space_needed)
291
282
  {
292
 
    return realloc(str_length + space_needed);
 
283
    realloc(str_length + space_needed);
293
284
  }
294
 
  int reserve(size_t space_needed, size_t grow_by);
295
 
 
296
 
  /*
297
 
    The following append operations do NOT check alloced memory
298
 
    q_*** methods writes values of parameters itself
299
 
    qs_*** methods writes string representation of value
300
 
  */
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);
307
 
 
308
 
  /* Inline (general) functions used by the protocol functions */
309
 
 
310
 
  inline char *prep_append(size_t arg_length, size_t step_alloc)
 
285
  void reserve(size_t space_needed, size_t grow_by);
 
286
 
 
287
  inline void append(const char *s, size_t arg_length, size_t step_alloc)
311
288
  {
312
289
    size_t new_length= arg_length + str_length;
313
290
    if (new_length > Alloced_length)
314
 
    {
315
 
      if (realloc(new_length + step_alloc))
316
 
        return 0;
317
 
    }
318
 
    size_t old_length= str_length;
319
 
    str_length+= arg_length;
320
 
    return Ptr+ old_length;                     /* Area to use */
321
 
  }
322
 
 
323
 
  inline bool append(const char *s, size_t arg_length, size_t step_alloc)
324
 
  {
325
 
    size_t new_length= arg_length + str_length;
326
 
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
327
 
      return true;
 
291
                        realloc(new_length + step_alloc);
328
292
    memcpy(Ptr+str_length, s, arg_length);
329
293
    str_length+= arg_length;
330
 
    return false;
331
294
  }
332
 
  void print(String *print);
 
295
 
 
296
  void print(String&) const;
333
297
 
334
298
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
335
299
  void swap(String &s);
340
304
  }
341
305
};
342
306
 
343
 
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
344
 
                             char *end);
 
307
bool check_if_only_end_space(const charset_info_st* const, char *str, char *end);
345
308
 
346
 
std::ostream& operator<<(std::ostream& output, const String &str);
 
309
std::ostream& operator<<(std::ostream&, const String&);
347
310
 
348
311
} /* namespace drizzled */
349
312
 
350
313
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
351
314
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
352
 
 
353
 
 
354
 
#endif /* DRIZZLED_SQL_STRING_H */