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 "global.h"
00028 
00032 namespace DOM {
00033   class DOMString;
00034 }
00035 class KJScript;
00036 class QString;
00037 class QConstString;
00038 
00039 namespace KJS {
00040 
00041   class UCharReference;
00042   class UString;
00043 
00051   struct KJS_EXPORT UChar {
00055     UChar();
00056     UChar(char u);
00057     UChar(unsigned char u);
00063     UChar(unsigned char h , unsigned char l);
00068     UChar(unsigned short u);
00069     UChar(const UCharReference &c);
00073     unsigned char high() const { return uc >> 8; }
00077     unsigned char low() const { return uc; }
00081     unsigned short unicode() const { return uc; }
00082   public:
00086     UChar toLower() const;
00090     UChar toUpper() const;
00094     static UChar null;
00095 
00096     unsigned short uc;
00097   } KJS_PACKED;
00098 
00099   inline UChar::UChar() { }
00100   inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
00101   inline UChar::UChar(char u) : uc((unsigned char)u) { }
00102   inline UChar::UChar(unsigned char u) : uc(u) { }
00103   inline UChar::UChar(unsigned short u) : uc(u) { }
00104 
00119   class KJS_EXPORT UCharReference {
00120     friend class UString;
00121     UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
00122   public:
00126     UCharReference& operator=(UChar c);
00130     UCharReference& operator=(char c) { return operator=(UChar(c)); }
00134     unsigned short unicode() const { return ref().uc; }
00138     unsigned char low() const { return ref().uc; }
00142     unsigned char high() const { return ref().uc >> 8; }
00146     UChar toLower() const { return ref().toLower(); }
00150     UChar toUpper() const  { return ref().toUpper(); }
00151   private:
00152     // not implemented, can only be constructed from UString
00153     UCharReference();
00154 
00155     UChar& ref() const;
00156     UString *str;
00157     int offset;
00158   };
00159 
00160   inline UChar::UChar(const UCharReference &c) : uc(c.unicode()) { }
00161 
00165   class KJS_EXPORT CString {
00166   public:
00167     CString() : data(0L), length(0) { }
00168     CString(const char *c);
00169     CString(const char *c, int len);
00170     CString(const CString &);
00171 
00172     ~CString();
00173 
00174     CString &append(const CString &);
00175     CString &operator=(const char *c);
00176     CString &operator=(const CString &);
00177     CString &operator+=(const CString &c) { return append(c); }
00178 
00179     int size() const { return length; }
00180     const char *c_str() const { return data; }
00181   private:
00182     char *data;
00183     int length;
00184   };
00185 
00189   class KJS_EXPORT UString {
00190     friend bool operator==(const UString&, const UString&);
00191     friend class UCharReference;
00192     friend class Identifier;
00193     friend class PropertyMap;
00194     friend class PropertyMapHashTableEntry;
00198     struct KJS_EXPORT Rep {
00199       friend class UString;
00200       friend bool operator==(const UString&, const UString&);
00201 
00202       static Rep *create(UChar *d, int l);
00203       void destroy();
00204 
00205       UChar *data() const { return dat; }
00206       int size() const { return len; }
00207 
00208       unsigned hash() const { if (_hash == 0) _hash = computeHash(dat, len); return _hash; }
00209 
00210       static unsigned computeHash(const UChar *, int length);
00211       static unsigned computeHash(const char *);
00212 
00213       void ref() { ++rc; }
00214       void deref() { if (--rc == 0) destroy(); }
00215 
00216       UChar *dat;
00217       int len;
00218       int capacity;
00219       int rc;
00220       mutable unsigned _hash;
00221 
00222       enum { capacityForIdentifier = 0x10000000 };
00223 
00224       static Rep null;
00225       static Rep empty;
00226     };
00227 
00228   public:
00232     UString();
00236     explicit UString(char c);
00240     UString(const char *c);
00245     UString(const UChar *c, int length);
00252     UString(UChar *c, int length, bool copy);
00256     UString(const UString &s) { attach(s.rep); }
00264     UString(const QString &);
00268     UString(const DOM::DOMString &);
00272     UString(const UString &, const UString &);
00277     ~UString() { release(); }
00278 
00282     static UString from(int i);
00286     static UString from(unsigned int u);
00290     static UString from(long l);
00294     static UString from(double d);
00295 
00299     UString &append(const UString &);
00300 
00304     CString cstring() const;
00312     char *ascii() const;
00316     DOM::DOMString string() const;
00320     QString qstring() const;
00324     QConstString qconststring() const;
00325 
00329     UString &operator=(const char *c);
00330     UString &operator=(const UString &);
00334     UString &operator+=(const UString &s) { return append(s); }
00335 
00339     const UChar* data() const { return rep->data(); }
00343     bool isNull() const { return (rep == &Rep::null); }
00347     bool isEmpty() const { return (!rep->len); }
00355     bool is8Bit() const;
00359     int size() const { return rep->size(); }
00363     UChar operator[](int pos) const;
00367     UCharReference operator[](int pos);
00368 
00377     double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
00378     double toDouble(bool tolerateTrailingJunk) const;
00379     double toDouble() const;
00386     unsigned long toULong(bool *ok, bool tolerateEmptyString) const;
00387     unsigned long toULong(bool *ok = 0) const;
00388 
00389     unsigned int toUInt32(bool *ok = 0) const;
00390     unsigned int toStrictUInt32(bool *ok = 0) const;
00391 
00398     unsigned toArrayIndex(bool *ok = 0) const;
00399 
00403     UString toLower() const;
00407     UString toUpper() const;
00412     int find(const UString &f, int pos = 0) const;
00413     int find(UChar, int pos = 0) const;
00419     int rfind(const UString &f, int pos) const;
00420     int rfind(UChar, int pos) const;
00424     UString substr(int pos = 0, int len = -1) const;
00428     static UString null;
00429 #ifdef KJS_DEBUG_MEM
00430 
00433     static void globalClear();
00434 #endif
00435   private:
00436     UString(Rep *r) { attach(r); }
00437     void attach(Rep *r);
00438     void detach();
00439     void release();
00440     Rep *rep;
00441   };
00442 
00443   KJS_EXPORT inline bool operator==(const UChar &c1, const UChar &c2) {
00444     return (c1.uc == c2.uc);
00445   }
00446   KJS_EXPORT inline bool operator!=(const UChar& c1, const UChar& c2) {
00447     return !KJS::operator==(c1, c2);
00448   }
00449   KJS_EXPORT bool operator==(const UString& s1, const UString& s2);
00450   inline bool operator!=(const UString& s1, const UString& s2) {
00451     return !KJS::operator==(s1, s2);
00452   }
00453   KJS_EXPORT bool operator<(const UString& s1, const UString& s2);
00454   KJS_EXPORT bool operator==(const UString& s1, const char *s2);
00455   KJS_EXPORT inline bool operator!=(const UString& s1, const char *s2) {
00456     return !KJS::operator==(s1, s2);
00457   }
00458   KJS_EXPORT inline bool operator==(const char *s1, const UString& s2) {
00459     return operator==(s2, s1);
00460   }
00461   KJS_EXPORT inline bool operator!=(const char *s1, const UString& s2) {
00462     return !KJS::operator==(s1, s2);
00463   }
00464   KJS_EXPORT bool operator==(const CString& s1, const CString& s2);
00465   KJS_EXPORT inline bool operator!=(const CString& s1, const CString& s2) {
00466     return !KJS::operator==(s1, s2);
00467   }
00468   KJS_EXPORT inline UString operator+(const UString& s1, const UString& s2) {
00469     return UString(s1, s2);
00470   }
00471 
00472   KJS_EXPORT int compare(const UString &, const UString &);
00473 
00474 } // namespace
00475 
00476 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys