1
/* Copyright (C) 2003 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 */
16
#ifndef __UTIL_BASESTRING_HPP_INCLUDED__
17
#define __UTIL_BASESTRING_HPP_INCLUDED__
19
#include <ndb_global.h>
24
* @brief Null terminated strings
28
/** @brief Constructs an empty string */
31
/** @brief Constructs a copy of a char * */
32
BaseString(const char* s);
34
/** @brief Constructs a copy of another BaseString */
35
BaseString(const BaseString& str);
37
/** @brief Destructor */
40
/** @brief Returns a C-style string */
41
const char* c_str() const;
43
/** @brief Returns the length of the string */
44
unsigned length() const;
46
/** @brief Checks if the string is empty */
49
/** @brief Clear a string */
52
/** @brief Convert to uppercase */
53
BaseString& ndb_toupper();
55
/** @brief Convert to lowercase */
56
BaseString& ndb_tolower();
58
/** @brief Assigns from a char * */
59
BaseString& assign(const char* s);
61
/** @brief Assigns from another BaseString */
62
BaseString& assign(const BaseString& str);
64
/** @brief Assigns from char *s, with maximum length n */
65
BaseString& assign(const char* s, size_t n);
67
/** @brief Assigns from another BaseString, with maximum length n */
68
BaseString& assign(const BaseString& str, size_t n);
71
* Assings from a Vector of BaseStrings, each Vector entry
72
* separated by separator.
74
* @param vector Vector of BaseStrings to append
75
* @param separator Separation between appended strings
77
BaseString& assign(const Vector<BaseString> &vector,
78
const BaseString &separator = BaseString(" "));
80
/** @brief Appends a char * to the end */
81
BaseString& append(const char* s);
83
/** @brief Appends a char to the end */
84
BaseString& append(char c);
86
/** @brief Appends another BaseString to the end */
87
BaseString& append(const BaseString& str);
90
* Appends a Vector of BaseStrings to the end, each Vector entry
91
* separated by separator.
93
* @param vector Vector of BaseStrings to append
94
* @param separator Separation between appended strings
96
BaseString& append(const Vector<BaseString> &vector,
97
const BaseString &separator = BaseString(" "));
99
/** @brief Assigns from a format string a la printf() */
100
BaseString& assfmt(const char* ftm, ...);
102
/** @brief Appends a format string a la printf() to the end */
103
BaseString& appfmt(const char* ftm, ...);
106
* Split a string into a vector of strings. Separate the string where
107
* any character included in separator exists.
108
* Maximally maxSize entries are added to the vector, if more separators
109
* exist in the string, the remainder of the string will be appended
110
* to the last entry in the vector.
111
* The vector will not be cleared, so any existing strings in the
112
* vector will remain.
114
* @param separator characters separating the entries
115
* @param vector where the separated strings will be stored
116
* @param maximum number of strings extracted
118
* @returns the number of string added to the vector
120
int split(Vector<BaseString> &vector,
121
const BaseString &separator = BaseString(" "),
122
int maxSize = -1) const;
125
* Returns the index of the first occurance of the character c.
127
* @params c character to look for
128
* @returns index of character, of -1 if no character found
130
ssize_t indexOf(char c);
133
* Returns the index of the last occurance of the character c.
135
* @params c character to look for
136
* @returns index of character, of -1 if no character found
138
ssize_t lastIndexOf(char c);
141
* Returns a subset of a string
143
* @param start index of first character
144
* @param stop index of last character
145
* @return a new string
147
BaseString substr(ssize_t start, ssize_t stop = -1);
150
* @brief Assignment operator
152
BaseString& operator=(const BaseString& str);
154
/** @brief Compare two strings */
155
bool operator<(const BaseString& str) const;
156
/** @brief Are two strings equal? */
157
bool operator==(const BaseString& str) const;
158
/** @brief Are two strings equal? */
159
bool operator==(const char *str) const;
160
/** @brief Are two strings not equal? */
161
bool operator!=(const BaseString& str) const;
162
/** @brief Are two strings not equal? */
163
bool operator!=(const char *str) const;
166
* Trim string from <i>delim</i>
168
BaseString& trim(const char * delim = " \t");
171
* Return c-array with strings suitable for execve
172
* When whitespace is detected, the characters '"' and '\' are honored,
173
* to make it possible to give arguments containing whitespace.
174
* The semantics of '"' and '\' match that of most Unix shells.
176
static char** argify(const char *argv0, const char *src);
179
* Trim string from <i>delim</i>
181
static char* trim(char * src, const char * delim);
184
* snprintf on some platforms need special treatment
186
static int snprintf(char *str, size_t size, const char *format, ...);
187
static int vsnprintf(char *str, size_t size, const char *format, va_list ap);
191
friend bool operator!(const BaseString& str);
195
BaseString::c_str() const
201
BaseString::length() const
207
BaseString::empty() const
222
BaseString::ndb_toupper() {
223
for(unsigned i = 0; i < length(); i++)
224
m_chr[i] = toupper(m_chr[i]);
229
BaseString::ndb_tolower() {
230
for(unsigned i = 0; i < length(); i++)
231
m_chr[i] = tolower(m_chr[i]);
236
BaseString::operator<(const BaseString& str) const
238
return strcmp(m_chr, str.m_chr) < 0;
242
BaseString::operator==(const BaseString& str) const
244
return strcmp(m_chr, str.m_chr) == 0;
248
BaseString::operator==(const char *str) const
250
return strcmp(m_chr, str) == 0;
254
BaseString::operator!=(const BaseString& str) const
256
return strcmp(m_chr, str.m_chr) != 0;
260
BaseString::operator!=(const char *str) const
262
return strcmp(m_chr, str) != 0;
266
operator!(const BaseString& str)
268
return str.m_chr == NULL;
272
BaseString::assign(const BaseString& str)
274
return assign(str.m_chr);
278
BaseString::assign(const Vector<BaseString> &vector,
279
const BaseString &separator) {
281
return append(vector, separator);
284
#endif /* !__UTIL_BASESTRING_HPP_INCLUDED__ */