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  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public License
00017  *  along with this library; see the file COPYING.LIB.  If not, write to
00018  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  *  Boston, MA 02111-1307, USA.
00020  *
00021  *  $Id: ustring.h,v 1.46.2.1 2003/05/17 11:19:25 mueller Exp $
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();
00061     UChar(unsigned char h , unsigned char l);
00066     UChar(unsigned short u);
00067     UChar(const UCharReference &c);
00071     unsigned char high() const { return uc >> 8; }
00075     unsigned char low() const { return uc & 0xFF; }
00079     unsigned short unicode() const { return uc; }
00080   public:
00084     UChar toLower() const;
00088     UChar toUpper() const;
00092     static UChar null;
00093   private:
00094     friend class UCharReference;
00095     friend class UString;
00096     friend bool operator==(const UChar &c1, const UChar &c2);
00097     friend bool operator==(const UString& s1, const char *s2);
00098     friend bool operator<(const UString& s1, const UString& s2);
00099 
00100     unsigned short uc;
00101   } KJS_PACKED;
00102 
00103   inline UChar::UChar() : uc(0) { }
00104   inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
00105   inline UChar::UChar(unsigned short u) : uc(u) { }
00106 
00121   class UCharReference {
00122     friend class UString;
00123     UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
00124   public:
00128     UCharReference& operator=(UChar c);
00132     UCharReference& operator=(char c) { return operator=(UChar(c)); }
00136     unsigned short unicode() const { return ref().unicode(); }
00140     unsigned char low() const { return ref().uc & 0xFF; }
00144     unsigned char high() const { return ref().uc >> 8; }
00148     UChar toLower() const { return ref().toLower(); }
00152     UChar toUpper() const  { return ref().toUpper(); }
00153   private:
00154     // not implemented, can only be constructed from UString
00155     UCharReference();
00156 
00157     UChar& ref() const;
00158     UString *str;
00159     int offset;
00160   };
00161 
00165   class CString {
00166   public:
00167     CString() : data(0L) { }
00168     CString(const char *c);
00169     CString(const CString &);
00170 
00171     ~CString();
00172 
00173     CString &append(const CString &);
00174     CString &operator=(const char *c);
00175     CString &operator=(const CString &);
00176     CString &operator+=(const CString &);
00177 
00178     int size() const;
00179     const char *c_str() const { return data; }
00180   private:
00181     char *data;
00182   };
00183 
00187   class UString {
00188     friend bool operator==(const UString&, const UString&);
00189     friend class UCharReference;
00193     struct Rep {
00194       friend class UString;
00195       friend bool operator==(const UString&, const UString&);
00196       static Rep *create(UChar *d, int l);
00197       inline UChar *data() const { return dat; }
00198       inline int size() const { return len; }
00199 
00200       inline void ref() { rc++; }
00201       inline int deref() { return --rc; }
00202 
00203       UChar *dat;
00204       int len;
00205       int rc;
00206       static Rep null;
00207     };
00208 
00209   public:
00213     UString();
00217     UString(char c);
00221     UString(const char *c);
00226     UString(const UChar *c, int length);
00233     UString(UChar *c, int length, bool copy);
00237     UString(const UString &);
00245     UString(const QString &);
00249     UString(const DOM::DOMString &);
00254     ~UString();
00255 
00259     static UString from(int i);
00263     static UString from(unsigned int u);
00267     static UString from(double d);
00268 
00272     UString &append(const UString &);
00273 
00277     CString cstring() const;
00285     char *ascii() const;
00289     DOM::DOMString string() const;
00293     QString qstring() const;
00297     QConstString qconststring() const;
00298 
00302     UString &operator=(const char *c);
00306     UString &operator=(const UString &);
00310     UString &operator+=(const UString &s);
00311 
00315     const UChar* data() const { return rep->data(); }
00319     bool isNull() const { return (rep == &Rep::null); }
00323     bool isEmpty() const { return (!rep->len); }
00331     bool is8Bit() const;
00335     int size() const { return rep->size(); }
00339     UChar operator[](int pos) const;
00343     UCharReference operator[](int pos);
00344 
00352     double toDouble(bool tolerant=false) const;
00357     unsigned long toULong(bool *ok = 0L) const;
00361     UString toLower() const;
00365     UString toUpper() const;
00370     int find(const UString &f, int pos = 0) const;
00376     int rfind(const UString &f, int pos) const;
00380     UString substr(int pos = 0, int len = -1) const;
00384     static UString null;
00385 #ifdef KJS_DEBUG_MEM
00386 
00389     static void globalClear();
00390 #endif
00391   private:
00392     void attach(Rep *r);
00393     void detach();
00394     void release();
00395     Rep *rep;
00396   };
00397 
00398   inline bool operator==(const UChar &c1, const UChar &c2) {
00399     return (c1.uc == c2.uc);
00400   }
00401   bool operator==(const UString& s1, const UString& s2);
00402   inline bool operator!=(const UString& s1, const UString& s2) {
00403     return !KJS::operator==(s1, s2);
00404   }
00405   bool operator<(const UString& s1, const UString& s2);
00406   bool operator==(const UString& s1, const char *s2);
00407   inline bool operator!=(const UString& s1, const char *s2) {
00408     return !KJS::operator==(s1, s2);
00409   }
00410   inline bool operator==(const char *s1, const UString& s2) {
00411     return operator==(s2, s1);
00412   }
00413   inline bool operator!=(const char *s1, const UString& s2) {
00414     return !KJS::operator==(s1, s2);
00415   }
00416   bool operator==(const CString& s1, const CString& s2);
00417   UString operator+(const UString& s1, const UString& s2);
00418 
00419 } // namespace
00420 
00421 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:37 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001