1 // -*- c-basic-offset: 2 -*-
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
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., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifndef _KJS_USTRING_H_
24 #define _KJS_USTRING_H_
27 #include "collector.h"
29 #include <wtf/Assertions.h>
30 #include <wtf/FastMalloc.h>
31 #include <wtf/PassRefPtr.h>
32 #include <wtf/RefPtr.h>
33 #include <wtf/Vector.h>
35 /* On some ARM platforms GCC won't pack structures by default so sizeof(UChar)
36 will end up being != 2 which causes crashes since the code depends on that. */
37 #if COMPILER(GCC) && PLATFORM(FORCE_PACK)
38 #define PACK_STRUCT __attribute__((packed))
54 using WTF::PlacementNewAdoptType;
55 using WTF::PlacementNewAdopt;
60 * @short Unicode character.
62 * UChar represents a 16 bit Unicode character. It's internal data
63 * representation is compatible to XChar2b and QChar. It's therefore
64 * possible to exchange data with X and Qt with shallow copies.
68 * Construct a character with uninitialized value.
72 * Construct a character with the value denoted by the arguments.
73 * @param h higher byte
76 UChar(unsigned char h , unsigned char l);
78 * Construct a character with the given value.
79 * @param u 16 bit Unicode value
82 UChar(unsigned char u);
83 UChar(unsigned short u);
85 * @return The higher byte of the character.
87 unsigned char high() const { return static_cast<unsigned char>(uc >> 8); }
89 * @return The lower byte of the character.
91 unsigned char low() const { return static_cast<unsigned char>(uc); }
93 * @return the 16 bit Unicode value of the character
95 unsigned short unicode() const { return uc; }
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 8 bit char based string class
111 CString() : data(0), length(0) { }
112 CString(const char *c);
113 CString(const char *c, size_t len);
114 CString(const CString &);
118 CString &append(const CString &);
119 CString &operator=(const char *c);
120 CString &operator=(const CString &);
121 CString &operator+=(const CString &c) { return append(c); }
123 size_t size() const { return length; }
124 const char *c_str() const { return data; }
131 * @short Unicode string class
134 friend bool operator==(const UString&, const UString&);
142 static PassRefPtr<Rep> create(UChar *d, int l);
143 static PassRefPtr<Rep> createCopying(const UChar *d, int l);
144 static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
148 bool baseIsSelf() const { return baseString == this; }
149 UChar* data() const { return baseString->buf + baseString->preCapacity + offset; }
150 int size() const { return len; }
152 unsigned hash() const { if (_hash == 0) _hash = computeHash(data(), len); return _hash; }
153 unsigned computedHash() const { ASSERT(_hash); return _hash; } // fast path for Identifiers
155 static unsigned computeHash(const UChar *, int length);
156 static unsigned computeHash(const char *);
158 Rep* ref() { ASSERT(JSLock::lockCount() > 0); ++rc; return this; }
159 ALWAYS_INLINE void deref() { ASSERT(JSLock::lockCount() > 0); if (--rc == 0) destroy(); }
165 mutable unsigned _hash;
167 UString::Rep* baseString;
170 // potentially shared data
184 * Constructs a null string.
188 * Constructs a string from a classical zero-terminated char string.
190 UString(const char *c);
192 * Constructs a string from an array of Unicode characters of the specified
195 UString(const UChar *c, int length);
197 * If copy is false the string data will be adopted.
198 * That means that the data will NOT be copied and the pointer will
199 * be deleted when the UString object is modified or destroyed.
200 * Behaviour defaults to a deep copy if copy is true.
202 UString(UChar *c, int length, bool copy);
204 * Copy constructor. Makes a shallow copy only.
206 UString(const UString &s) : m_rep(s.m_rep) {}
208 UString(const Vector<UChar>& buffer);
211 * Convenience declaration only ! You'll be on your own to write the
212 * implementation for a construction from DOM::DOMString.
214 * Note: feel free to contact me if you want to see a dummy header for
215 * your favorite FooString class here !
217 UString(const DOM::DOMString&);
219 * Convenience declaration only ! See UString(const DOM::DOMString&).
221 UString(const DOM::AtomicString&);
224 * Concatenation constructor. Makes operator+ more efficient.
226 UString(const UString &, const UString &);
232 // Special constructor for cases where we overwrite an object in place.
233 UString(PlacementNewAdoptType) : m_rep(PlacementNewAdopt) { }
236 * Constructs a string from an int.
238 static UString from(int i);
240 * Constructs a string from an unsigned int.
242 static UString from(unsigned int u);
244 * Constructs a string from a long int.
246 static UString from(long u);
248 * Constructs a string from a double.
250 static UString from(double d);
254 Range(int pos, int len) : position(pos), length(len) {}
260 UString spliceSubstringsWithSeparators(const Range *substringRanges, int rangeCount, const UString *separators, int separatorCount) const;
263 * Append another string.
265 UString &append(const UString &);
266 UString &append(const char *);
267 UString &append(unsigned short);
268 UString &append(char c) { return append(static_cast<unsigned short>(static_cast<unsigned char>(c))); }
269 UString &append(UChar c) { return append(c.uc); }
272 * @return The string converted to the 8-bit string type CString().
273 * This method is not Unicode safe and shouldn't be used unless the string
274 * is known to be ASCII.
276 CString cstring() const;
278 * Convert the Unicode string to plain ASCII chars chopping of any higher
279 * bytes. This method should only be used for *debugging* purposes as it
280 * is neither Unicode safe nor free from side effects. In order not to
281 * waste any memory the char buffer is static and *shared* by all UString
287 * Convert the string to UTF-8, assuming it is UTF-16 encoded.
288 * In non-strict mode, this function is tolerant of badly formed UTF-16, it
289 * can create UTF-8 strings that are invalid because they have characters in
290 * the range U+D800-U+DDFF, U+FFFE, or U+FFFF, but the UTF-8 string is
291 * guaranteed to be otherwise valid.
292 * In strict mode, error is returned as null CString.
294 CString UTF8String(bool strict = false) const;
297 * @see UString(const DOM::DOMString&).
299 DOM::DOMString domString() const;
302 * Assignment operator.
304 UString &operator=(const char *c);
306 * Appends the specified string.
308 UString &operator+=(const UString &s) { return append(s); }
309 UString &operator+=(const char *s) { return append(s); }
312 * @return A pointer to the internal Unicode data.
314 const UChar* data() const { return m_rep->data(); }
316 * @return True if null.
318 bool isNull() const { return (m_rep == &Rep::null); }
320 * @return True if null or zero length.
322 bool isEmpty() const { return (!m_rep->len); }
324 * Use this if you want to make sure that this string is a plain ASCII
325 * string. For example, if you don't want to lose any information when
326 * using cstring() or ascii().
328 * @return True if the string doesn't contain any non-ASCII characters.
332 * @return The length of the string.
334 int size() const { return m_rep->size(); }
336 * Const character at specified position.
338 const UChar operator[](int pos) const;
341 * Attempts an conversion to a number. Apart from floating point numbers,
342 * the algorithm will recognize hexadecimal representations (as
343 * indicated by a 0x or 0X prefix) and +/- Infinity.
344 * Returns NaN if the conversion failed.
345 * @param tolerateTrailingJunk if true, toDouble can tolerate garbage after the number.
346 * @param tolerateEmptyString if false, toDouble will turn an empty string into NaN rather than 0.
348 double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
349 double toDouble(bool tolerateTrailingJunk) const;
350 double toDouble() const;
353 * Attempts an conversion to a 32-bit integer. ok will be set
354 * according to the success.
355 * @param tolerateEmptyString if false, toUInt32 will return false for *ok for an empty string.
357 uint32_t toUInt32(bool *ok = 0) const;
358 uint32_t toUInt32(bool *ok, bool tolerateEmptyString) const;
359 uint32_t toStrictUInt32(bool *ok = 0) const;
362 * Attempts an conversion to an array index. The "ok" boolean will be set
363 * to true if it is a valid array index according to the rule from
364 * ECMA 15.2 about what an array index is. It must exactly match the string
365 * form of an unsigned integer, and be less than 2^32 - 1.
367 unsigned toArrayIndex(bool *ok = 0) const;
370 * @return Position of first occurrence of f starting at position pos.
371 * -1 if the search was not successful.
373 int find(const UString &f, int pos = 0) const;
374 int find(UChar, int pos = 0) const;
376 * @return Position of first occurrence of f searching backwards from
378 * -1 if the search was not successful.
380 int rfind(const UString &f, int pos) const;
381 int rfind(UChar, int pos) const;
383 * @return The sub string starting at position pos and length len.
385 UString substr(int pos = 0, int len = -1) const;
387 * Static instance of a null string.
389 static const UString &null();
391 Rep* rep() const { return m_rep.get(); }
392 UString(PassRefPtr<Rep> r) : m_rep(r) { ASSERT(m_rep); }
397 size_t expandedSize(size_t size, size_t otherSize) const;
398 int usedCapacity() const;
399 int usedPreCapacity() const;
400 void expandCapacity(int requiredLength);
401 void expandPreCapacity(int requiredPreCap);
406 inline bool operator==(const UChar &c1, const UChar &c2) {
407 return (c1.uc == c2.uc);
409 bool operator==(const UString& s1, const UString& s2);
410 inline bool operator!=(const UString& s1, const UString& s2) {
411 return !KJS::operator==(s1, s2);
413 bool operator<(const UString& s1, const UString& s2);
414 bool operator==(const UString& s1, const char *s2);
415 inline bool operator!=(const UString& s1, const char *s2) {
416 return !KJS::operator==(s1, s2);
418 inline bool operator==(const char *s1, const UString& s2) {
419 return operator==(s2, s1);
421 inline bool operator!=(const char *s1, const UString& s2) {
422 return !KJS::operator==(s1, s2);
424 bool operator==(const CString& s1, const CString& s2);
425 inline UString operator+(const UString& s1, const UString& s2) {
426 return UString(s1, s2);
429 int compare(const UString &, const UString &);
431 inline UString::UString()
436 // Rule from ECMA 15.2 about what an array index is.
437 // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
438 inline unsigned UString::toArrayIndex(bool *ok) const
440 unsigned i = toStrictUInt32(ok);
441 if (ok && i >= 0xFFFFFFFFU)
446 // We'd rather not do shared substring append for small strings, since
447 // this runs too much risk of a tiny initial string holding down a
449 // FIXME: this should be size_t but that would cause warnings until we
450 // fix UString sizes to be size_t instead of int
451 static const int minShareSize = Collector::minExtraCostSize / sizeof(UChar);
453 inline size_t UString::cost() const
455 size_t capacity = (m_rep->baseString->capacity + m_rep->baseString->preCapacity) * sizeof(UChar);
456 size_t reportedCost = m_rep->baseString->reportedCost;
457 ASSERT(capacity >= reportedCost);
459 size_t capacityDelta = capacity - reportedCost;
461 if (capacityDelta < static_cast<unsigned int>(minShareSize))
464 m_rep->baseString->reportedCost = capacity;
465 return capacityDelta;