00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef NSTRING_H
00023 #define NSTRING_H
00024
00030 #include "ndefs.h"
00031 #include "nlist.h"
00032
00033 #include <cstdio>
00034 #include <cstdarg>
00035 #include <cstring>
00036 #include <cstdlib>
00037
00038
00039
00040 #undef n_vasprintf
00041 #if defined(__WIN32) && defined (_MSC_VER)
00042 #define n_vasprintf(fmt, ap, ret) \
00043 { \
00044 int len = _vscprintf(fmt, ap); \
00045 ret = static_cast<char *>(malloc(len * sizeof(char))); \
00046 vsprintf_s(ret, len, fmt, ap); \
00047 }
00048 #elif defined(__WIN32) && defined (__GNUC__)
00049 #define n_vasprintf(fmt, ap, ret) \
00050 { \
00051 int max_size = 2048;\
00052 ret = static_cast<char *>(malloc(max_size));\
00053 vsnprintf(ret, max_size, fmt, ap); \
00054 }
00055 #else
00056 #define n_vasprintf(fmt, ap, ret) vasprintf(&ret, fmt, ap);
00057 #endif
00058
00063 class DllDeclSpec NString {
00064 public:
00068 NString(void);
00069
00074 NString(const char *str);
00075
00080 NString(const NString &str);
00081
00085 ~NString(void);
00086
00092 static NString number(int num);
00093
00099 static NString number(nint64 num);
00100
00106 NString &set(const char *str);
00107
00113 NString &set(const NString &str);
00114
00120 const char *toChar() const;
00121
00122
00127 unsigned int length() const;
00128
00133 bool isNull() const;
00134
00140 NString &append(const char *str, nuint32 bytes);
00141
00147 NString &append(const char *str);
00148
00154 NString &append(const NString &str);
00155
00161 NString &append(char c);
00162
00170 NString &insert(nuint32 pos, nuint32 size, const NString str);
00171
00178 NString &insert(nuint32 pos, const NString str);
00179
00188 bool compare(nuint32 pos, nuint32 size, const NString &str) const;
00189
00190
00198 bool compare(nuint32 pos, const NString &str) const;
00199
00209 bool compare(nuint32 pos, nuint32 size, const NString &str,
00210 nuint32 str_pos) const;
00211
00220 NString substr(nuint32 pos, nuint32 size) const;
00221
00222
00230 nint32 find(const NString &str, nuint32 pos, nuint32 ocur) const;
00231
00238 nint32 find(const NString &str, nuint32 pos) const;
00239
00240
00246 nint32 find(const NString &str) const;
00247
00248
00256 NString &replace_first(const NString &src, const NString &dest);
00257
00265 NString &replace(const NString &src, const NString &dest);
00266
00275 NString &replace(const NString &src, const NString &dest,
00276 nuint32 ocur);
00277
00278
00285 NList<NString> split(const NString &substr);
00286
00293 NString operator+(const char *str);
00299 NString operator+(const NString &str);
00300
00306 NString operator+(char c);
00307
00313 NString &operator+=(const char *str);
00314
00320 NString &operator+=(const NString &str);
00321
00327 NString &operator+=(char c);
00328
00334 char operator[](unsigned int index) const;
00335
00341 bool operator==(const NString &str) const;
00342
00348 bool operator!=(const NString &str) const;
00349
00356 bool operator>(const NString &str) const;
00357
00364 bool operator<(const NString &str) const;
00365
00373 bool operator>=(const NString &str) const;
00374
00382 bool operator<=(const NString &str) const;
00383
00384
00390 NString &operator=(const NString &str);
00391
00392
00398 NString &arg(const char *fmt, ...);
00399
00400 static const nint32 NOT_FOUND;
00402 private:
00403 char *m_string;
00404 nuint32 m_used;
00405 nuint32 m_allocated;
00406 static const nuint32 ALLOC_SIZE;
00407
00408 bool needsMemory(nuint32 bytes);
00409 nuint32 calculateBlocks(nuint32 bytes);
00410 void memcopy(char *dest);
00411 nuint32 freeBytes(void) const;
00412 void realloc(nuint32 bytes);
00413
00414 };
00415
00423 inline NString NSTR(const char *str) {
00424 return NString(str);
00425 }
00426
00427 #endif // NSTRING_H
00428
00429