1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/* Copyright (c) 2003-2005, 2007 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
#ifndef __UTIL_BASESTRING_HPP_INCLUDED__
#define __UTIL_BASESTRING_HPP_INCLUDED__
#include <ndb_global.h>
#include <Vector.hpp>
/**
* @class BaseString
* @brief Null terminated strings
*/
class BaseString {
public:
/** @brief Constructs an empty string */
BaseString();
/** @brief Constructs a copy of a char * */
BaseString(const char* s);
/** @brief Constructs a copy of another BaseString */
BaseString(const BaseString& str);
/** @brief Destructor */
~BaseString();
/** @brief Returns a C-style string */
const char* c_str() const;
/** @brief Returns the length of the string */
unsigned length() const;
/** @brief Checks if the string is empty */
bool empty() const;
/** @brief Clear a string */
void clear();
/** @brief Convert to uppercase */
BaseString& ndb_toupper();
/** @brief Convert to lowercase */
BaseString& ndb_tolower();
/** @brief Assigns from a char * */
BaseString& assign(const char* s);
/** @brief Assigns from another BaseString */
BaseString& assign(const BaseString& str);
/** @brief Assigns from char *s, with maximum length n */
BaseString& assign(const char* s, size_t n);
/** @brief Assigns from another BaseString, with maximum length n */
BaseString& assign(const BaseString& str, size_t n);
/**
* Assings from a Vector of BaseStrings, each Vector entry
* separated by separator.
*
* @param vector Vector of BaseStrings to append
* @param separator Separation between appended strings
*/
BaseString& assign(const Vector<BaseString> &vector,
const BaseString &separator = BaseString(" "));
/** @brief Appends a char * to the end */
BaseString& append(const char* s);
/** @brief Appends a char to the end */
BaseString& append(char c);
/** @brief Appends another BaseString to the end */
BaseString& append(const BaseString& str);
/**
* Appends a Vector of BaseStrings to the end, each Vector entry
* separated by separator.
*
* @param vector Vector of BaseStrings to append
* @param separator Separation between appended strings
*/
BaseString& append(const Vector<BaseString> &vector,
const BaseString &separator = BaseString(" "));
/** @brief Assigns from a format string a la printf() */
BaseString& assfmt(const char* ftm, ...);
/** @brief Appends a format string a la printf() to the end */
BaseString& appfmt(const char* ftm, ...);
/**
* Split a string into a vector of strings. Separate the string where
* any character included in separator exists.
* Maximally maxSize entries are added to the vector, if more separators
* exist in the string, the remainder of the string will be appended
* to the last entry in the vector.
* The vector will not be cleared, so any existing strings in the
* vector will remain.
*
* @param separator characters separating the entries
* @param vector where the separated strings will be stored
* @param maximum number of strings extracted
*
* @returns the number of string added to the vector
*/
int split(Vector<BaseString> &vector,
const BaseString &separator = BaseString(" "),
int maxSize = -1) const;
/**
* Returns the index of the first occurance of the character c.
*
* @params c character to look for
* @returns index of character, of -1 if no character found
*/
ssize_t indexOf(char c);
/**
* Returns the index of the last occurance of the character c.
*
* @params c character to look for
* @returns index of character, of -1 if no character found
*/
ssize_t lastIndexOf(char c);
/**
* Returns a subset of a string
*
* @param start index of first character
* @param stop index of last character
* @return a new string
*/
BaseString substr(ssize_t start, ssize_t stop = -1);
/**
* @brief Assignment operator
*/
BaseString& operator=(const BaseString& str);
/** @brief Compare two strings */
bool operator<(const BaseString& str) const;
/** @brief Are two strings equal? */
bool operator==(const BaseString& str) const;
/** @brief Are two strings equal? */
bool operator==(const char *str) const;
/** @brief Are two strings not equal? */
bool operator!=(const BaseString& str) const;
/** @brief Are two strings not equal? */
bool operator!=(const char *str) const;
/**
* Trim string from <i>delim</i>
*/
BaseString& trim(const char * delim = " \t");
/**
* Return c-array with strings suitable for execve
* When whitespace is detected, the characters '"' and '\' are honored,
* to make it possible to give arguments containing whitespace.
* The semantics of '"' and '\' match that of most Unix shells.
*/
static char** argify(const char *argv0, const char *src);
/**
* Trim string from <i>delim</i>
*/
static char* trim(char * src, const char * delim);
/**
* snprintf on some platforms need special treatment
*/
static int snprintf(char *str, size_t size, const char *format, ...);
static int vsnprintf(char *str, size_t size, const char *format, va_list ap);
private:
char* m_chr;
unsigned m_len;
friend bool operator!(const BaseString& str);
};
inline const char*
BaseString::c_str() const
{
return m_chr;
}
inline unsigned
BaseString::length() const
{
return m_len;
}
inline bool
BaseString::empty() const
{
return m_len == 0;
}
inline void
BaseString::clear()
{
delete[] m_chr;
m_chr = new char[1];
m_chr[0] = 0;
m_len = 0;
}
inline BaseString&
BaseString::ndb_toupper() {
for(unsigned i = 0; i < length(); i++)
m_chr[i] = toupper(m_chr[i]);
return *this;
}
inline BaseString&
BaseString::ndb_tolower() {
for(unsigned i = 0; i < length(); i++)
m_chr[i] = tolower(m_chr[i]);
return *this;
}
inline bool
BaseString::operator<(const BaseString& str) const
{
return strcmp(m_chr, str.m_chr) < 0;
}
inline bool
BaseString::operator==(const BaseString& str) const
{
return strcmp(m_chr, str.m_chr) == 0;
}
inline bool
BaseString::operator==(const char *str) const
{
return strcmp(m_chr, str) == 0;
}
inline bool
BaseString::operator!=(const BaseString& str) const
{
return strcmp(m_chr, str.m_chr) != 0;
}
inline bool
BaseString::operator!=(const char *str) const
{
return strcmp(m_chr, str) != 0;
}
inline bool
operator!(const BaseString& str)
{
return str.m_chr == NULL;
}
inline BaseString&
BaseString::assign(const BaseString& str)
{
return assign(str.m_chr);
}
inline BaseString&
BaseString::assign(const Vector<BaseString> &vector,
const BaseString &separator) {
assign("");
return append(vector, separator);
}
#endif /* !__UTIL_BASESTRING_HPP_INCLUDED__ */
|