1
// -*- c-basic-offset: 2 -*-
3
* This file is part of the KDE libraries
4
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
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.
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.
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., 59 Temple Place - Suite 330,
19
* Boston, MA 02111-1307, USA.
22
#ifndef SIDEWINDER_USTRING_H_
23
#define SIDEWINDER_USTRING_H_
25
namespace Sidewinder {
28
* @return True if d is not a number (platform support required).
32
bool isPosInf(double d);
33
bool isNegInf(double d);
40
* @short Unicode character.
42
* UChar represents a 16 bit Unicode character. It's internal data
43
* representation is compatible to XChar2b and QChar. It's therefore
44
* possible to exchange data with X and Qt with shallow copies.
48
* Construct a character with value 0.
52
* Construct a character with the value denoted by the arguments.
53
* @param h higher byte
56
UChar(unsigned char h , unsigned char l);
58
* Construct a character with the given value.
59
* @param u 16 bit Unicode value
61
UChar(unsigned short u);
62
UChar(const UCharReference &c);
64
* @return The higher byte of the character.
66
unsigned char high() const { return uc >> 8; }
68
* @return The lower byte of the character.
70
unsigned char low() const { return uc & 0xFF; }
72
* @return the 16 bit Unicode value of the character
74
unsigned short unicode() const { return uc; }
77
* @return The character converted to lower case.
79
UChar toLower() const;
81
* @return The character converted to upper case.
83
UChar toUpper() const;
85
* A static instance of UChar(0).
89
friend class UCharReference;
91
friend bool operator==(const UChar &c1, const UChar &c2);
92
friend bool operator==(const UString& s1, const char *s2);
93
friend bool operator<(const UString& s1, const UString& s2);
98
inline UChar::UChar() : uc(0) { }
99
inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
100
inline UChar::UChar(unsigned short u) : uc(u) { }
103
* @short Dynamic reference to a string character.
105
* UCharReference is the dynamic counterpart of @ref UChar. It's used when
106
* characters retrieved via index from a @ref UString are used in an
107
* assignment expression (and therefore can't be treated as being const):
109
* UString s("hello world");
113
* If that sounds confusing your best bet is to simply forget about the
114
* existance of this class and treat is as being identical to @ref UChar.
116
class UCharReference {
117
friend class UString;
118
UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
121
* Set the referenced character to c.
123
UCharReference& operator=(UChar c);
125
* Same operator as above except the argument that it takes.
127
UCharReference& operator=(char c) { return operator=(UChar(c)); }
129
* @return Unicode value.
131
unsigned short unicode() const { return ref().unicode(); }
133
* @return Lower byte.
135
unsigned char low() const { return ref().uc & 0xFF; }
137
* @return Higher byte.
139
unsigned char high() const { return ref().uc >> 8; }
141
* @return Character converted to lower case.
143
UChar toLower() const { return ref().toLower(); }
145
* @return Character converted to upper case.
147
UChar toUpper() const { return ref().toUpper(); }
149
// not implemented, can only be constructed from UString
158
* @short 8 bit char based string class
162
CString() : data(0L) { }
163
explicit CString(const char *c);
164
CString(const CString &);
168
CString &append(const CString &);
169
CString &operator=(const char *c);
170
CString &operator=(const CString &);
171
CString &operator+=(const CString &);
174
const char *c_str() const { return data; }
180
* @short Unicode string class
183
friend bool operator==(const UString&, const UString&);
184
friend class UCharReference;
185
friend class UConstString;
190
friend class UString;
191
friend bool operator==(const UString&, const UString&);
192
static Rep *create(UChar *d, int l);
193
inline UChar *data() const { return dat; }
194
inline int length() const { return len; }
196
inline void ref() { rc++; }
197
inline int deref() { return --rc; }
207
* Constructs a null string.
211
* Constructs a string from the single character c.
213
explicit UString(char c);
215
* Constructs a string from the single character c.
217
explicit UString(UChar c);
219
* Constructs a string from a classical zero determined char string.
221
explicit UString(const char *c);
223
* Constructs a string from an array of Unicode characters of the specified
226
UString(const UChar *c, int length);
228
* If copy is false a shallow copy of the string will be created. That
229
* means that the data will NOT be copied and you'll have to guarantee that
230
* it doesn't get deleted during the lifetime of the UString object.
232
UString(UChar *c, int length, bool copy);
234
* Copy constructor. Makes a shallow copy only.
236
UString(const UString &);
238
* Destructor. If this handle was the only one holding a reference to the
239
* string the data will be freed.
244
* Constructs a string from an int.
246
static UString from(int i);
248
* Constructs a string from an unsigned int.
250
static UString from(unsigned int u);
252
* Constructs a string from a double.
254
static UString from(double d);
257
* Append another string.
259
UString &append(const UString &);
262
* @return The string converted to the 8-bit string type @ref CString().
264
CString cstring() const;
266
* Convert the Unicode string to plain ASCII chars chopping of any higher
267
* bytes. This method should only be used for *debugging* purposes as it
268
* is neither Unicode safe nor free from side effects. In order not to
269
* waste any memory the char buffer is static and *shared* by all UString
275
* Assignment operator.
277
UString &operator=(const char *c);
279
* Assignment operator.
281
UString &operator=(const UString &);
283
* Appends the specified string.
285
UString &operator+=(const UString &s);
288
* @return A pointer to the internal Unicode data.
290
const UChar* data() const { return rep->data(); }
292
* @return True if null.
294
bool isNull() const { return (rep == &Rep::null); }
296
* @return True if null or zero length.
298
bool isEmpty() const { return (!rep->len); }
300
* Use this if you want to make sure that this string is a plain ASCII
301
* string. For example, if you don't want to lose any information when
302
* using @ref cstring() or @ref ascii().
304
* @return True if the string doesn't contain any non-ASCII characters.
308
* @return The length of the string.
310
int length() const { return rep->length(); }
312
* Const character at specified position.
314
UChar operator[](int pos) const;
316
* Writable reference to character at specified position.
318
UCharReference operator[](int pos);
321
* Attempts an conversion to a number. Apart from floating point numbers,
322
* the algorithm will recognize hexadecimal representations (as
323
* indicated by a 0x or 0X prefix) and +/- Infinity.
324
* Returns NaN if the conversion failed.
325
* @param tolerant if true, toDouble can tolerate garbage after the number.
327
double toDouble(bool tolerant=false) const;
329
* Attempts an conversion to an unsigned long integer. ok will be set
330
* according to the success.
332
unsigned long toULong(bool *ok = 0L) const;
334
* @return Position of first occurence of f starting at position pos.
335
* -1 if the search was not successful.
337
int find(const UString &f, int pos = 0) const;
339
* @return Position of first occurence of f searching backwards from
341
* -1 if the search was not successful.
343
int rfind(const UString &f, int pos) const;
345
* @return The sub string starting at position pos and length len.
347
UString substr(int pos = 0, int len = -1) const;
349
* Static instance of a null string.
360
inline bool operator==(const UChar &c1, const UChar &c2) {
361
return (c1.uc == c2.uc);
363
inline bool operator!=(const UChar &c1, const UChar &c2) {
366
bool operator==(const UString& s1, const UString& s2);
367
inline bool operator!=(const UString& s1, const UString& s2) {
368
return !Sidewinder::operator==(s1, s2);
370
bool operator<(const UString& s1, const UString& s2);
371
bool operator==(const UString& s1, const char *s2);
372
inline bool operator!=(const UString& s1, const char *s2) {
373
return !Sidewinder::operator==(s1, s2);
375
inline bool operator==(const char *s1, const UString& s2) {
376
return operator==(s2, s1);
378
inline bool operator!=(const char *s1, const UString& s2) {
379
return !Sidewinder::operator==(s1, s2);
381
bool operator==(const CString& s1, const CString& s2);
382
UString operator+(const UString& s1, const UString& s2);
385
class UConstString : private UString {
387
UConstString( UChar* data, unsigned int length );
390
const UString& string() const { return *this; }
393
} // namespace SIDEWINDER_USTRING_H