1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 * Copyright (C) 2004 Apple Computer, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef _KJS_USTRING_H_
25 #define _KJS_USTRING_H_
27 #include <kxmlcore/FastMalloc.h>
28 #include <kxmlcore/RefPtr.h>
29 #include <kxmlcore/PassRefPtr.h>
50 * @short Unicode character.
52 * UChar represents a 16 bit Unicode character. It's internal data
53 * representation is compatible to XChar2b and QChar. It's therefore
54 * possible to exchange data with X and Qt with shallow copies.
58 * Construct a character with uninitialized value.
62 * Construct a character with the value denoted by the arguments.
63 * @param h higher byte
66 UChar(unsigned char h , unsigned char l);
68 * Construct a character with the given value.
69 * @param u 16 bit Unicode value
72 UChar(unsigned char u);
73 UChar(unsigned short u);
74 UChar(const UCharReference &c);
76 * @return The higher byte of the character.
78 unsigned char high() const { return uc >> 8; }
80 * @return The lower byte of the character.
82 unsigned char low() const { return uc; }
84 * @return the 16 bit Unicode value of the character
86 unsigned short unicode() const { return uc; }
89 * @return The character converted to lower case.
91 UChar toLower() const;
93 * @return The character converted to upper case.
95 UChar toUpper() const;
100 inline UChar::UChar() { }
101 inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
102 inline UChar::UChar(char u) : uc((unsigned char)u) { }
103 inline UChar::UChar(unsigned char u) : uc(u) { }
104 inline UChar::UChar(unsigned short u) : uc(u) { }
107 * @short Dynamic reference to a string character.
109 * UCharReference is the dynamic counterpart of UChar. It's used when
110 * characters retrieved via index from a UString are used in an
111 * assignment expression (and therefore can't be treated as being const):
113 * UString s("hello world");
117 * If that sounds confusing your best bet is to simply forget about the
118 * existence of this class and treat is as being identical to UChar.
120 class UCharReference {
121 friend class UString;
122 UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
125 * Set the referenced character to c.
127 UCharReference& operator=(UChar c);
129 * Same operator as above except the argument that it takes.
131 UCharReference& operator=(char c) { return operator=(UChar(c)); }
133 * @return Unicode value.
135 unsigned short unicode() const { return ref().uc; }
137 * @return Lower byte.
139 unsigned char low() const { return ref().uc; }
141 * @return Higher byte.
143 unsigned char high() const { return ref().uc >> 8; }
145 * @return Character converted to lower case.
147 UChar toLower() const { return ref().toLower(); }
149 * @return Character converted to upper case.
151 UChar toUpper() const { return ref().toUpper(); }
153 // not implemented, can only be constructed from UString
161 inline UChar::UChar(const UCharReference &c) : uc(c.unicode()) { }
164 * @short 8 bit char based string class
168 CString() : data(0), length(0) { }
169 CString(const char *c);
170 CString(const char *c, int len);
171 CString(const CString &);
175 CString &append(const CString &);
176 CString &operator=(const char *c);
177 CString &operator=(const CString &);
178 CString &operator+=(const CString &c) { return append(c); }
180 int size() const { return length; }
181 const char *c_str() const { return data; }
188 * @short Unicode string class
191 friend bool operator==(const UString&, const UString&);
199 static PassRefPtr<Rep> create(UChar *d, int l);
200 static PassRefPtr<Rep> createCopying(const UChar *d, int l);
201 static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
205 UChar *data() const { return baseString ? (baseString->buf + baseString->preCapacity + offset) : (buf + preCapacity + offset); }
206 int size() const { return len; }
208 unsigned hash() const { if (_hash == 0) _hash = computeHash(data(), len); return _hash; }
209 static unsigned computeHash(const UChar *, int length);
210 static unsigned computeHash(const char *);
213 void deref() { if (--rc == 0) destroy(); }
219 mutable unsigned _hash;
221 UString::Rep *baseString;
223 // potentially shared data
236 * Constructs a null string.
240 * Constructs a string from the single character c.
242 explicit UString(char c);
244 * Constructs a string from a classical zero determined char string.
246 UString(const char *c);
248 * Constructs a string from an array of Unicode characters of the specified
251 UString(const UChar *c, int length);
253 * If copy is false the string data will be adopted.
254 * That means that the data will NOT be copied and the pointer will
255 * be deleted when the UString object is modified or destroyed.
256 * Behaviour defaults to a deep copy if copy is true.
258 UString(UChar *c, int length, bool copy);
260 * Copy constructor. Makes a shallow copy only.
262 UString(const UString &s) : m_rep(s.m_rep) {}
264 * Convenience declaration only ! You'll be on your own to write the
265 * implementation for a construction from QString.
267 * Note: feel free to contact me if you want to see a dummy header for
268 * your favorite FooString class here !
270 UString(const QString&);
272 * Convenience declaration only ! See UString(const QString&).
274 UString(const DOM::DOMString&);
276 * Convenience declaration only ! See UString(const QString&).
278 UString(const DOM::AtomicString&);
281 * Concatenation constructor. Makes operator+ more efficient.
283 UString(const UString &, const UString &);
290 * Constructs a string from an int.
292 static UString from(int i);
294 * Constructs a string from an unsigned int.
296 static UString from(unsigned int u);
298 * Constructs a string from a long int.
300 static UString from(long u);
302 * Constructs a string from a double.
304 static UString from(double d);
308 Range(int pos, int len) : position(pos), length(len) {}
314 UString spliceSubstringsWithSeparators(const Range *substringRanges, int rangeCount, const UString *separators, int separatorCount) const;
317 * Append another string.
319 UString &append(const UString &);
320 UString &append(const char *);
321 UString &append(unsigned short);
322 UString &append(char c) { return append(static_cast<unsigned short>(static_cast<unsigned char>(c))); }
323 UString &append(UChar c) { return append(c.uc); }
326 * @return The string converted to the 8-bit string type CString().
328 CString cstring() const;
330 * Convert the Unicode string to plain ASCII chars chopping of any higher
331 * bytes. This method should only be used for *debugging* purposes as it
332 * is neither Unicode safe nor free from side effects. In order not to
333 * waste any memory the char buffer is static and *shared* by all UString
339 * Convert the string to UTF-8, assuming it is UTF-16 encoded.
340 * Since this function is tolerant of badly formed UTF-16, it can create UTF-8
341 * strings that are invalid because they have characters in the range
342 * U+D800-U+DDFF, U+FFFE, or U+FFFF, but the UTF-8 string is guaranteed to
343 * be otherwise valid.
345 CString UTF8String() const;
348 * @see UString(const QString&).
350 DOM::DOMString domString() const;
352 * @see UString(const QString&).
354 QString qstring() const;
356 * @see UString(const QString&).
358 QConstString qconststring() const;
361 * Assignment operator.
363 UString &operator=(const char *c);
365 * Appends the specified string.
367 UString &operator+=(const UString &s) { return append(s); }
368 UString &operator+=(const char *s) { return append(s); }
371 * @return A pointer to the internal Unicode data.
373 const UChar* data() const { return m_rep->data(); }
375 * @return True if null.
377 bool isNull() const { return (m_rep == &Rep::null); }
379 * @return True if null or zero length.
381 bool isEmpty() const { return (!m_rep->len); }
383 * Use this if you want to make sure that this string is a plain ASCII
384 * string. For example, if you don't want to lose any information when
385 * using cstring() or ascii().
387 * @return True if the string doesn't contain any non-ASCII characters.
391 * @return The length of the string.
393 int size() const { return m_rep->size(); }
395 * Const character at specified position.
397 UChar operator[](int pos) const;
399 * Writable reference to character at specified position.
401 UCharReference operator[](int pos);
404 * Attempts an conversion to a number. Apart from floating point numbers,
405 * the algorithm will recognize hexadecimal representations (as
406 * indicated by a 0x or 0X prefix) and +/- Infinity.
407 * Returns NaN if the conversion failed.
408 * @param tolerateTrailingJunk if true, toDouble can tolerate garbage after the number.
409 * @param tolerateEmptyString if false, toDouble will turn an empty string into NaN rather than 0.
411 double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
412 double toDouble(bool tolerateTrailingJunk) const;
413 double toDouble() const;
416 * Attempts an conversion to a 32-bit integer. ok will be set
417 * according to the success.
418 * @param tolerateEmptyString if false, toUInt32 will return false for *ok for an empty string.
420 uint32_t toUInt32(bool *ok = 0) const;
421 uint32_t toUInt32(bool *ok, bool tolerateEmptyString) const;
422 uint32_t toStrictUInt32(bool *ok = 0) const;
425 * Attempts an conversion to an array index. The "ok" boolean will be set
426 * to true if it is a valid array index according to the rule from
427 * ECMA 15.2 about what an array index is. It must exactly match the string
428 * form of an unsigned integer, and be less than 2^32 - 1.
430 unsigned toArrayIndex(bool *ok = 0) const;
433 * @return Position of first occurrence of f starting at position pos.
434 * -1 if the search was not successful.
436 int find(const UString &f, int pos = 0) const;
437 int find(UChar, int pos = 0) const;
439 * @return Position of first occurrence of f searching backwards from
441 * -1 if the search was not successful.
443 int rfind(const UString &f, int pos) const;
444 int rfind(UChar, int pos) const;
446 * @return The sub string starting at position pos and length len.
448 UString substr(int pos = 0, int len = -1) const;
450 * Static instance of a null string.
452 static const UString &null();
455 * Clear statically allocated resources.
457 static void globalClear();
460 Rep *rep() const { return m_rep.get(); }
461 UString(PassRefPtr<Rep> r) : m_rep(r) { }
463 void copyForWriting();
466 int expandedSize(int size, int otherSize) const;
467 int usedCapacity() const;
468 int usedPreCapacity() const;
469 void expandCapacity(int requiredLength);
470 void expandPreCapacity(int requiredPreCap);
475 inline bool operator==(const UChar &c1, const UChar &c2) {
476 return (c1.uc == c2.uc);
478 bool operator==(const UString& s1, const UString& s2);
479 inline bool operator!=(const UString& s1, const UString& s2) {
480 return !KJS::operator==(s1, s2);
482 bool operator<(const UString& s1, const UString& s2);
483 bool operator==(const UString& s1, const char *s2);
484 inline bool operator!=(const UString& s1, const char *s2) {
485 return !KJS::operator==(s1, s2);
487 inline bool operator==(const char *s1, const UString& s2) {
488 return operator==(s2, s1);
490 inline bool operator!=(const char *s1, const UString& s2) {
491 return !KJS::operator==(s1, s2);
493 bool operator==(const CString& s1, const CString& s2);
494 inline UString operator+(const UString& s1, const UString& s2) {
495 return UString(s1, s2);
498 int compare(const UString &, const UString &);
500 // Given a first byte, gives the length of the UTF-8 sequence it begins.
501 // Returns 0 for bytes that are not legal starts of UTF-8 sequences.
502 // Only allows sequences of up to 4 bytes, since that works for all Unicode characters (U-00000000 to U-0010FFFF).
503 int UTF8SequenceLength(char);
505 // Takes a null-terminated C-style string with a UTF-8 sequence in it and converts it to a character.
506 // Only allows Unicode characters (U-00000000 to U-0010FFFF).
507 // Returns -1 if the sequence is not valid (including presence of extra bytes).
508 int decodeUTF8Sequence(const char *);
510 inline UString::UString()
515 // Rule from ECMA 15.2 about what an array index is.
516 // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
517 inline unsigned UString::toArrayIndex(bool *ok) const
519 unsigned i = toStrictUInt32(ok);
520 if (ok && i >= 0xFFFFFFFFU)