ustring.h
00001 // -*- c-basic-offset: 2 -*- 00002 /* 00003 * This file is part of the KDE libraries 00004 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) 00005 * Copyright (C) 2003 Apple Computer, Inc. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Library General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Library General Public License 00018 * along with this library; see the file COPYING.LIB. If not, write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 * Boston, MA 02110-1301, USA. 00021 * 00022 */ 00023 00024 #ifndef _KJS_USTRING_H_ 00025 #define _KJS_USTRING_H_ 00026 00027 #include <tqstring.h> 00028 #include "global.h" 00029 00033 namespace DOM { 00034 class DOMString; 00035 } 00036 class KJScript; 00037 class TQString; 00038 class TQConstString; 00039 00040 namespace KJS { 00041 00042 class UCharReference; 00043 class UString; 00044 00052 struct KJS_EXPORT UChar { 00056 UChar(); 00057 UChar(char u); 00058 UChar(unsigned char u); 00064 UChar(unsigned char h , unsigned char l); 00069 UChar(unsigned short u); 00070 UChar(const UCharReference &c); 00074 unsigned char high() const { return uc >> 8; } 00078 unsigned char low() const { return uc; } 00082 unsigned short unicode() const { return uc; } 00083 public: 00087 UChar toLower() const; 00091 UChar toUpper() const; 00095 static UChar null; 00096 00097 unsigned short uc; 00098 } KJS_PACKED; 00099 00100 inline UChar::UChar() { } 00101 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { } 00102 inline UChar::UChar(char u) : uc((unsigned char)u) { } 00103 inline UChar::UChar(unsigned char u) : uc(u) { } 00104 inline UChar::UChar(unsigned short u) : uc(u) { } 00105 00120 class KJS_EXPORT UCharReference { 00121 friend class UString; 00122 UCharReference(UString *s, unsigned int off) : str(s), offset(off) { } 00123 public: 00127 UCharReference& operator=(UChar c); 00131 UCharReference& operator=(char c) { return operator=(UChar(c)); } 00135 unsigned short unicode() const { return ref().uc; } 00139 unsigned char low() const { return ref().uc; } 00143 unsigned char high() const { return ref().uc >> 8; } 00147 UChar toLower() const { return ref().toLower(); } 00151 UChar toUpper() const { return ref().toUpper(); } 00152 private: 00153 // not implemented, can only be constructed from UString 00154 UCharReference(); 00155 00156 UChar& ref() const; 00157 UString *str; 00158 int offset; 00159 }; 00160 00161 inline UChar::UChar(const UCharReference &c) : uc(c.unicode()) { } 00162 00166 class KJS_EXPORT CString { 00167 public: 00168 CString() : data(0L), length(0) { } 00169 CString(const char *c); 00170 CString(const char *c, int len); 00171 CString(const CString &); 00172 00173 ~CString(); 00174 00175 CString &append(const CString &); 00176 CString &operator=(const char *c); 00177 CString &operator=(const CString &); 00178 CString &operator+=(const CString &c) { return append(c); } 00179 00180 int size() const { return length; } 00181 const char *c_str() const { return data; } 00182 private: 00183 char *data; 00184 int length; 00185 }; 00186 00190 class KJS_EXPORT UString { 00191 friend bool operator==(const UString&, const UString&); 00192 friend class UCharReference; 00193 friend class Identifier; 00194 friend class PropertyMap; 00195 friend class PropertyMapHashTableEntry; 00199 struct KJS_EXPORT Rep { 00200 friend class UString; 00201 friend bool operator==(const UString&, const UString&); 00202 00203 static Rep *create(UChar *d, int l); 00204 void destroy(); 00205 00206 UChar *data() const { return dat; } 00207 int size() const { return len; } 00208 00209 unsigned hash() const { if (_hash == 0) _hash = computeHash(dat, len); return _hash; } 00210 00211 static unsigned computeHash(const UChar *, int length); 00212 static unsigned computeHash(const char *); 00213 00214 void ref() { ++rc; } 00215 void deref() { if (--rc == 0) destroy(); } 00216 00217 UChar *dat; 00218 int len; 00219 int capacity; 00220 int rc; 00221 mutable unsigned _hash; 00222 00223 enum { capacityForIdentifier = 0x10000000 }; 00224 00225 static Rep null; 00226 static Rep empty; 00227 }; 00228 00229 public: 00233 UString(); 00237 explicit UString(char c); 00241 UString(const char *c); 00246 UString(const UChar *c, int length); 00253 UString(UChar *c, int length, bool copy); 00257 UString(const UString &s) { attach(s.rep); } 00265 UString(const TQString &); 00269 UString(const DOM::DOMString &); 00273 UString(const UString &, const UString &); 00278 ~UString() { release(); } 00279 00283 static UString from(int i); 00287 static UString from(unsigned int u); 00291 static UString from(long l); 00295 static UString from(double d); 00296 00300 UString &append(const UString &); 00301 00305 CString cstring() const; 00313 char *ascii() const; 00317 DOM::DOMString string() const; 00321 TQString qstring() const; 00325 TQConstString qconststring() const; 00326 00330 UString &operator=(const char *c); 00331 UString &operator=(const UString &); 00335 UString &operator+=(const UString &s) { return append(s); } 00336 00340 const UChar* data() const { return rep->data(); } 00344 bool isNull() const { return (rep == &Rep::null); } 00348 bool isEmpty() const { return (!rep->len); } 00356 bool is8Bit() const; 00360 int size() const { return rep->size(); } 00364 UChar operator[](int pos) const; 00368 UCharReference operator[](int pos); 00369 00378 double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const; 00379 double toDouble(bool tolerateTrailingJunk) const; 00380 double toDouble() const; 00387 unsigned long toULong(bool *ok, bool tolerateEmptyString) const; 00388 unsigned long toULong(bool *ok = 0) const; 00389 00390 unsigned int toUInt32(bool *ok = 0) const; 00391 unsigned int toStrictUInt32(bool *ok = 0) const; 00392 00399 unsigned toArrayIndex(bool *ok = 0) const; 00400 00404 UString toLower() const; 00408 UString toUpper() const; 00413 int find(const UString &f, int pos = 0) const; 00414 int find(UChar, int pos = 0) const; 00420 int rfind(const UString &f, int pos) const; 00421 int rfind(UChar, int pos) const; 00425 UString substr(int pos = 0, int len = -1) const; 00429 static UString null; 00430 #ifdef KJS_DEBUG_MEM 00431 00434 static void globalClear(); 00435 #endif 00436 private: 00437 UString(Rep *r) { attach(r); } 00438 void attach(Rep *r); 00439 void detach(); 00440 void release(); 00441 Rep *rep; 00442 }; 00443 00444 KJS_EXPORT inline bool operator==(const UChar &c1, const UChar &c2) { 00445 return (c1.uc == c2.uc); 00446 } 00447 KJS_EXPORT inline bool operator!=(const UChar& c1, const UChar& c2) { 00448 return !KJS::operator==(c1, c2); 00449 } 00450 KJS_EXPORT bool operator==(const UString& s1, const UString& s2); 00451 inline bool operator!=(const UString& s1, const UString& s2) { 00452 return !KJS::operator==(s1, s2); 00453 } 00454 KJS_EXPORT bool operator<(const UString& s1, const UString& s2); 00455 KJS_EXPORT bool operator==(const UString& s1, const char *s2); 00456 KJS_EXPORT inline bool operator!=(const UString& s1, const char *s2) { 00457 return !KJS::operator==(s1, s2); 00458 } 00459 KJS_EXPORT inline bool operator==(const char *s1, const UString& s2) { 00460 return operator==(s2, s1); 00461 } 00462 KJS_EXPORT inline bool operator!=(const char *s1, const UString& s2) { 00463 return !KJS::operator==(s1, s2); 00464 } 00465 KJS_EXPORT bool operator==(const CString& s1, const CString& s2); 00466 KJS_EXPORT inline bool operator!=(const CString& s1, const CString& s2) { 00467 return !KJS::operator==(s1, s2); 00468 } 00469 KJS_EXPORT inline UString operator+(const UString& s1, const UString& s2) { 00470 return UString(s1, s2); 00471 } 00472 00473 KJS_EXPORT int compare(const UString &, const UString &); 00474 00475 } // namespace 00476 00477 #endif