kpresenter/powerpoint/libppt/ustring.h
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef LIBPPT_USTRING_H_
22#define LIBPPT_USTRING_H_
23
24namespace Libppt {
25
29 bool isNaN(double d);
30
31 bool isPosInf(double d);
32 bool isNegInf(double d);
33
34 class UCharReference;
35 class UString;
36 class UConstString;
37
45 struct UChar {
49 UChar();
55 UChar(unsigned char h , unsigned char l);
60 UChar(unsigned short u);
61 UChar(const UCharReference &c);
65 unsigned char high() const { return uc >> 8; }
69 unsigned char low() const { return uc & 0xFF; }
73 unsigned short unicode() const { return uc; }
74 public:
78 UChar toLower() const;
82 UChar toUpper() const;
86 static UChar null;
87 private:
88 friend class UCharReference;
89 friend class UString;
90 friend bool operator==(const UChar &c1, const UChar &c2);
91 friend bool operator==(const UString& s1, const char *s2);
92 friend bool operator<(const UString& s1, const UString& s2);
93
94 unsigned short uc;
95 };
96
97 inline UChar::UChar() : uc(0) { }
98 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
99 inline UChar::UChar(unsigned short u) : uc(u) { }
100
116 friend class UString;
117 UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
118 public:
126 UCharReference& operator=(char c) { return operator=(UChar(c)); }
130 unsigned short unicode() const { return ref().unicode(); }
134 unsigned char low() const { return ref().uc & 0xFF; }
138 unsigned char high() const { return ref().uc >> 8; }
142 UChar toLower() const { return ref().toLower(); }
146 UChar toUpper() const { return ref().toUpper(); }
147 private:
148 // not implemented, can only be constructed from UString
150
151 UChar& ref() const;
152 UString *str;
153 int offset;
154 };
155
159 class CString {
160 public:
161 CString() : data(0L) { }
162 explicit CString(const char *c);
163 CString(const CString &);
164
165 ~CString();
166
167 CString &append(const CString &);
168 CString &operator=(const char *c);
169 CString &operator=(const CString &);
170 CString &operator+=(const CString &);
171
172 int length() const;
173 const char *c_str() const { return data; }
174 private:
175 char *data;
176 };
177
181 class UString {
182 friend bool operator==(const UString&, const UString&);
183 friend class UCharReference;
184 friend class UConstString;
188 struct Rep {
189 friend class UString;
190 friend bool operator==(const UString&, const UString&);
191 static Rep *create(UChar *d, int l);
192 inline UChar *data() const { return dat; }
193 inline int length() const { return len; }
194
195 inline void ref() { rc++; }
196 inline int deref() { return --rc; }
197
198 UChar *dat;
199 int len;
200 int rc;
201 static Rep null;
202 };
203
204 public:
208 UString();
212 explicit UString(char c);
216 explicit UString(UChar c);
220 explicit UString(const char *c);
225 UString(const UChar *c, int length);
231 UString(UChar *c, int length, bool copy);
235 UString(const UString &);
240 ~UString();
241
245 static UString from(int i);
249 static UString from(unsigned int u);
253 static UString from(double d);
254
258 UString &append(const UString &);
259
263 CString cstring() const;
271 char *ascii() const;
272
276 UString &operator=(const char *c);
280 UString &operator=(const UString &);
284 UString &operator+=(const UString &s);
285
289 const UChar* data() const { return rep->data(); }
293 bool isNull() const { return (rep == &Rep::null); }
297 bool isEmpty() const { return (!rep->len); }
305 bool is8Bit() const;
309 int length() const { return rep->length(); }
313 UChar operator[](int pos) const;
317 UCharReference operator[](int pos);
318
326 double toDouble(bool tolerant=false) const;
331 unsigned long toULong(bool *ok = 0L) const;
336 int find(const UString &f, int pos = 0) const;
342 int rfind(const UString &f, int pos) const;
346 UString substr(int pos = 0, int len = -1) const;
350 static UString null;
351
352 private:
353 void attach(Rep *r);
354 void detach();
355 void release();
356 Rep *rep;
357 };
358
359 inline bool operator==(const UChar &c1, const UChar &c2) {
360 return (c1.uc == c2.uc);
361 }
362 inline bool operator!=(const UChar &c1, const UChar &c2) {
363 return !(c1 == c2);
364 }
365 bool operator==(const UString& s1, const UString& s2);
366 inline bool operator!=(const UString& s1, const UString& s2) {
367 return !Libppt::operator==(s1, s2);
368 }
369 bool operator<(const UString& s1, const UString& s2);
370 bool operator==(const UString& s1, const char *s2);
371 inline bool operator!=(const UString& s1, const char *s2) {
372 return !Libppt::operator==(s1, s2);
373 }
374 inline bool operator==(const char *s1, const UString& s2) {
375 return operator==(s2, s1);
376 }
377 inline bool operator!=(const char *s1, const UString& s2) {
378 return !Libppt::operator==(s1, s2);
379 }
380 bool operator==(const CString& s1, const CString& s2);
381 UString operator+(const UString& s1, const UString& s2);
382
383
384 class UConstString : private UString {
385 public:
386 UConstString( UChar* data, unsigned int length );
387 ~UConstString();
388
389 const UString& string() const { return *this; }
390 };
391
392}
393
394#endif /* LIBPPT_USTRING_H_ */
8 bit char based string class
Dynamic reference to a string character.
UCharReference & operator=(UChar c)
Set the referenced character to c.
UCharReference & operator=(char c)
Same operator as above except the argument that it takes.
bool is8Bit() const
Use this if you want to make sure that this string is a plain ASCII string.
int find(const UString &f, int pos=0) const
int rfind(const UString &f, int pos) const
UString & operator=(const char *c)
Assignment operator.
char * ascii() const
Convert the Unicode string to plain ASCII chars chopping of any higher bytes.
UString & operator+=(const UString &s)
Appends the specified string.
double toDouble(bool tolerant=false) const
Attempts an conversion to a number.
static UString null
Static instance of a null string.
UString substr(int pos=0, int len=-1) const
unsigned long toULong(bool *ok=0L) const
Attempts an conversion to an unsigned long integer.
UChar operator[](int pos) const
Const character at specified position.
UString & append(const UString &)
Append another string.
static UString from(int i)
Constructs a string from an int.
static UChar null
A static instance of UChar(0).
UChar()
Construct a character with value 0.