1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
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., 59 Temple Place - Suite 330,
|
---|
19 | * Boston, MA 02111-1307, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef _KJS_USTRING_H_
|
---|
24 | #define _KJS_USTRING_H_
|
---|
25 |
|
---|
26 | #ifdef APPLE_CHANGES
|
---|
27 | #ifndef KWQ_UNSIGNED_TYPES_DEFINED
|
---|
28 | #define KWQ_UNSIGNED_TYPES_DEFINED
|
---|
29 | typedef unsigned char uchar;
|
---|
30 | typedef unsigned short ushort;
|
---|
31 | typedef unsigned uint;
|
---|
32 | typedef unsigned long ulong;
|
---|
33 | #endif
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @internal
|
---|
38 | */
|
---|
39 | namespace DOM {
|
---|
40 | class DOMString;
|
---|
41 | };
|
---|
42 | class KJScript;
|
---|
43 | class QString;
|
---|
44 | class QConstString;
|
---|
45 |
|
---|
46 | namespace KJS {
|
---|
47 |
|
---|
48 | class UCharReference;
|
---|
49 | class UString;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @short Unicode character.
|
---|
53 | *
|
---|
54 | * UChar represents a 16 bit Unicode character. It's internal data
|
---|
55 | * representation is compatible to XChar2b and QChar. It's therefore
|
---|
56 | * possible to exchange data with X and Qt with shallow copies.
|
---|
57 | */
|
---|
58 | struct UChar {
|
---|
59 | /**
|
---|
60 | * Construct a character with value 0.
|
---|
61 | */
|
---|
62 | UChar();
|
---|
63 | /**
|
---|
64 | * Construct a character with the value denoted by the arguments.
|
---|
65 | * @param h higher byte
|
---|
66 | * @param l lower byte
|
---|
67 | */
|
---|
68 | UChar(unsigned char h , unsigned char l);
|
---|
69 | /**
|
---|
70 | * Construct a character with the given value.
|
---|
71 | * @param u 16 bit Unicode value
|
---|
72 | */
|
---|
73 | UChar(unsigned short u);
|
---|
74 | UChar(const UCharReference &c);
|
---|
75 | /**
|
---|
76 | * @return The higher byte of the character.
|
---|
77 | */
|
---|
78 | unsigned char high() const { return uc >> 8; }
|
---|
79 | /**
|
---|
80 | * @return The lower byte of the character.
|
---|
81 | */
|
---|
82 | unsigned char low() const { return uc & 0xFF; }
|
---|
83 | /**
|
---|
84 | * @return the 16 bit Unicode value of the character
|
---|
85 | */
|
---|
86 | unsigned short unicode() const { return uc; }
|
---|
87 | public:
|
---|
88 | /**
|
---|
89 | * @return The character converted to lower case.
|
---|
90 | */
|
---|
91 | UChar toLower() const;
|
---|
92 | /**
|
---|
93 | * @return The character converted to upper case.
|
---|
94 | */
|
---|
95 | UChar toUpper() const;
|
---|
96 | /**
|
---|
97 | * A static instance of UChar(0).
|
---|
98 | */
|
---|
99 | static UChar null;
|
---|
100 | private:
|
---|
101 | friend class UCharReference;
|
---|
102 | friend class UString;
|
---|
103 | friend bool operator==(const UChar &c1, const UChar &c2);
|
---|
104 | friend bool operator==(const UString& s1, const char *s2);
|
---|
105 | friend bool operator<(const UString& s1, const UString& s2);
|
---|
106 |
|
---|
107 | unsigned short uc;
|
---|
108 | };
|
---|
109 |
|
---|
110 | inline UChar::UChar() : uc(0) { }
|
---|
111 | inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
|
---|
112 | inline UChar::UChar(unsigned short u) : uc(u) { }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * @short Dynamic reference to a string character.
|
---|
116 | *
|
---|
117 | * UCharReference is the dynamic counterpart of @ref UChar. It's used when
|
---|
118 | * characters retrieved via index from a @ref UString are used in an
|
---|
119 | * assignment expression (and therefore can't be treated as being const):
|
---|
120 | * <pre>
|
---|
121 | * UString s("hello world");
|
---|
122 | * s[0] = 'H';
|
---|
123 | * </pre>
|
---|
124 | *
|
---|
125 | * If that sounds confusing your best bet is to simply forget about the
|
---|
126 | * existance of this class and treat is as being identical to @ref UChar.
|
---|
127 | */
|
---|
128 | class UCharReference {
|
---|
129 | friend class UString;
|
---|
130 | UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
|
---|
131 | public:
|
---|
132 | /**
|
---|
133 | * Set the referenced character to c.
|
---|
134 | */
|
---|
135 | UCharReference& operator=(UChar c);
|
---|
136 | /**
|
---|
137 | * Same operator as above except the argument that it takes.
|
---|
138 | */
|
---|
139 | UCharReference& operator=(char c) { return operator=(UChar(c)); }
|
---|
140 | /**
|
---|
141 | * @return Unicode value.
|
---|
142 | */
|
---|
143 | unsigned short unicode() const { return ref().unicode(); }
|
---|
144 | /**
|
---|
145 | * @return Lower byte.
|
---|
146 | */
|
---|
147 | unsigned char low() const { return ref().uc & 0xFF; }
|
---|
148 | /**
|
---|
149 | * @return Higher byte.
|
---|
150 | */
|
---|
151 | unsigned char high() const { return ref().uc >> 8; }
|
---|
152 | /**
|
---|
153 | * @return Character converted to lower case.
|
---|
154 | */
|
---|
155 | UChar toLower() const { return ref().toLower(); }
|
---|
156 | /**
|
---|
157 | * @return Character converted to upper case.
|
---|
158 | */
|
---|
159 | UChar toUpper() const { return ref().toUpper(); }
|
---|
160 | private:
|
---|
161 | // not implemented, can only be constructed from UString
|
---|
162 | UCharReference();
|
---|
163 |
|
---|
164 | UChar& ref() const;
|
---|
165 | UString *str;
|
---|
166 | int offset;
|
---|
167 | };
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * @short 8 bit char based string class
|
---|
171 | */
|
---|
172 | class CString {
|
---|
173 | public:
|
---|
174 | CString() : data(0L) { }
|
---|
175 | CString(const char *c);
|
---|
176 | CString(const CString &);
|
---|
177 |
|
---|
178 | ~CString();
|
---|
179 |
|
---|
180 | CString &append(const CString &);
|
---|
181 | CString &operator=(const char *c);
|
---|
182 | CString &operator=(const CString &);
|
---|
183 | CString &operator+=(const CString &);
|
---|
184 |
|
---|
185 | int size() const;
|
---|
186 | const char *c_str() const { return data; }
|
---|
187 | private:
|
---|
188 | char *data;
|
---|
189 | };
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * @short Unicode string class
|
---|
193 | */
|
---|
194 | class UString {
|
---|
195 | friend bool operator==(const UString&, const UString&);
|
---|
196 | friend class UCharReference;
|
---|
197 | /**
|
---|
198 | * @internal
|
---|
199 | */
|
---|
200 | struct Rep {
|
---|
201 | friend class UString;
|
---|
202 | friend bool operator==(const UString&, const UString&);
|
---|
203 | static Rep *create(UChar *d, int l);
|
---|
204 | inline UChar *data() const { return dat; }
|
---|
205 | inline int size() const { return len; }
|
---|
206 |
|
---|
207 | inline void ref() { rc++; }
|
---|
208 | inline int deref() { return --rc; }
|
---|
209 |
|
---|
210 | UChar *dat;
|
---|
211 | int len;
|
---|
212 | int rc;
|
---|
213 | static Rep null;
|
---|
214 | };
|
---|
215 |
|
---|
216 | public:
|
---|
217 | /**
|
---|
218 | * Constructs a null string.
|
---|
219 | */
|
---|
220 | UString();
|
---|
221 | /**
|
---|
222 | * Constructs a string from the single character c.
|
---|
223 | */
|
---|
224 | UString(char c);
|
---|
225 | /**
|
---|
226 | * Constructs a string from a classical zero determined char string.
|
---|
227 | */
|
---|
228 | UString(const char *c);
|
---|
229 | /**
|
---|
230 | * Constructs a string from an array of Unicode characters of the specified
|
---|
231 | * length.
|
---|
232 | */
|
---|
233 | UString(const UChar *c, int length);
|
---|
234 | /**
|
---|
235 | * If copy is false a shallow copy of the string will be created. That
|
---|
236 | * means that the data will NOT be copied and you'll have to guarantee that
|
---|
237 | * it doesn't get deleted during the lifetime of the UString object.
|
---|
238 | * Behaviour defaults to a deep copy if copy is true.
|
---|
239 | */
|
---|
240 | UString(UChar *c, int length, bool copy);
|
---|
241 | /**
|
---|
242 | * Copy constructor. Makes a shallow copy only.
|
---|
243 | */
|
---|
244 | UString(const UString &);
|
---|
245 | /**
|
---|
246 | * Convenience declaration only ! You'll be on your own to write the
|
---|
247 | * implementation for a construction from QString.
|
---|
248 | *
|
---|
249 | * Note: feel free to contact me if you want to see a dummy header for
|
---|
250 | * your favourite FooString class here !
|
---|
251 | */
|
---|
252 | UString(const QString &);
|
---|
253 | /**
|
---|
254 | * Convenience declaration only ! See @ref UString(const QString&).
|
---|
255 | */
|
---|
256 | UString(const DOM::DOMString &);
|
---|
257 | /**
|
---|
258 | * Destructor. If this handle was the only one holding a reference to the
|
---|
259 | * string the data will be freed.
|
---|
260 | */
|
---|
261 | ~UString();
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Constructs a string from an int.
|
---|
265 | */
|
---|
266 | static UString from(int i);
|
---|
267 | /**
|
---|
268 | * Constructs a string from an unsigned int.
|
---|
269 | */
|
---|
270 | static UString from(unsigned int u);
|
---|
271 | /**
|
---|
272 | * Constructs a string from a double.
|
---|
273 | */
|
---|
274 | static UString from(double d);
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Append another string.
|
---|
278 | */
|
---|
279 | UString &append(const UString &);
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * @return The string converted to the 8-bit string type @ref CString().
|
---|
283 | */
|
---|
284 | CString cstring() const;
|
---|
285 | /**
|
---|
286 | * Convert the Unicode string to plain ASCII chars chopping of any higher
|
---|
287 | * bytes. This method should only be used for *debugging* purposes as it
|
---|
288 | * is neither Unicode safe nor free from side effects. In order not to
|
---|
289 | * waste any memory the char buffer is static and *shared* by all UString
|
---|
290 | * instances.
|
---|
291 | */
|
---|
292 | char *ascii() const;
|
---|
293 | /**
|
---|
294 | * @see UString(const QString&).
|
---|
295 | */
|
---|
296 | DOM::DOMString string() const;
|
---|
297 | /**
|
---|
298 | * @see UString(const QString&).
|
---|
299 | */
|
---|
300 | QString qstring() const;
|
---|
301 | /**
|
---|
302 | * @see UString(const QString&).
|
---|
303 | */
|
---|
304 | QConstString qconststring() const;
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Assignment operator.
|
---|
308 | */
|
---|
309 | UString &operator=(const char *c);
|
---|
310 | /**
|
---|
311 | * Assignment operator.
|
---|
312 | */
|
---|
313 | UString &operator=(const UString &);
|
---|
314 | /**
|
---|
315 | * Appends the specified string.
|
---|
316 | */
|
---|
317 | UString &operator+=(const UString &s);
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * @return A pointer to the internal Unicode data.
|
---|
321 | */
|
---|
322 | const UChar* data() const { return rep->data(); }
|
---|
323 | /**
|
---|
324 | * @return True if null.
|
---|
325 | */
|
---|
326 | bool isNull() const { return (rep == &Rep::null); }
|
---|
327 | /**
|
---|
328 | * @return True if null or zero length.
|
---|
329 | */
|
---|
330 | bool isEmpty() const { return (!rep->len); }
|
---|
331 | /**
|
---|
332 | * Use this if you want to make sure that this string is a plain ASCII
|
---|
333 | * string. For example, if you don't want to lose any information when
|
---|
334 | * using @ref cstring() or @ref ascii().
|
---|
335 | *
|
---|
336 | * @return True if the string doesn't contain any non-ASCII characters.
|
---|
337 | */
|
---|
338 | bool is8Bit() const;
|
---|
339 | /**
|
---|
340 | * @return The length of the string.
|
---|
341 | */
|
---|
342 | int size() const { return rep->size(); }
|
---|
343 | /**
|
---|
344 | * Const character at specified position.
|
---|
345 | */
|
---|
346 | UChar operator[](int pos) const;
|
---|
347 | /**
|
---|
348 | * Writable reference to character at specified position.
|
---|
349 | */
|
---|
350 | UCharReference operator[](int pos);
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Attempts an conversion to a number. Apart from floating point numbers,
|
---|
354 | * the algorithm will recognize hexadecimal representations (as
|
---|
355 | * indicated by a 0x or 0X prefix) and +/- Infinity.
|
---|
356 | * Returns NaN if the conversion failed.
|
---|
357 | * @param tolerant if true, toDouble can tolerate garbage after the number.
|
---|
358 | */
|
---|
359 | double toDouble(bool tolerant=false) const;
|
---|
360 | /**
|
---|
361 | * Attempts an conversion to an unsigned long integer. ok will be set
|
---|
362 | * according to the success.
|
---|
363 | */
|
---|
364 | unsigned long toULong(bool *ok = 0L) const;
|
---|
365 | /**
|
---|
366 | * @return Position of first occurence of f starting at position pos.
|
---|
367 | * -1 if the search was not successful.
|
---|
368 | */
|
---|
369 | int find(const UString &f, int pos = 0) const;
|
---|
370 | /**
|
---|
371 | * @return Position of first occurence of f searching backwards from
|
---|
372 | * position pos.
|
---|
373 | * -1 if the search was not successful.
|
---|
374 | */
|
---|
375 | int rfind(const UString &f, int pos) const;
|
---|
376 | /**
|
---|
377 | * @return The sub string starting at position pos and length len.
|
---|
378 | */
|
---|
379 | UString substr(int pos = 0, int len = -1) const;
|
---|
380 | /**
|
---|
381 | * Static instance of a null string.
|
---|
382 | */
|
---|
383 | static UString null;
|
---|
384 | private:
|
---|
385 | void attach(Rep *r);
|
---|
386 | void detach();
|
---|
387 | void release();
|
---|
388 | Rep *rep;
|
---|
389 | };
|
---|
390 |
|
---|
391 | inline bool operator==(const UChar &c1, const UChar &c2) {
|
---|
392 | return (c1.uc == c2.uc);
|
---|
393 | }
|
---|
394 | bool operator==(const UString& s1, const UString& s2);
|
---|
395 | inline bool operator!=(const UString& s1, const UString& s2) {
|
---|
396 | return !KJS::operator==(s1, s2);
|
---|
397 | }
|
---|
398 | bool operator<(const UString& s1, const UString& s2);
|
---|
399 | bool operator==(const UString& s1, const char *s2);
|
---|
400 | inline bool operator!=(const UString& s1, const char *s2) {
|
---|
401 | return !KJS::operator==(s1, s2);
|
---|
402 | }
|
---|
403 | inline bool operator==(const char *s1, const UString& s2) {
|
---|
404 | return operator==(s2, s1);
|
---|
405 | }
|
---|
406 | inline bool operator!=(const char *s1, const UString& s2) {
|
---|
407 | return !KJS::operator==(s1, s2);
|
---|
408 | }
|
---|
409 | bool operator==(const CString& s1, const CString& s2);
|
---|
410 | UString operator+(const UString& s1, const UString& s2);
|
---|
411 |
|
---|
412 | }; // namespace
|
---|
413 |
|
---|
414 | #endif
|
---|