~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Andrew Hutchings
  • Date: 2011-02-01 10:23:22 UTC
  • mto: (2136.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2137.
  • Revision ID: andrew@linuxjedi.co.uk-20110201102322-oxztcyrjzg3c7yta
Fix counters cleanup

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
 
#pragma once
 
20
#ifndef DRIZZLED_SQL_STRING_H
 
21
#define DRIZZLED_SQL_STRING_H
21
22
 
22
23
/* This file is originally from the mysql distribution. Coded by monty */
23
24
 
24
25
#include <drizzled/common.h>
25
 
 
26
26
#include <cassert>
27
27
#include <cstdlib>
28
28
#include <cstring>
29
29
#include <string>
30
30
 
31
 
#include <drizzled/visibility.h>
 
31
#include "drizzled/visibility.h"
32
32
 
33
33
#ifndef NOT_FIXED_DEC
34
34
#define NOT_FIXED_DEC                   (uint8_t)31
35
35
#endif
36
36
 
37
 
namespace drizzled {
 
37
namespace drizzled
 
38
{
 
39
 
 
40
class String;
38
41
 
39
42
extern DRIZZLED_API String my_empty_string;
40
43
extern const String my_null_string;
41
 
 
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;
 
46
 
 
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);
 
49
 
 
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,
49
57
                                 size_t nchars,
50
58
                                 const char **well_formed_error_pos,
57
65
  char *Ptr;
58
66
  size_t str_length,Alloced_length;
59
67
  bool alloced;
60
 
  const charset_info_st *str_charset;
 
68
  const CHARSET_INFO *str_charset;
61
69
 
62
70
public:
63
71
  String();
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);
69
77
 
70
78
  static void *operator new(size_t size, memory::Root *mem_root);
74
82
  { }
75
83
  ~String();
76
84
 
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;
131
139
  }
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)
133
141
  {
134
142
    free();
135
143
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
136
144
    str_charset=cs;
137
145
  }
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)
139
147
  {
140
148
    free();
141
149
    Ptr= const_cast<char*>(str);
142
150
    str_length=arg_length; Alloced_length=0 ; alloced=0;
143
151
    str_charset=cs;
144
152
  }
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)
147
155
  {
148
156
    if (!alloced)
149
157
    {
151
159
    }
152
160
    str_charset= cs;
153
161
  }
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);
160
168
 
161
169
  /*
162
170
    PMG 2004.11.12
196
204
      str_length=0;                             /* Safety */
197
205
    }
198
206
  }
199
 
  inline void alloc(size_t arg_length)
 
207
  inline bool alloc(size_t arg_length)
200
208
  {
201
 
    if (arg_length >= Alloced_length)
202
 
      real_alloc(arg_length);
 
209
    if (arg_length < Alloced_length)
 
210
      return 0;
 
211
    return real_alloc(arg_length);
203
212
  }
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
207
216
  {
208
217
    if (arg_length < Alloced_length)
220
229
      }
221
230
    }
222
231
  }
223
 
  bool is_alloced() { return alloced; } const
 
232
  bool is_alloced() { return alloced; }
224
233
  inline String& operator = (const String &s)
225
234
  {
226
235
    if (&s != this)
237
246
    return *this;
238
247
  }
239
248
 
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,
246
255
                               size_t *offset);
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);
259
 
 
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)
261
270
  {
262
271
    if (str_length < Alloced_length)
263
272
    {
265
274
    }
266
275
    else
267
276
    {
268
 
      realloc(str_length+1);
 
277
      if (realloc(str_length+1))
 
278
        return 1;
269
279
      Ptr[str_length++]=chr;
270
280
    }
 
281
    return 0;
271
282
  }
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);
277
288
 
278
 
  void reserve(size_t space_needed)
 
289
  int reserve(size_t space_needed)
279
290
  {
280
 
    realloc(str_length + space_needed);
 
291
    return realloc(str_length + space_needed);
281
292
  }
282
 
  void reserve(size_t space_needed, size_t grow_by);
 
293
  int reserve(size_t space_needed, size_t grow_by);
283
294
 
284
295
  /*
285
296
    The following append operations do NOT check alloced memory
299
310
  {
300
311
    size_t new_length= arg_length + str_length;
301
312
    if (new_length > Alloced_length)
302
 
      realloc(new_length + step_alloc);
 
313
    {
 
314
      if (realloc(new_length + step_alloc))
 
315
        return 0;
 
316
    }
303
317
    size_t old_length= str_length;
304
318
    str_length+= arg_length;
305
319
    return Ptr+ old_length;                     /* Area to use */
306
320
  }
307
321
 
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)
309
323
  {
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))
 
326
      return true;
313
327
    memcpy(Ptr+str_length, s, arg_length);
314
328
    str_length+= arg_length;
 
329
    return false;
315
330
  }
316
 
 
317
331
  void print(String *print);
318
332
 
319
333
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
325
339
  }
326
340
};
327
341
 
328
 
bool check_if_only_end_space(const charset_info_st * const cs, char *str,
 
342
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
329
343
                             char *end);
330
344
 
331
345
std::ostream& operator<<(std::ostream& output, const String &str);
336
350
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
337
351
 
338
352
 
 
353
#endif /* DRIZZLED_SQL_STRING_H */