kjs Library API Documentation

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., 59 Temple Place - Suite 330,
00020  *  Boston, MA 02111-1307, USA.
00021  *
00022  */
00023 
00024 #ifndef _KJS_USTRING_H_
00025 #define _KJS_USTRING_H_
00026 
00027 #include <kjs/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 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 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 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 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 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       int 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 int _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;
00385     unsigned long toULong(bool *ok, bool tolerateEmptyString) const;
00386     unsigned long toULong(bool *ok = 0) const;
00387 
00388     unsigned int toStrictUInt32(bool *ok = 0) const;
00389 
00396     unsigned toArrayIndex(bool *ok = 0) const;
00397 
00401     UString toLower() const;
00405     UString toUpper() const;
00410     int find(const UString &f, int pos = 0) const;
00411     int find(UChar, int pos = 0) const;
00417     int rfind(const UString &f, int pos) const;
00418     int rfind(UChar, int pos) const;
00422     UString substr(int pos = 0, int len = -1) const;
00426     static UString null;
00427 #ifdef KJS_DEBUG_MEM
00428 
00431     static void globalClear();
00432 #endif
00433   private:
00434     UString(Rep *r) { attach(r); }
00435     void attach(Rep *r);
00436     void detach();
00437     void release();
00438     Rep *rep;
00439   };
00440 
00441   inline bool operator==(const UChar &c1, const UChar &c2) {
00442     return (c1.uc == c2.uc);
00443   }
00444   inline bool operator!=(const UChar& c1, const UChar& c2) {
00445     return !KJS::operator==(c1, c2);
00446   }
00447   bool operator==(const UString& s1, const UString& s2);
00448   inline bool operator!=(const UString& s1, const UString& s2) {
00449     return !KJS::operator==(s1, s2);
00450   }
00451   bool operator<(const UString& s1, const UString& s2);
00452   bool operator==(const UString& s1, const char *s2);
00453   inline bool operator!=(const UString& s1, const char *s2) {
00454     return !KJS::operator==(s1, s2);
00455   }
00456   inline bool operator==(const char *s1, const UString& s2) {
00457     return operator==(s2, s1);
00458   }
00459   inline bool operator!=(const char *s1, const UString& s2) {
00460     return !KJS::operator==(s1, s2);
00461   }
00462   bool operator==(const CString& s1, const CString& s2);
00463   inline bool operator!=(const CString& s1, const CString& s2) {
00464     return !KJS::operator==(s1, s2);
00465   }
00466   inline UString operator+(const UString& s1, const UString& s2) {
00467     return UString(s1, s2);
00468   }
00469 
00470   int compare(const UString &, const UString &);
00471 
00472 } // namespace
00473 
00474 #endif
KDE Logo
This file is part of the documentation for kjs Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 21 18:43:31 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003