1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
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
1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
23
16
/* This file is originally from the mysql distribution. Coded by monty */
25
#include <drizzled/common.h>
18
#ifdef USE_PRAGMA_INTERFACE
19
#pragma interface /* gcc class implementation */
31
22
#ifndef NOT_FIXED_DEC
32
#define NOT_FIXED_DEC (uint8_t)31
23
#define NOT_FIXED_DEC 31
40
extern String my_empty_string;
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);
49
int stringcmp(const String *a,const String *b);
50
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
51
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
52
char *to, uint32_t to_length,
53
const CHARSET_INFO * const from_cs,
54
const char *from, uint32_t from_length,
56
const char **well_formed_error_pos,
57
const char **cannot_convert_error_pos,
58
const char **from_end_pos);
27
int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
28
String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
29
uint32 copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
30
const char *from, uint32 from_length,
31
CHARSET_INFO *from_cs, uint *errors);
32
uint32 well_formed_copy_nchars(CHARSET_INFO *to_cs,
33
char *to, uint to_length,
34
CHARSET_INFO *from_cs,
35
const char *from, uint from_length,
37
const char **well_formed_error_pos,
38
const char **cannot_convert_error_pos,
39
const char **from_end_pos);
40
size_t my_copy_with_hex_escaping(CHARSET_INFO *cs,
41
char *dst, size_t dstlen,
42
const char *src, size_t srclen);
64
uint32_t str_length,Alloced_length;
47
uint32 str_length,Alloced_length;
66
const CHARSET_INFO *str_charset;
49
CHARSET_INFO *str_charset;
70
String(uint32_t length_arg);
71
String(const char *str, const CHARSET_INFO * const cs);
72
String(const char *str, uint32_t len, const CHARSET_INFO * const cs);
73
String(char *str, uint32_t len, const CHARSET_INFO * const cs);
74
String(const String &str);
76
static void *operator new(size_t size, memory::Root *mem_root);
77
static void operator delete(void *, size_t)
79
static void operator delete(void *, memory::Root *)
83
inline void set_charset(const CHARSET_INFO * const charset_arg)
53
Ptr=0; str_length=Alloced_length=0; alloced=0;
54
str_charset= &my_charset_bin;
56
String(uint32 length_arg)
58
alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
59
str_charset= &my_charset_bin;
61
String(const char *str, CHARSET_INFO *cs)
63
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
66
String(const char *str,uint32 len, CHARSET_INFO *cs)
68
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
71
String(char *str,uint32 len, CHARSET_INFO *cs)
73
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
76
String(const String &str)
78
Ptr=str.Ptr ; str_length=str.str_length ;
79
Alloced_length=str.Alloced_length; alloced=0;
80
str_charset=str.str_charset;
82
static void *operator new(size_t size, MEM_ROOT *mem_root)
83
{ return (void*) alloc_root(mem_root, (uint) size); }
84
static void operator delete(void *ptr_arg __attribute__((__unused__)),
85
size_t size __attribute__((__unused__)))
86
{ TRASH(ptr_arg, size); }
87
static void operator delete(void *ptr_arg __attribute__((__unused__)),
88
MEM_ROOT *mem_root __attribute__((__unused__)))
89
{ /* never called */ }
92
inline void set_charset(CHARSET_INFO *charset_arg)
84
93
{ str_charset= charset_arg; }
85
inline const CHARSET_INFO *charset() const { return str_charset; }
86
inline uint32_t length() const { return str_length;}
87
inline uint32_t alloced_length() const { return Alloced_length;}
88
inline char& operator [] (uint32_t i) const { return Ptr[i]; }
89
inline void length(uint32_t len) { str_length=len ; }
94
inline CHARSET_INFO *charset() const { return str_charset; }
95
inline uint32 length() const { return str_length;}
96
inline uint32 alloced_length() const { return Alloced_length;}
97
inline char& operator [] (uint32 i) const { return Ptr[i]; }
98
inline void length(uint32 len) { str_length=len ; }
90
99
inline bool is_empty() { return (str_length == 0); }
91
100
inline void mark_as_const() { Alloced_length= 0;}
92
inline char *ptr() { return Ptr; }
93
101
inline const char *ptr() const { return Ptr; }
94
102
inline char *c_ptr()
96
if (str_length == Alloced_length)
104
if (!Ptr || Ptr[str_length]) /* Should be safe */
97
105
(void) realloc(str_length);
103
108
inline char *c_ptr_quick()
114
119
(void) realloc(str_length);
119
if (Ptr && str_length < Alloced_length)
122
(void) realloc(str_length);
125
void append_identifier(const char *name, uint32_t length);
127
void set(String &str,uint32_t offset,uint32_t arg_length)
123
void set(String &str,uint32 offset,uint32 arg_length)
129
125
assert(&str != this);
131
Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
127
Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
132
128
if (str.Alloced_length)
133
129
Alloced_length=str.Alloced_length-offset;
135
131
Alloced_length=0;
136
132
str_charset=str.str_charset;
138
inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
141
Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
144
inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
147
Ptr= const_cast<char*>(str);
148
str_length=arg_length; Alloced_length=0 ; alloced=0;
151
bool set_ascii(const char *str, uint32_t arg_length);
152
inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
134
inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
137
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
140
inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
143
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
146
bool set_ascii(const char *str, uint32 arg_length);
147
inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
156
Ptr= str; str_length= Alloced_length= arg_length;
151
Ptr=(char*) str; str_length=Alloced_length=arg_length;
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)
155
bool set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs);
156
bool set(longlong num, CHARSET_INFO *cs)
162
157
{ 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,uint32_t decimals, const CHARSET_INFO * const cs);
158
bool set(ulonglong num, CHARSET_INFO *cs)
159
{ return set_int((longlong)num, true, cs); }
160
bool set_real(double num,uint decimals, CHARSET_INFO *cs);
247
242
bool copy(); // Alloc string if not alloced
248
243
bool copy(const String &s); // Allocate new string
249
bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs); // Allocate new string
250
static bool needs_conversion(uint32_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, uint32_t arg_length, const CHARSET_INFO * const cs);
254
bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
255
const CHARSET_INFO * const csto, uint32_t *errors);
244
bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
245
static bool needs_conversion(uint32 arg_length,
246
CHARSET_INFO *cs_from, CHARSET_INFO *cs_to,
248
bool copy_aligned(const char *s, uint32 arg_length, uint32 offset,
250
bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs);
251
bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom,
252
CHARSET_INFO *csto, uint *errors);
256
253
bool append(const String &s);
257
254
bool append(const char *s);
258
bool append(const char *s,uint32_t arg_length);
259
bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
260
bool append_with_prefill(const char *s, uint32_t arg_length,
261
uint32_t full_length, char fill_char);
262
int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
263
int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
264
bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
265
bool replace(uint32_t offset,uint32_t arg_length,const String &to);
255
bool append(const char *s,uint32 arg_length);
256
bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs);
257
bool append(IO_CACHE* file, uint32 arg_length);
258
bool append_with_prefill(const char *s, uint32 arg_length,
259
uint32 full_length, char fill_char);
260
int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
261
int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
262
bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length);
263
bool replace(uint32 offset,uint32 arg_length,const String &to);
266
264
inline bool append(char chr)
268
266
if (str_length < Alloced_length)
280
friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
278
bool fill(uint32 max_length,char fill);
280
friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
281
281
friend int stringcmp(const String *a,const String *b);
282
friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
284
int charpos(int i,uint32_t offset=0);
282
friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
284
int charpos(int i,uint32 offset=0);
286
int reserve(uint32_t space_needed)
286
int reserve(uint32 space_needed)
288
288
return realloc(str_length + space_needed);
290
int reserve(uint32_t space_needed, uint32_t grow_by);
290
int reserve(uint32 space_needed, uint32 grow_by);
293
293
The following append operations do NOT check alloced memory
294
294
q_*** methods writes values of parameters itself
295
295
qs_*** methods writes string representation of value
297
void q_append(const char c);
298
void q_append(const uint32_t n);
299
void q_append(double d);
300
void q_append(double *d);
301
void q_append(const char *data, uint32_t data_len);
302
void write_at_position(int position, uint32_t value);
297
void q_append(const char c)
299
Ptr[str_length++] = c;
301
void q_append(const uint32 n)
303
int4store(Ptr + str_length, n);
306
void q_append(double d)
308
float8store(Ptr + str_length, d);
311
void q_append(double *d)
313
float8store(Ptr + str_length, *d);
316
void q_append(const char *data, uint32 data_len)
318
memcpy(Ptr + str_length, data, data_len);
319
str_length += data_len;
322
void write_at_position(int position, uint32 value)
324
int4store(Ptr + position,value);
327
void qs_append(const char *str, uint32 len);
328
void qs_append(double d);
329
void qs_append(double *d);
330
inline void qs_append(const char c)
335
void qs_append(int i);
336
void qs_append(uint i);
304
338
/* Inline (general) functions used by the protocol functions */
306
inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
340
inline char *prep_append(uint32 arg_length, uint32 step_alloc)
308
uint32_t new_length= arg_length + str_length;
342
uint32 new_length= arg_length + str_length;
309
343
if (new_length > Alloced_length)
311
345
if (realloc(new_length + step_alloc))
314
uint32_t old_length= str_length;
348
uint32 old_length= str_length;
315
349
str_length+= arg_length;
316
350
return Ptr+ old_length; /* Area to use */
319
inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
353
inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
321
uint32_t new_length= arg_length + str_length;
355
uint32 new_length= arg_length + str_length;
322
356
if (new_length > Alloced_length && realloc(new_length + step_alloc))
324
358
memcpy(Ptr+str_length, s, arg_length);
325
359
str_length+= arg_length;
328
362
void print(String *print);