filters
ustring.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SWINDER_USTRING_H_
00023 #define SWINDER_USTRING_H_
00024
00025 namespace Swinder {
00026
00030 bool isNaN(double d);
00031
00032 bool isPosInf(double d);
00033 bool isNegInf(double d);
00034
00035 class UCharReference;
00036 class UString;
00037 class UConstString;
00038
00046 struct UChar {
00050 UChar();
00056 UChar(unsigned char h , unsigned char l);
00061 UChar(unsigned short u);
00062 UChar(const UCharReference &c);
00066 unsigned char high() const { return uc >> 8; }
00070 unsigned char low() const { return uc & 0xFF; }
00074 unsigned short unicode() const { return uc; }
00075 public:
00079 UChar toLower() const;
00083 UChar toUpper() const;
00087 static UChar null;
00088 private:
00089 friend class UCharReference;
00090 friend class UString;
00091 friend bool operator==(const UChar &c1, const UChar &c2);
00092 friend bool operator==(const UString& s1, const char *s2);
00093 friend bool operator<(const UString& s1, const UString& s2);
00094
00095 unsigned short uc;
00096 };
00097
00098 inline UChar::UChar() : uc(0) { }
00099 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
00100 inline UChar::UChar(unsigned short u) : uc(u) { }
00101
00116 class UCharReference {
00117 friend class UString;
00118 UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
00119 public:
00123 UCharReference& operator=(UChar c);
00127 UCharReference& operator=(char c) { return operator=(UChar(c)); }
00131 unsigned short unicode() const { return ref().unicode(); }
00135 unsigned char low() const { return ref().uc & 0xFF; }
00139 unsigned char high() const { return ref().uc >> 8; }
00143 UChar toLower() const { return ref().toLower(); }
00147 UChar toUpper() const { return ref().toUpper(); }
00148 private:
00149
00150 UCharReference();
00151
00152 UChar& ref() const;
00153 UString *str;
00154 int offset;
00155 };
00156
00160 class CString {
00161 public:
00162 CString() : data(0L) { }
00163 explicit CString(const char *c);
00164 CString(const CString &);
00165
00166 ~CString();
00167
00168 CString &append(const CString &);
00169 CString &operator=(const char *c);
00170 CString &operator=(const CString &);
00171 CString &operator+=(const CString &);
00172
00173 int length() const;
00174 const char *c_str() const { return data; }
00175 private:
00176 char *data;
00177 };
00178
00182 class UString {
00183 friend bool operator==(const UString&, const UString&);
00184 friend class UCharReference;
00185 friend class UConstString;
00189 struct Rep {
00190 friend class UString;
00191 friend bool operator==(const UString&, const UString&);
00192 static Rep *create(UChar *d, int l);
00193 inline UChar *data() const { return dat; }
00194 inline int length() const { return len; }
00195
00196 inline void ref() { rc++; }
00197 inline int deref() { return --rc; }
00198
00199 UChar *dat;
00200 int len;
00201 int rc;
00202 static Rep null;
00203 };
00204
00205 public:
00209 UString();
00213 explicit UString(char c);
00217 explicit UString(UChar c);
00221 explicit UString(const char *c);
00226 UString(const UChar *c, int length);
00232 UString(UChar *c, int length, bool copy);
00236 UString(const UString &);
00241 ~UString();
00242
00246 static UString from(int i);
00250 static UString from(unsigned int u);
00254 static UString from(double d);
00255
00259 UString &append(const UString &);
00260
00264 CString cstring() const;
00272 char *ascii() const;
00273
00277 UString &operator=(const char *c);
00281 UString &operator=(const UString &);
00285 UString &operator+=(const UString &s);
00286
00290 const UChar* data() const { return rep->data(); }
00294 bool isNull() const { return (rep == &Rep::null); }
00298 bool isEmpty() const { return (!rep->len); }
00306 bool is8Bit() const;
00310 int length() const { return rep->length(); }
00314 UChar operator[](int pos) const;
00318 UCharReference operator[](int pos);
00319
00327 double toDouble(bool tolerant=false) const;
00332 unsigned long toULong(bool *ok = 0L) const;
00337 int find(const UString &f, int pos = 0) const;
00343 int rfind(const UString &f, int pos) const;
00347 UString substr(int pos = 0, int len = -1) const;
00351 static UString null;
00352
00353 private:
00354 void attach(Rep *r);
00355 void detach();
00356 void release();
00357 Rep *rep;
00358 };
00359
00360 inline bool operator==(const UChar &c1, const UChar &c2) {
00361 return (c1.uc == c2.uc);
00362 }
00363 inline bool operator!=(const UChar &c1, const UChar &c2) {
00364 return !(c1 == c2);
00365 }
00366 bool operator==(const UString& s1, const UString& s2);
00367 inline bool operator!=(const UString& s1, const UString& s2) {
00368 return !Swinder::operator==(s1, s2);
00369 }
00370 bool operator<(const UString& s1, const UString& s2);
00371 bool operator==(const UString& s1, const char *s2);
00372 inline bool operator!=(const UString& s1, const char *s2) {
00373 return !Swinder::operator==(s1, s2);
00374 }
00375 inline bool operator==(const char *s1, const UString& s2) {
00376 return operator==(s2, s1);
00377 }
00378 inline bool operator!=(const char *s1, const UString& s2) {
00379 return !Swinder::operator==(s1, s2);
00380 }
00381 bool operator==(const CString& s1, const CString& s2);
00382 UString operator+(const UString& s1, const UString& s2);
00383
00384
00385 class UConstString : private UString {
00386 public:
00387 UConstString( UChar* data, unsigned int length );
00388 ~UConstString();
00389
00390 const UString& string() const { return *this; }
00391 };
00392
00393 }
00394
00395 #endif
|