filters

ustring.h

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
00004  *  Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library 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  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public License
00017  *  along with this library; see the file COPYING.LIB.  If not, write to
00018  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #ifndef SWINDER_USTRING_H_
00023 #define SWINDER_USTRING_H_
00024 
00025 namespace Swinder {
00026 
00027   class UCharReference;
00028   class UString;
00029   class UConstString;
00030 
00038   struct UChar {
00042     UChar();
00048     UChar(unsigned char h , unsigned char l);
00053     UChar(unsigned short u);
00054 
00055     UChar(unsigned char u);
00056     UChar(char u);
00057     UChar(unsigned int u);
00058 
00059     UChar(const UCharReference &c);
00063     unsigned char high() const { return uc >> 8; }
00067     unsigned char low() const { return uc & 0xFF; }
00071     unsigned short unicode() const { return uc; }
00072   public:
00076     static UChar null;
00077   private:
00078     friend class UCharReference;
00079     friend class UString;
00080     friend bool operator==(const UChar &c1, const UChar &c2);
00081     friend bool operator==(const UString& s1, const char *s2);
00082     friend bool operator<(const UString& s1, const UString& s2);
00083 
00084     unsigned short uc;
00085   };
00086 
00087   inline UChar::UChar() : uc(0) { }
00088   inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
00089   inline UChar::UChar(unsigned short u) : uc(u) { }
00090   inline UChar::UChar(unsigned int u) : uc(u) { }
00091   inline UChar::UChar(unsigned char u) : uc(u) { }
00092   inline UChar::UChar(char u) : uc((unsigned char)u) { }
00093 
00108   class UCharReference {
00109     friend class UString;
00110     UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
00111   public:
00115     UCharReference& operator=(UChar c);
00119     UCharReference& operator=(char c) { return operator=(UChar(c)); }
00123     unsigned short unicode() const { return ref().uc; }
00127     unsigned char low() const { return ref().uc & 0xFF; }
00131     unsigned char high() const { return ref().uc >> 8; }
00132   private:
00133     // not implemented, can only be constructed from UString
00134     UCharReference();
00135 
00136     UChar& ref() const;
00137     UString *str;
00138     int offset;
00139   };
00140 
00141 
00145   class UString {
00146     friend bool operator==(const UString&, const UString&);
00147     friend class UCharReference;
00148     friend class UConstString;
00152     struct Rep {
00153       friend class UString;
00154       friend bool operator==(const UString&, const UString&);
00155       static Rep *create(UChar *d, int l);
00156       static Rep *create(UChar *d, int l, int c);
00157       inline UChar *data() const { return dat; }
00158       inline int length() const { return len; }
00159       inline int capacity() const { return cap; }
00160 
00161       inline void ref() { rc++; }
00162       inline int deref() { return --rc; }
00163 
00164       UChar *dat;
00165       int len;
00166       int rc;
00167       int cap;
00168       static Rep null;
00169     };
00170 
00171   public:
00175     UString();
00179     explicit UString(char c);
00183     explicit UString(UChar c);
00187     explicit UString(const char *c);
00192     UString(const UChar *c, int length);
00198     UString(UChar *c, int length, bool copy);
00202     UString(const UString &);
00207     ~UString();
00208 
00212     static UString number(int i);
00216     static UString number(unsigned int u);
00220     static UString number(double d);
00221 
00225     UString &append(const UString &);
00229     UString &append(UChar c);
00233     UString &append(const char*);
00237     UString &append(char c);
00238     
00242     UString &prepend(const UString &);
00246     UString &prepend(UChar c);
00250     UString &prepend(const char*);
00254     UString &prepend(char c);
00255     
00263     char *ascii() const;
00264 
00268     UString &operator=(const char *c);
00272     UString &operator=(const UString &);
00276     UString &operator+=(const UString &s);
00277 
00281     const UChar* data() const { return rep->data(); }
00285     bool isNull() const { return (rep == &Rep::null); }
00289     bool isEmpty() const { return (!rep->len); }
00297     bool is8Bit() const;
00301     int length() const { return rep->length(); }
00302     
00306     void truncate(int n);
00307     
00311     int capacity() const { return rep->capacity(); }
00315     void reserve(int r);
00316     
00320     UChar operator[](int pos) const;
00324     UCharReference operator[](int pos);
00325 
00326 
00330     UString substr(int pos = 0, int len = -1) const;
00331 
00336     int find(const UString &f, int pos = 0) const;
00337 
00341     static UString null;
00342 
00343   private:
00344     UString(Rep* r);
00345     void attach(Rep *r);
00346     void detach();
00347     void release();
00348     Rep *rep;
00349   };
00350 
00351   inline bool operator==(const UChar &c1, const UChar &c2) {
00352     return (c1.uc == c2.uc);
00353   }
00354   inline bool operator!=(const UChar &c1, const UChar &c2) {
00355     return !(c1 == c2);
00356   }
00357   bool operator==(const UString& s1, const UString& s2);
00358   inline bool operator!=(const UString& s1, const UString& s2) {
00359     return !Swinder::operator==(s1, s2);
00360   }
00361   bool operator<(const UString& s1, const UString& s2);
00362   bool operator==(const UString& s1, const char *s2);
00363   inline bool operator!=(const UString& s1, const char *s2) {
00364     return !Swinder::operator==(s1, s2);
00365   }
00366   inline bool operator==(const char *s1, const UString& s2) {
00367     return operator==(s2, s1);
00368   }
00369   inline bool operator!=(const char *s1, const UString& s2) {
00370     return !Swinder::operator==(s1, s2);
00371   }
00372   UString operator+(const UString& s1, const UString& s2);
00373 
00374 
00375   class UConstString : private UString {
00376     public:
00377       UConstString( UChar* data, unsigned int length );
00378       ~UConstString();
00379 
00380       const UString& string() const { return *this; }
00381   };
00382 
00383 } // namespace SWINDER_USTRING_H
00384 
00385 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys