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, Inc.
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
23
/* This file is originally from the mysql distribution. Coded by monty */
25
#include <drizzled/common.h>
32
#include <drizzled/visibility.h>
35
#define NOT_FIXED_DEC (uint8_t)31
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
67
size_t str_length,Alloced_length;
69
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);
80
static void operator delete(void *, size_t)
82
static void operator delete(void *, memory::Root *)
86
inline void set_charset(const CHARSET_INFO * const charset_arg)
87
{ str_charset= charset_arg; }
88
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 ; }
93
inline bool is_empty() { return (str_length == 0); }
94
inline void mark_as_const() { Alloced_length= 0;}
95
inline char *ptr() { return Ptr; }
96
inline const char *ptr() const { return Ptr; }
99
if (str_length == Alloced_length)
100
(void) realloc(str_length);
106
inline char *c_ptr_quick()
108
if (Ptr && str_length < Alloced_length)
112
inline char *c_ptr_safe()
114
if (Ptr && str_length < Alloced_length)
117
(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);
130
void set(String &str,size_t offset,size_t arg_length)
132
assert(&str != this);
134
Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
135
if (str.Alloced_length)
136
Alloced_length=str.Alloced_length-offset;
139
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)
159
Ptr= str; str_length= Alloced_length= arg_length;
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);
172
This is a method that works the same as perl's "chop". It simply
173
drops the last character of a string. This is useful in the case
174
of the federated storage handler where I'm building a unknown
175
number, list of values and fields to be used in a sql insert
176
statement to be run on the remote server, and have a comma after each.
177
When the list is complete, I "chop" off the trailing comma
181
stringobj.append("VALUES ('foo', 'fi', 'fo',");
183
stringobj.append(")");
185
In this case, the value of string was:
187
VALUES ('foo', 'fi', 'fo',
188
VALUES ('foo', 'fi', 'fo'
189
VALUES ('foo', 'fi', 'fo')
194
Ptr[str_length--]= '\0';
205
str_length=0; /* Safety */
208
inline bool alloc(size_t arg_length)
210
if (arg_length < Alloced_length)
212
return real_alloc(arg_length);
214
bool real_alloc(size_t arg_length); // Empties old string
215
bool realloc(size_t arg_length);
216
inline void shrink(size_t arg_length) // Shrink buffer
218
if (arg_length < Alloced_length)
221
if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
224
real_alloc(arg_length);
229
Alloced_length=arg_length;
233
bool is_alloced() { return alloced; }
234
inline String& operator = (const String &s)
239
It is forbidden to do assignments like
240
some_string = substring_of_that_string
242
assert(!s.uses_buffer_owned_by(this));
244
Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
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,
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);
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);
270
inline bool append(char chr)
272
if (str_length < Alloced_length)
274
Ptr[str_length++]=chr;
278
if (realloc(str_length+1))
280
Ptr[str_length++]=chr;
284
friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
285
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);
290
int reserve(size_t space_needed)
292
return realloc(str_length + space_needed);
294
int reserve(size_t space_needed, size_t grow_by);
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
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);
308
/* Inline (general) functions used by the protocol functions */
310
inline char *prep_append(size_t arg_length, size_t step_alloc)
312
size_t new_length= arg_length + str_length;
313
if (new_length > Alloced_length)
315
if (realloc(new_length + step_alloc))
318
size_t old_length= str_length;
319
str_length+= arg_length;
320
return Ptr+ old_length; /* Area to use */
323
inline bool append(const char *s, size_t arg_length, size_t step_alloc)
325
size_t new_length= arg_length + str_length;
326
if (new_length > Alloced_length && realloc(new_length + step_alloc))
328
memcpy(Ptr+str_length, s, arg_length);
329
str_length+= arg_length;
332
void print(String *print);
334
/* Swap two string objects. Efficient way to exchange data without memcpy. */
335
void swap(String &s);
337
inline bool uses_buffer_owned_by(const String *s) const
339
return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
343
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
346
std::ostream& operator<<(std::ostream& output, const String &str);
348
} /* namespace drizzled */
350
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
351
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
354
#endif /* DRIZZLED_SQL_STRING_H */