kspread/excel/sidewinder/ustring.h
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#ifndef SWINDER_USTRING_H_
23#define SWINDER_USTRING_H_
24
25namespace Swinder {
26
27 class UCharReference;
28 class UString;
29 class UConstString;
30
38 struct UChar {
42 UChar();
48 UChar(unsigned char h , unsigned char l);
53 UChar(unsigned short u);
54
55 UChar(unsigned char u);
56 UChar(char u);
57 UChar(unsigned int u);
58
59 UChar(const UCharReference &c);
63 unsigned char high() const { return uc >> 8; }
67 unsigned char low() const { return uc & 0xFF; }
71 unsigned short unicode() const { return uc; }
72 public:
76 static UChar null;
77 private:
78 friend class UCharReference;
79 friend class UString;
80 friend bool operator==(const UChar &c1, const UChar &c2);
81 friend bool operator==(const UString& s1, const char *s2);
82 friend bool operator<(const UString& s1, const UString& s2);
83
84 unsigned short uc;
85 };
86
87 inline UChar::UChar() : uc(0) { }
88 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
89 inline UChar::UChar(unsigned short u) : uc(u) { }
90 inline UChar::UChar(unsigned int u) : uc(u) { }
91 inline UChar::UChar(unsigned char u) : uc(u) { }
92 inline UChar::UChar(char u) : uc((unsigned char)u) { }
93
109 friend class UString;
110 UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
111 public:
119 UCharReference& operator=(char c) { return operator=(UChar(c)); }
123 unsigned short unicode() const { return ref().uc; }
127 unsigned char low() const { return ref().uc & 0xFF; }
131 unsigned char high() const { return ref().uc >> 8; }
132 private:
133 // not implemented, can only be constructed from UString
135
136 UChar& ref() const;
137 UString *str;
138 int offset;
139 };
140
141
145 class UString {
146 friend bool operator==(const UString&, const UString&);
147 friend class UCharReference;
148 friend class UConstString;
152 struct Rep {
153 friend class UString;
154 friend bool operator==(const UString&, const UString&);
155 static Rep *create(UChar *d, int l);
156 static Rep *create(UChar *d, int l, int c);
157 inline UChar *data() const { return dat; }
158 inline int length() const { return len; }
159 inline int capacity() const { return cap; }
160
161 inline void ref() { rc++; }
162 inline int deref() { return --rc; }
163
164 UChar *dat;
165 int len;
166 int rc;
167 int cap;
168 static Rep null;
169 };
170
171 public:
175 UString();
179 explicit UString(char c);
183 explicit UString(UChar c);
187 explicit UString(const char *c);
192 UString(const UChar *c, int length);
198 UString(UChar *c, int length, bool copy);
202 UString(const UString &);
207 ~UString();
208
212 static UString number(int i);
216 static UString number(unsigned int u);
220 static UString number(double d);
221
225 UString &append(const UString &);
229 UString &append(UChar c);
233 UString &append(const char*);
237 UString &append(char c);
238
242 UString &prepend(const UString &);
250 UString &prepend(const char*);
254 UString &prepend(char c);
255
263 char *ascii() const;
264
268 UString &operator=(const char *c);
272 UString &operator=(const UString &);
276 UString &operator+=(const UString &s);
277
281 const UChar* data() const { return rep->data(); }
285 bool isNull() const { return (rep == &Rep::null); }
289 bool isEmpty() const { return (!rep->len); }
297 bool is8Bit() const;
301 int length() const { return rep->length(); }
302
306 void truncate(int n);
307
311 int capacity() const { return rep->capacity(); }
315 void reserve(int r);
316
320 UChar operator[](int pos) const;
324 UCharReference operator[](int pos);
325
326
330 UString substr(int pos = 0, int len = -1) const;
331
336 int find(const UString &f, int pos = 0) const;
337
341 static UString null;
342
343 private:
344 UString(Rep* r);
345 void attach(Rep *r);
346 void detach();
347 void release();
348 Rep *rep;
349 };
350
351 inline bool operator==(const UChar &c1, const UChar &c2) {
352 return (c1.uc == c2.uc);
353 }
354 inline bool operator!=(const UChar &c1, const UChar &c2) {
355 return !(c1 == c2);
356 }
357 bool operator==(const UString& s1, const UString& s2);
358 inline bool operator!=(const UString& s1, const UString& s2) {
359 return !Swinder::operator==(s1, s2);
360 }
361 bool operator<(const UString& s1, const UString& s2);
362 bool operator==(const UString& s1, const char *s2);
363 inline bool operator!=(const UString& s1, const char *s2) {
364 return !Swinder::operator==(s1, s2);
365 }
366 inline bool operator==(const char *s1, const UString& s2) {
367 return operator==(s2, s1);
368 }
369 inline bool operator!=(const char *s1, const UString& s2) {
370 return !Swinder::operator==(s1, s2);
371 }
372 UString operator+(const UString& s1, const UString& s2);
373
374
375 class UConstString : private UString {
376 public:
377 UConstString( UChar* data, unsigned int length );
378 ~UConstString();
379
380 const UString& string() const { return *this; }
381 };
382
383} // namespace SWINDER_USTRING_H
384
385#endif
Dynamic reference to a string character.
UCharReference & operator=(char c)
Same operator as above except the argument that it takes.
UCharReference & operator=(UChar c)
Set the referenced character to c.
bool is8Bit() const
Use this if you want to make sure that this string is a plain ASCII string.
UString & prepend(const UString &)
Prepend another string.
int find(const UString &f, int pos=0) const
static UString number(int i)
Constructs a string from an int.
UString()
Constructs a null string.
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.
void truncate(int n)
Truncates the string to n.
static UString null
Static instance of a null string.
UString substr(int pos=0, int len=-1) const
void reserve(int r)
Reserves room for the string, useful to speed up append().
UChar operator[](int pos) const
Const character at specified position.
UString & append(const UString &)
Append another string.
static UChar null
A static instance of UChar(0).
UChar()
Construct a character with value 0.
unsigned short unicode() const