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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
#ifndef TOOLBOX_STRING_H
#define TOOLBOX_STRING_H
#include "tools/tools.h"
#include "tools/shared.h"
#include <ctype.h>
#include <stddef.h>
#include <string.h>
#ifdef TOOLBOX_REGEX
class cTBRegEx;
#endif
class cTBString: public cTBShared {
private:
uint m_StringLen;
/* Unhide and forbid baseclass method */
virtual void Set (const cTBShared &src) {}
public:
cTBString ();
cTBString (const cTBString &src);
cTBString (const uchar *src);
cTBString (const char *src);
cTBString (char src);
virtual ~cTBString ();
static const cTBString Null;
void Clear ();
void Set (const cTBString &String);
void Set (const uchar *String);
void Set (const char *String);
void Set (char Character);
void Fill (char Character, int Length = -1);
void Release (uint newsize);
cTBString &operator= (const cTBString &src) { Set(src); return *this; }
cTBString &operator= (const char *src) { Set(src); return *this; }
cTBString &operator= (char src) { Set(src); return *this; }
void Append (const cTBString &src);
void Append (const char *src);
void Append (char src);
friend cTBString operator+ (const cTBString &a, const cTBString &b);
friend cTBString operator+ (const cTBString &a, const char *b);
friend cTBString operator+ (const char *a, const cTBString &b);
friend cTBString operator+ (const cTBString &a, char b);
friend cTBString operator+ (char a, const cTBString &b);
friend cTBString &operator+= (cTBString &a, const cTBString &b);
friend cTBString &operator+= (cTBString &a, const char *b);
friend cTBString &operator+= (cTBString &a, char b);
void Prepend (const cTBString &src);
void Prepend (const char *src);
void Prepend (char src);
void Insert (uint Index, const cTBString &src);
void Insert (uint Index, const char *src);
void Insert (uint Index, char src);
char At (uint i) const;
char operator[] (int i) const { return At((uint)i); }
char &At (uint i);
char &operator[] (int i) { return At(i); }
cTBString Left (uint Count) const;
cTBString Right (uint Count) const;
cTBString Mid (int idx, int Count = -1) const;
int Find (const cTBString &String, uint Offset = 0) const;
int Find (const char *String, uint Offset = 0) const;
int Find (char Character, uint Offset = 0) const;
#ifdef TOOLBOX_REGEX
bool Find (cTBRegEx &Regex, uint Offset = 0) const;
#endif
cTBString &Format (const char *fmt, ...)
#if defined(__GNUC__)
__attribute__ ((format (printf, 2, 3)))
#endif
;
cTBString &Format (const cTBString &fmt, ...);
typedef int(*TOFUNC)(int);
template<TOFUNC F> cTBString ToAnything(void) const;
cTBString ToUpper (void) const { return ToAnything<toupper>(); }
cTBString ToLower (void) const { return ToAnything<tolower>(); }
typedef int(*ISFUNC)(int);
template<ISFUNC F> bool IsAnything(void) const;
bool IsAlnum(void) const { return IsAnything<isalnum>(); }
bool IsAlpha(void) const { return IsAnything<isalpha>(); }
bool IsAscii(void) const { return IsAnything<isascii>(); }
bool IsCntrl(void) const { return IsAnything<iscntrl>(); }
bool IsDigit(void) const { return IsAnything<isdigit>(); }
bool IsGraph(void) const { return IsAnything<isgraph>(); }
bool IsLower(void) const { return IsAnything<islower>(); }
bool IsPrint(void) const { return IsAnything<isprint>(); }
bool IsPunct(void) const { return IsAnything<ispunct>(); }
bool IsSpace(void) const { return IsAnything<isspace>(); }
bool IsUpper(void) const { return IsAnything<isupper>(); }
bool IsXdigit(void) const { return IsAnything<isxdigit>(); }
#if defined(_GNU_SOURCE)
bool IsBlank(void) const { return IsAnything<isblank>(); }
#endif
uint Length (void) const { return m_StringLen; }
bool IsEmpty (void) const { return m_StringLen == 0; }
bool IsNull (void) const { return Buffer() == 0; }
short ToShort(bool *Ok = NULL) const;
ushort ToUShort(bool *Ok = NULL) const;
int ToInt(bool *Ok = NULL) const;
uint ToUInt(bool *Ok = NULL) const;
long ToLong(bool *Ok = NULL) const;
ulong ToULong(bool *Ok = NULL) const;
float ToFloat(bool *Ok = NULL) const;
double ToDouble(bool *Ok = NULL) const;
static cTBString Number(short Num);
static cTBString Number(ushort Num);
static cTBString Number(int Num);
static cTBString Number(uint Num);
static cTBString Number(long Num);
static cTBString Number(ulong Num);
static cTBString Number(float Num);
static cTBString Number(double Num);
friend bool operator== (const cTBString &str1, const cTBString &str2);
friend bool operator== (const cTBString &str1, const char *str2);
friend bool operator== (const char *str1, const cTBString &str2);
friend bool operator!= (const cTBString &str1, const cTBString &str2);
friend bool operator!= (const cTBString &str1, const char *str2);
friend bool operator!= (const char *str1, const cTBString &str2);
friend bool operator< (const cTBString &str1, const cTBString &str2);
friend bool operator< (const cTBString &str1, const char *str2);
friend bool operator< (const char *str1, const cTBString &str2);
friend bool operator> (const cTBString &str1, const cTBString &str2);
friend bool operator> (const cTBString &str1, const char *str2);
friend bool operator> (const char *str1, const cTBString &str2);
friend bool operator<= (const cTBString &str1, const cTBString &str2);
friend bool operator<= (const cTBString &str1, const char *str2);
friend bool operator<= (const char *str1, const cTBString &str2);
friend bool operator>= (const cTBString &str1, const cTBString &str2);
friend bool operator>= (const cTBString &str1, const char *str2);
friend bool operator>= (const char *str1, const cTBString &str2);
};
inline char cTBString::At(uint idx) const {
ASSERT(idx >= m_StringLen);
return Buffer() ? Buffer()[idx] : 0;
}
inline char &cTBString::At(uint idx) {
static char null = 0;
ASSERT(idx >= m_StringLen);
if (Buffer()) {
Exclusive();
return Buffer()[idx];
} else
return (null = 0);
}
inline
RETURNS(cTBString, operator+(const cTBString &a, const cTBString &b), ret(a))
ret.Append(b);
RETURN(ret)
inline
RETURNS(cTBString, operator+ (const cTBString &a, const char *b), ret(a))
ret.Append(b);
RETURN(ret)
inline
RETURNS(cTBString, operator+ (const char *a, const cTBString &b), ret(a))
ret.Append(b);
RETURN(ret)
inline
RETURNS(cTBString, operator+ (const cTBString &a, char b), ret(a))
ret.Append(b);
RETURN(ret)
inline
RETURNS(cTBString, operator+ (char a, const cTBString &b), ret(a))
ret.Append(b);
RETURN(ret)
inline cTBString &operator+= (cTBString &a, const cTBString &b) {
a.Append(b);
return a;
}
inline cTBString &operator+= (cTBString &a, const char *b) {
a.Append(b);
return a;
}
inline cTBString &operator+= (cTBString &a, char b) {
a.Append(b);
return a;
}
inline bool operator== (const cTBString &str1, const cTBString &str2) {
if (str1.Length() != str2.Length())
return false;
return memcmp(str1.Buffer(), str2.Buffer(), str1.Length()) == 0;
}
inline bool operator== (const cTBString &str1, const char *str2) {
uint len = strlen(str2);
if (str1.Length() != len)
return false;
return memcmp(str1.Buffer(), str2, len) == 0;
}
inline bool operator== (const char *str1, const cTBString &str2) {
uint len = strlen(str1);
if (len != str2.Length())
return false;
return memcmp(str1, str2.Buffer(), len) == 0;
}
inline bool operator!= (const cTBString &str1, const cTBString &str2) {
if (str1.Length() != str2.Length())
return true;
return memcmp(str1.Buffer(), str2.Buffer(), str1.Length()) != 0;
}
inline bool operator!= (const cTBString &str1, const char *str2) {
uint len = strlen(str2);
if (str1.Length() != len)
return true;
return memcmp(str1.Buffer(), str2, len) != 0;
}
inline bool operator!= (const char *str1, const cTBString &str2) {
uint len = strlen(str1);
if (len != str2.Length())
return true;
return memcmp(str1, str2.Buffer(), len) != 0;
}
inline bool operator< (const cTBString &str1, const cTBString &str2) {
int ret = memcmp(str1.Buffer(), str2.Buffer(), str1.Length() < str2.Length() ? str1.Length() : str2.Length());
if ((ret < 0) || ((ret == 0) && (str1.Length() < str2.Length())))
return true;
return false;
}
inline bool operator< (const cTBString &str1, const char *str2) {
uint len = strlen(str2);
int ret = memcmp(str1.Buffer(), str2, str1.Length() < len ? str1.Length() : len);
if ((ret < 0) || ((ret == 0) && (str1.Length() < len)))
return true;
return false;
}
inline bool operator< (const char *str1, const cTBString &str2) {
uint len = strlen(str1);
int ret = memcmp(str1, str2.Buffer(), len < str2.Length() ? len : str2.Length());
if ((ret < 0) || ((ret == 0) && (len < str2.Length())))
return true;
return false;
}
inline bool operator> (const cTBString &str1, const cTBString &str2) {
int ret = memcmp(str1.Buffer(), str2.Buffer(), str1.Length() < str2.Length() ? str1.Length() : str2.Length());
if ((ret > 0) || ((ret == 0) && (str1.Length() < str2.Length())))
return true;
return false;
}
inline bool operator> (const cTBString &str1, const char *str2) {
uint len = strlen(str2);
int ret = memcmp(str1.Buffer(), str2, str1.Length() < len ? str1.Length() : len);
if ((ret > 0) || ((ret == 0) && (str1.Length() < len)))
return true;
return false;
}
inline bool operator> (const char *str1, const cTBString &str2) {
uint len = strlen(str1);
int ret = memcmp(str1, str2.Buffer(), len < str2.Length() ? len : str2.Length());
if ((ret > 0) || ((ret == 0) && (len < str2.Length())))
return true;
return false;
}
inline bool operator<= (const cTBString &str1, const cTBString &str2) {
int ret = memcmp(str1.Buffer(), str2.Buffer(), str1.Length() < str2.Length() ? str1.Length() : str2.Length());
if ((ret < 0) || ((ret == 0) && (str1.Length() <= str2.Length())))
return true;
return false;
}
inline bool operator<= (const cTBString &str1, const char *str2) {
uint len = strlen(str2);
int ret = memcmp(str1.Buffer(), str2, str1.Length() < len ? str1.Length() : len);
if ((ret < 0) || ((ret == 0) && (str1.Length() <= len)))
return true;
return false;
}
inline bool operator<= (const char *str1, const cTBString &str2) {
uint len = strlen(str1);
int ret = memcmp(str1, str2.Buffer(), len < str2.Length() ? len : str2.Length());
if ((ret < 0) || ((ret == 0) && (len <= str2.Length())))
return true;
return false;
}
inline bool operator>= (const cTBString &str1, const cTBString &str2) {
int ret = memcmp(str1.Buffer(), str2.Buffer(), str1.Length() < str2.Length() ? str1.Length() : str2.Length());
if ((ret > 0) || ((ret == 0) && (str1.Length() >= str2.Length())))
return true;
return false;
}
inline bool operator>= (const cTBString &str1, const char *str2) {
uint len = strlen(str2);
int ret = memcmp(str1.Buffer(), str2, str1.Length() < len ? str1.Length() : len);
if ((ret > 0) || ((ret == 0) && (str1.Length() >= len)))
return true;
return false;
}
inline bool operator>= (const char *str1, const cTBString &str2) {
uint len = strlen(str1);
int ret = memcmp(str1, str2.Buffer(), len < str2.Length() ? len : str2.Length());
if ((ret > 0) || ((ret == 0) && (len >= str2.Length())))
return true;
return false;
}
#endif // TOOLBOX_STRING_H
|