MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaseString.hpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef __UTIL_BASESTRING_HPP_INCLUDED__
19 #define __UTIL_BASESTRING_HPP_INCLUDED__
20 
21 #include <ndb_global.h>
22 #include <util/Vector.hpp>
23 #include "Bitmask.hpp"
24 
29 class BaseString {
30 public:
32  BaseString();
33 
35  BaseString(const char* s);
36 
38  BaseString(const char* s, size_t len);
39 
41  BaseString(const BaseString& str);
42 
44  ~BaseString();
45 
47  const char* c_str() const;
48 
50  unsigned length() const;
51 
53  bool empty() const;
54 
56  void clear();
57 
60 
63 
65  BaseString& assign(const char* s);
66 
68  BaseString& assign(const BaseString& str);
69 
71  BaseString& assign(const char* s, size_t n);
72 
74  BaseString& assign(const BaseString& str, size_t n);
75 
83  BaseString& assign(const Vector<BaseString> &vector,
84  const BaseString &separator = BaseString(" "));
85 
87  BaseString& append(const char* s);
88 
90  BaseString& append(char c);
91 
93  BaseString& append(const BaseString& str);
94 
102  BaseString& append(const Vector<BaseString> &vector,
103  const BaseString &separator = BaseString(" "));
104 
106  BaseString& assfmt(const char* ftm, ...)
107  ATTRIBUTE_FORMAT(printf, 2, 3);
108 
110  BaseString& appfmt(const char* ftm, ...)
111  ATTRIBUTE_FORMAT(printf, 2, 3);
112 
128  int split(Vector<BaseString> &vector,
129  const BaseString &separator = BaseString(" "),
130  int maxSize = -1) const;
131 
138  ssize_t indexOf(char c) const;
139 
146  ssize_t lastIndexOf(char c) const;
147 
155  BaseString substr(ssize_t start, ssize_t stop = -1) const;
156 
160  BaseString& operator=(const BaseString& str);
161 
163  bool operator<(const BaseString& str) const;
165  bool operator==(const BaseString& str) const;
167  bool operator==(const char *str) const;
169  bool operator!=(const BaseString& str) const;
171  bool operator!=(const char *str) const;
172 
176  BaseString& trim(const char * delim = " \t");
177 
184  static char** argify(const char *argv0, const char *src);
185 
189  static char* trim(char * src, const char * delim);
190 
194  static int snprintf(char *str, size_t size, const char *format, ...)
195  ATTRIBUTE_FORMAT(printf, 3, 4);
196  static int vsnprintf(char *str, size_t size, const char *format, va_list ap);
197 
198  template<unsigned size>
199  static BaseString getText(const Bitmask<size>& mask) {
200  return BaseString::getText(size, mask.rep.data);
201  }
202 
203  template<unsigned size>
204  static BaseString getPrettyText(const Bitmask<size>& mask) {
205  return BaseString::getPrettyText(size, mask.rep.data);
206  }
207  template<unsigned size>
208  static BaseString getPrettyTextShort(const Bitmask<size>& mask) {
209  return BaseString::getPrettyTextShort(size, mask.rep.data);
210  }
211 
212  template<unsigned size>
213  static BaseString getText(const BitmaskPOD<size>& mask) {
214  return BaseString::getText(size, mask.rep.data);
215  }
216 
217  template<unsigned size>
218  static BaseString getPrettyText(const BitmaskPOD<size>& mask) {
219  return BaseString::getPrettyText(size, mask.rep.data);
220  }
221  template<unsigned size>
222  static BaseString getPrettyTextShort(const BitmaskPOD<size>& mask) {
223  return BaseString::getPrettyTextShort(size, mask.rep.data);
224  }
225 
226  static BaseString getText(unsigned size, const Uint32 data[]);
227  static BaseString getPrettyText(unsigned size, const Uint32 data[]);
228  static BaseString getPrettyTextShort(unsigned size, const Uint32 data[]);
229 
230 private:
231  char* m_chr;
232  unsigned m_len;
233  friend bool operator!(const BaseString& str);
234 };
235 
236 inline const char*
238 {
239  return m_chr;
240 }
241 
242 inline unsigned
244 {
245  return m_len;
246 }
247 
248 inline bool
250 {
251  return m_len == 0;
252 }
253 
254 inline void
256 {
257  delete[] m_chr;
258  m_chr = new char[1];
259  m_chr[0] = 0;
260  m_len = 0;
261 }
262 
263 inline BaseString&
265  for(unsigned i = 0; i < length(); i++)
266  m_chr[i] = toupper(m_chr[i]);
267  return *this;
268 }
269 
270 inline BaseString&
272  for(unsigned i = 0; i < length(); i++)
273  m_chr[i] = tolower(m_chr[i]);
274  return *this;
275 }
276 
277 inline bool
279 {
280  return strcmp(m_chr, str.m_chr) < 0;
281 }
282 
283 inline bool
285 {
286  return strcmp(m_chr, str.m_chr) == 0;
287 }
288 
289 inline bool
290 BaseString::operator==(const char *str) const
291 {
292  return strcmp(m_chr, str) == 0;
293 }
294 
295 inline bool
297 {
298  return strcmp(m_chr, str.m_chr) != 0;
299 }
300 
301 inline bool
302 BaseString::operator!=(const char *str) const
303 {
304  return strcmp(m_chr, str) != 0;
305 }
306 
307 inline bool
308 operator!(const BaseString& str)
309 {
310  return str.m_chr == NULL;
311 }
312 
313 inline BaseString&
315 {
316  return assign(str.m_chr);
317 }
318 
319 inline BaseString&
321  const BaseString &separator) {
322  assign("");
323  return append(vector, separator);
324 }
325 
330 const void * BaseString_get_key(const void* key, size_t* key_length);
331 
332 #endif /* !__UTIL_BASESTRING_HPP_INCLUDED__ */