Drizzled Public API Documentation

sql_string.h
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
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.
9  *
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.
14  *
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
18  */
19 
20 #pragma once
21 
22 /* This file is originally from the mysql distribution. Coded by monty */
23 
24 #include <drizzled/common.h>
25 #include <drizzled/util/data_ref.h>
26 
27 #include <cassert>
28 #include <cstdlib>
29 #include <cstring>
30 #include <string>
31 
32 #include <drizzled/visibility.h>
33 #include <drizzled/common_fwd.h>
34 
35 #ifndef NOT_FIXED_DEC
36 #define NOT_FIXED_DEC (uint8_t)31
37 #endif
38 
39 namespace drizzled {
40 
41 extern DRIZZLED_API String my_empty_string;
42 extern const String my_null_string;
43 
44 int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
45 int stringcmp(const String *a,const String *b);
46 String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
47 size_t well_formed_copy_nchars(const charset_info_st * const to_cs,
48  char *to, size_t to_length,
49  const charset_info_st * const from_cs,
50  const char *from, size_t from_length,
51  size_t nchars,
52  const char **well_formed_error_pos,
53  const char **cannot_convert_error_pos,
54  const char **from_end_pos);
55 
56 
58 {
59  char *Ptr;
60  size_t str_length,Alloced_length;
61  bool alloced;
62  const charset_info_st *str_charset;
63 
64 public:
65  String();
66  String(size_t length_arg);
67  String(const char*, const charset_info_st*);
68  String(const char*, size_t, const charset_info_st*);
69  String(char *str, size_t len, const charset_info_st*);
71  String(const String&);
72 
73  static void *operator new(size_t size, memory::Root *mem_root);
74  static void operator delete(void *, size_t)
75  { }
76  static void operator delete(void *, memory::Root *)
77  { }
78  ~String();
79 
80  inline void set_charset(const charset_info_st * const charset_arg)
81  { str_charset= charset_arg; }
82  inline const charset_info_st *charset() const { return str_charset; }
83  inline size_t length() const { return str_length;}
84  inline size_t alloced_length() const { return Alloced_length;}
85  inline char& operator [] (size_t i) const { return Ptr[i]; }
86  inline void length(size_t len) { str_length=len; }
87  inline bool empty() const { return str_length == 0; }
88  inline void mark_as_const() { Alloced_length= 0; }
89  inline char *ptr() { return Ptr; }
90  inline const char *ptr() const { return Ptr; }
91  inline char *c_ptr()
92  {
93  if (str_length == Alloced_length)
94  realloc(str_length);
95  else
96  Ptr[str_length]= 0;
97 
98  return Ptr;
99  }
100  inline const char* begin() const
101  {
102  return Ptr;
103  }
104  inline const char* end() const
105  {
106  return begin() + size();
107  }
108  inline const char* data() const
109  {
110  return Ptr;
111  }
112  inline size_t size() const
113  {
114  return length();
115  }
116  inline const char* c_str()
117  {
118  if (Ptr && str_length < Alloced_length)
119  Ptr[str_length]=0;
120  else
121  realloc(str_length);
122  return Ptr;
123  }
124  void append_identifier(const char *name, size_t length);
125  void append_identifier(str_ref);
126 
127  void set(String &str,size_t offset,size_t arg_length)
128  {
129  assert(&str != this);
130  free();
131  Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
132  if (str.Alloced_length)
133  Alloced_length=str.Alloced_length-offset;
134  else
135  Alloced_length=0;
136  str_charset=str.str_charset;
137  }
138  inline void set(char *str,size_t arg_length, const charset_info_st * const cs)
139  {
140  free();
141  Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
142  str_charset=cs;
143  }
144 
145  inline void set(const char *str,size_t arg_length, const charset_info_st * const cs)
146  {
147  free();
148  Ptr= const_cast<char*>(str);
149  str_length=arg_length; Alloced_length=0 ; alloced=0;
150  str_charset=cs;
151  }
152  void set_ascii(const char *str, size_t arg_length);
153 
154  inline void set_quick(char *str,size_t arg_length, const charset_info_st * const cs)
155  {
156  if (!alloced)
157  {
158  Ptr= str; str_length= Alloced_length= arg_length;
159  }
160  str_charset= cs;
161  }
162 
163  void set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs);
164  void set(int64_t num, const charset_info_st * const cs)
165  { set_int(num, false, cs); }
166  void set(uint64_t num, const charset_info_st * const cs)
167  { set_int(static_cast<int64_t>(num), true, cs); }
168  void set_real(double num,size_t decimals, const charset_info_st* cs);
169 
170  /*
171  PMG 2004.11.12
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
178 
179  ex.
180  String stringobj;
181  stringobj.append("VALUES ('foo', 'fi', 'fo',");
182  stringobj.chop();
183  stringobj.append(")");
184 
185  In this case, the value of string was:
186 
187  VALUES ('foo', 'fi', 'fo',
188  VALUES ('foo', 'fi', 'fo'
189  VALUES ('foo', 'fi', 'fo')
190 
191  */
192  inline void chop()
193  {
194  Ptr[str_length--]= '\0';
195  }
196 
197  inline void free()
198  {
199  if (alloced)
200  {
201  alloced=0;
202  Alloced_length=0;
203  ::free(Ptr);
204  Ptr=0;
205  str_length=0; /* Safety */
206  }
207  }
208  inline void alloc(size_t arg_length)
209  {
210  if (arg_length >= Alloced_length)
211  real_alloc(arg_length);
212  }
213  void real_alloc(size_t arg_length); // Empties old string
214  void realloc(size_t arg_length);
215  inline void shrink(size_t arg_length) // Shrink buffer
216  {
217  if (arg_length < Alloced_length)
218  {
219  char *new_ptr;
220  if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
221  {
222  Alloced_length = 0;
223  real_alloc(arg_length);
224  }
225  else
226  {
227  Ptr=new_ptr;
228  Alloced_length=arg_length;
229  }
230  }
231  }
232  bool is_alloced() { return alloced; } const
233  inline String& operator = (const String &s)
234  {
235  if (&s != this)
236  {
237  /*
238  It is forbidden to do assignments like
239  some_string = substring_of_that_string
240  */
241  assert(!s.uses_buffer_owned_by(this));
242  free();
243  Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
244  alloced=0;
245  }
246  return *this;
247  }
248 
249  void copy(); // Alloc string if not alloced
250  void copy(const String&); // Allocate new string
251  void copy(const std::string&, const charset_info_st*); // Allocate new string
252  void copy(const char*, size_t, const charset_info_st*); // Allocate new string
253  static bool needs_conversion(size_t arg_length, const charset_info_st* cs_from, const charset_info_st* cs_to);
254  void set_or_copy_aligned(const char *s, size_t arg_length, const charset_info_st*);
255  void copy(const char*s,size_t arg_length, const charset_info_st& csto);
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);
259  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
260  int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
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);
263 
264  inline void append(char chr)
265  {
266  if (str_length < Alloced_length)
267  {
268  Ptr[str_length++]=chr;
269  }
270  else
271  {
272  realloc(str_length+1);
273  Ptr[str_length++]=chr;
274  }
275  }
276  friend int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
277  friend int stringcmp(const String *a,const String *b);
278  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
279  size_t numchars() const;
280  int charpos(int i, size_t offset= 0) const;
281 
282  void reserve(size_t space_needed)
283  {
284  realloc(str_length + space_needed);
285  }
286  void reserve(size_t space_needed, size_t grow_by);
287 
288  inline void append(const char *s, size_t arg_length, size_t step_alloc)
289  {
290  size_t new_length= arg_length + str_length;
291  if (new_length > Alloced_length)
292  realloc(new_length + step_alloc);
293  memcpy(Ptr+str_length, s, arg_length);
294  str_length+= arg_length;
295  }
296 
297  void print(String&) const;
298 
299  /* Swap two string objects. Efficient way to exchange data without memcpy. */
300  void swap(String &s);
301 
302  inline bool uses_buffer_owned_by(const String *s) const
303  {
304  return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
305  }
306 };
307 
308 bool check_if_only_end_space(const charset_info_st* const, char *str, char *end);
309 
310 std::ostream& operator<<(std::ostream&, const String&);
311 
312 } /* namespace drizzled */
313 
314 bool operator==(const drizzled::String &s1, const drizzled::String &s2);
315 bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
#define DRIZZLED_API
Definition: visibility.h:62
Visibility Control Macros.