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.
5 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
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.
28 #include "collector.h"
31 #include "identifier.h"
32 #include "operations.h"
39 #include <wtf/Assertions.h>
40 #include <wtf/ASCIICType.h>
41 #include <wtf/MathExtras.h>
42 #include <wtf/Vector.h>
43 #include <wtf/unicode/UTF8.h>
53 using namespace WTF::Unicode;
58 extern const double NaN;
59 extern const double Inf;
61 static inline const size_t overflowIndicator() { return std::numeric_limits<size_t>::max(); }
62 static inline const size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); }
64 static inline UChar* allocChars(size_t length)
67 if (length > maxUChars())
69 return static_cast<UChar*>(fastMalloc(sizeof(UChar) * length));
72 static inline UChar* reallocChars(UChar* buffer, size_t length)
75 if (length > maxUChars())
77 return static_cast<UChar*>(fastRealloc(buffer, sizeof(UChar) * length));
80 COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes)
82 CString::CString(const char *c)
85 data = new char[length+1];
86 memcpy(data, c, length + 1);
89 CString::CString(const char *c, size_t len)
92 data = new char[len+1];
97 CString::CString(const CString &b)
101 data = new char[length+1];
102 memcpy(data, b.data, length + 1);
113 CString &CString::append(const CString &t)
116 n = new char[length+t.length+1];
118 memcpy(n, data, length);
120 memcpy(n+length, t.data, t.length);
130 CString &CString::operator=(const char *c)
135 data = new char[length+1];
136 memcpy(data, c, length + 1);
141 CString &CString::operator=(const CString &str)
150 data = new char[length + 1];
151 memcpy(data, str.data, length + 1);
159 bool operator==(const CString& c1, const CString& c2)
161 size_t len = c1.size();
162 return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0);
165 // Hack here to avoid a global with a constructor; point to an unsigned short instead of a UChar.
166 static unsigned short almostUChar;
167 UString::Rep UString::Rep::null = { 0, 0, 1, 0, 0, &UString::Rep::null, 0, 0, 0, 0, 0, 0 };
168 UString::Rep UString::Rep::empty = { 0, 0, 1, 0, 0, &UString::Rep::empty, 0, reinterpret_cast<UChar*>(&almostUChar), 0, 0, 0, 0 };
169 const int normalStatBufferSize = 4096;
170 static char *statBuffer = 0; // FIXME: This buffer is never deallocated.
171 static int statBufferSize = 0;
173 PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int l)
175 ASSERT(JSLock::lockCount() > 0);
177 int sizeInBytes = l * sizeof(UChar);
178 UChar *copyD = static_cast<UChar *>(fastMalloc(sizeInBytes));
179 memcpy(copyD, d, sizeInBytes);
181 return create(copyD, l);
184 PassRefPtr<UString::Rep> UString::Rep::create(UChar *d, int l)
186 ASSERT(JSLock::lockCount() > 0);
199 r->usedPreCapacity = 0;
202 // steal the single reference this Rep was created with
206 PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length)
208 ASSERT(JSLock::lockCount() > 0);
211 int baseOffset = base->offset;
213 base = base->baseString;
215 ASSERT(-(offset + baseOffset) <= base->usedPreCapacity);
216 ASSERT(offset + baseOffset + length <= base->usedCapacity);
219 r->offset = baseOffset + offset;
224 r->baseString = base.releaseRef();
229 r->usedPreCapacity = 0;
232 // steal the single reference this Rep was created with
236 void UString::Rep::destroy()
238 ASSERT(JSLock::lockCount() > 0);
241 Identifier::remove(this);
242 if (baseString != this) {
250 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
251 // or anything like that.
252 const unsigned PHI = 0x9e3779b9U;
254 // Paul Hsieh's SuperFastHash
255 // http://www.azillionmonkeys.com/qed/hash.html
256 unsigned UString::Rep::computeHash(const UChar *s, int len)
268 tmp = (s[1].uc << 11) ^ hash;
269 hash = (hash << 16) ^ tmp;
281 // Force "avalanching" of final 127 bits
288 // this avoids ever returning a hash code of 0, since that is used to
289 // signal "hash not computed yet", using a value that is likely to be
290 // effectively the same as 0 when the low bits are masked
297 // Paul Hsieh's SuperFastHash
298 // http://www.azillionmonkeys.com/qed/hash.html
299 unsigned UString::Rep::computeHash(const char *s)
301 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
302 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
303 // were 16-bit chunks, which should give matching results
307 size_t l = strlen(s);
314 hash += (unsigned char)s[0];
315 tmp = ((unsigned char)s[1] << 11) ^ hash;
316 hash = (hash << 16) ^ tmp;
323 hash += (unsigned char)s[0];
328 // Force "avalanching" of final 127 bits
335 // this avoids ever returning a hash code of 0, since that is used to
336 // signal "hash not computed yet", using a value that is likely to be
337 // effectively the same as 0 when the low bits are masked
344 // put these early so they can be inlined
345 inline size_t UString::expandedSize(size_t size, size_t otherSize) const
347 // Do the size calculation in two parts, returning overflowIndicator if
348 // we overflow the maximum value that we can handle.
350 if (size > maxUChars())
351 return overflowIndicator();
353 size_t expandedSize = ((size + 10) / 10 * 11) + 1;
354 if (maxUChars() - expandedSize < otherSize)
355 return overflowIndicator();
357 return expandedSize + otherSize;
360 inline int UString::usedCapacity() const
362 return m_rep->baseString->usedCapacity;
365 inline int UString::usedPreCapacity() const
367 return m_rep->baseString->usedPreCapacity;
370 void UString::expandCapacity(int requiredLength)
372 Rep* r = m_rep->baseString;
374 if (requiredLength > r->capacity) {
375 size_t newCapacity = expandedSize(requiredLength, r->preCapacity);
376 UChar* oldBuf = r->buf;
377 r->buf = reallocChars(r->buf, newCapacity);
383 r->capacity = newCapacity - r->preCapacity;
385 if (requiredLength > r->usedCapacity) {
386 r->usedCapacity = requiredLength;
390 void UString::expandPreCapacity(int requiredPreCap)
392 Rep* r = m_rep->baseString;
394 if (requiredPreCap > r->preCapacity) {
395 size_t newCapacity = expandedSize(requiredPreCap, r->capacity);
396 int delta = newCapacity - r->capacity - r->preCapacity;
398 UChar* newBuf = allocChars(newCapacity);
403 memcpy(newBuf + delta, r->buf, (r->capacity + r->preCapacity) * sizeof(UChar));
407 r->preCapacity = newCapacity - r->capacity;
409 if (requiredPreCap > r->usedPreCapacity) {
410 r->usedPreCapacity = requiredPreCap;
414 UString::UString(const char *c)
426 size_t length = strlen(c);
427 UChar *d = allocChars(length);
431 for (size_t i = 0; i < length; i++)
433 m_rep = Rep::create(d, static_cast<int>(length));
437 UString::UString(const UChar *c, int length)
442 m_rep = Rep::createCopying(c, length);
445 UString::UString(UChar *c, int length, bool copy)
450 m_rep = Rep::createCopying(c, length);
452 m_rep = Rep::create(c, length);
455 UString::UString(const Vector<UChar>& buffer)
460 m_rep = Rep::createCopying(buffer.data(), buffer.size());
464 UString::UString(const UString &a, const UString &b)
466 int aSize = a.size();
467 int aOffset = a.m_rep->offset;
468 int bSize = b.size();
469 int bOffset = b.m_rep->offset;
470 int length = aSize + bSize;
477 } else if (bSize == 0) {
480 } else if (aOffset + aSize == a.usedCapacity() && aSize >= minShareSize && 4 * aSize >= bSize &&
481 (-bOffset != b.usedPreCapacity() || aSize >= bSize)) {
482 // - a reaches the end of its buffer so it qualifies for shared append
483 // - also, it's at least a quarter the length of b - appending to a much shorter
484 // string does more harm than good
485 // - however, if b qualifies for prepend and is longer than a, we'd rather prepend
487 x.expandCapacity(aOffset + length);
488 if (a.data() && x.data()) {
489 memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar));
490 m_rep = Rep::create(a.m_rep, 0, length);
493 } else if (-bOffset == b.usedPreCapacity() && bSize >= minShareSize && 4 * bSize >= aSize) {
494 // - b reaches the beginning of its buffer so it qualifies for shared prepend
495 // - also, it's at least a quarter the length of a - prepending to a much shorter
496 // string does more harm than good
498 y.expandPreCapacity(-bOffset + aSize);
499 if (b.data() && y.data()) {
500 memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar));
501 m_rep = Rep::create(b.m_rep, -aSize, length);
505 // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
506 size_t newCapacity = expandedSize(length, 0);
507 UChar* d = allocChars(newCapacity);
511 memcpy(d, a.data(), aSize * sizeof(UChar));
512 memcpy(d + aSize, b.data(), bSize * sizeof(UChar));
513 m_rep = Rep::create(d, length);
514 m_rep->capacity = newCapacity;
519 const UString& UString::null()
521 static UString* n = new UString;
525 UString UString::from(int i)
527 UChar buf[1 + sizeof(i) * 3];
528 UChar *end = buf + sizeof(buf) / sizeof(UChar);
533 } else if (i == INT_MIN) {
534 char minBuf[1 + sizeof(i) * 3];
535 sprintf(minBuf, "%d", INT_MIN);
536 return UString(minBuf);
538 bool negative = false;
544 *--p = (unsigned short)((i % 10) + '0');
552 return UString(p, static_cast<int>(end - p));
555 UString UString::from(unsigned int u)
557 UChar buf[sizeof(u) * 3];
558 UChar *end = buf + sizeof(buf) / sizeof(UChar);
565 *--p = (unsigned short)((u % 10) + '0');
570 return UString(p, static_cast<int>(end - p));
573 UString UString::from(long l)
575 UChar buf[1 + sizeof(l) * 3];
576 UChar *end = buf + sizeof(buf) / sizeof(UChar);
581 } else if (l == LONG_MIN) {
582 char minBuf[1 + sizeof(l) * 3];
583 sprintf(minBuf, "%ld", LONG_MIN);
584 return UString(minBuf);
586 bool negative = false;
592 *--p = (unsigned short)((l % 10) + '0');
600 return UString(p, static_cast<int>(end - p));
603 UString UString::from(double d)
605 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
613 char *result = kjs_dtoa(d, 0, 0, &decimalPoint, &sign, NULL);
614 int length = static_cast<int>(strlen(result));
621 if (decimalPoint <= 0 && decimalPoint > -6) {
624 for (int j = decimalPoint; j < 0; j++) {
627 strcpy(buf + i, result);
628 } else if (decimalPoint <= 21 && decimalPoint > 0) {
629 if (length <= decimalPoint) {
630 strcpy(buf + i, result);
632 for (int j = 0; j < decimalPoint - length; j++) {
637 strncpy(buf + i, result, decimalPoint);
640 strcpy(buf + i, result + decimalPoint);
642 } else if (result[0] < '0' || result[0] > '9') {
643 strcpy(buf + i, result);
645 buf[i++] = result[0];
648 strcpy(buf + i, result + 1);
653 buf[i++] = (decimalPoint >= 0) ? '+' : '-';
654 // decimalPoint can't be more than 3 digits decimal given the
655 // nature of float representation
656 int exponential = decimalPoint - 1;
658 exponential = -exponential;
659 if (exponential >= 100)
660 buf[i++] = static_cast<char>('0' + exponential / 100);
661 if (exponential >= 10)
662 buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
663 buf[i++] = static_cast<char>('0' + exponential % 10);
667 kjs_freedtoa(result);
672 UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
674 if (rangeCount == 1 && separatorCount == 0) {
675 int thisSize = size();
676 int position = substringRanges[0].position;
677 int length = substringRanges[0].length;
678 if (position <= 0 && length >= thisSize)
680 return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
684 for (int i = 0; i < rangeCount; i++)
685 totalLength += substringRanges[i].length;
686 for (int i = 0; i < separatorCount; i++)
687 totalLength += separators[i].size();
689 if (totalLength == 0)
692 UChar* buffer = allocChars(totalLength);
696 int maxCount = max(rangeCount, separatorCount);
698 for (int i = 0; i < maxCount; i++) {
699 if (i < rangeCount) {
700 memcpy(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length * sizeof(UChar));
701 bufferPos += substringRanges[i].length;
703 if (i < separatorCount) {
704 memcpy(buffer + bufferPos, separators[i].data(), separators[i].size() * sizeof(UChar));
705 bufferPos += separators[i].size();
709 return UString::Rep::create(buffer, totalLength);
712 UString &UString::append(const UString &t)
714 int thisSize = size();
715 int thisOffset = m_rep->offset;
716 int tSize = t.size();
717 int length = thisSize + tSize;
723 } else if (tSize == 0) {
725 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
726 // this is direct and has refcount of 1 (so we can just alter it directly)
727 expandCapacity(thisOffset + length);
729 memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar));
733 } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
734 // this reaches the end of the buffer - extend it if it's long enough to append to
735 expandCapacity(thisOffset + length);
737 memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar));
738 m_rep = Rep::create(m_rep, 0, length);
741 // this is shared with someone using more capacity, gotta make a whole new string
742 size_t newCapacity = expandedSize(length, 0);
743 UChar* d = allocChars(newCapacity);
747 memcpy(d, data(), thisSize * sizeof(UChar));
748 memcpy(const_cast<UChar*>(d + thisSize), t.data(), tSize * sizeof(UChar));
749 m_rep = Rep::create(d, length);
750 m_rep->capacity = newCapacity;
757 UString &UString::append(const char *t)
759 int thisSize = size();
760 int thisOffset = m_rep->offset;
761 int tSize = static_cast<int>(strlen(t));
762 int length = thisSize + tSize;
768 } else if (tSize == 0) {
769 // t is empty, we'll just return *this below.
770 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
771 // this is direct and has refcount of 1 (so we can just alter it directly)
772 expandCapacity(thisOffset + length);
773 UChar *d = const_cast<UChar *>(data());
775 for (int i = 0; i < tSize; ++i)
776 d[thisSize + i] = t[i];
780 } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
781 // this string reaches the end of the buffer - extend it
782 expandCapacity(thisOffset + length);
783 UChar *d = const_cast<UChar *>(data());
785 for (int i = 0; i < tSize; ++i)
786 d[thisSize + i] = t[i];
787 m_rep = Rep::create(m_rep, 0, length);
790 // this is shared with someone using more capacity, gotta make a whole new string
791 size_t newCapacity = expandedSize(length, 0);
792 UChar* d = allocChars(newCapacity);
796 memcpy(d, data(), thisSize * sizeof(UChar));
797 for (int i = 0; i < tSize; ++i)
798 d[thisSize + i] = t[i];
799 m_rep = Rep::create(d, length);
800 m_rep->capacity = newCapacity;
807 UString &UString::append(unsigned short c)
809 int thisOffset = m_rep->offset;
814 // this is empty - must make a new m_rep because we don't want to pollute the shared empty one
815 size_t newCapacity = expandedSize(1, 0);
816 UChar* d = allocChars(newCapacity);
821 m_rep = Rep::create(d, 1);
822 m_rep->capacity = newCapacity;
824 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
825 // this is direct and has refcount of 1 (so we can just alter it directly)
826 expandCapacity(thisOffset + length + 1);
827 UChar *d = const_cast<UChar *>(data());
830 m_rep->len = length + 1;
833 } else if (thisOffset + length == usedCapacity() && length >= minShareSize) {
834 // this reaches the end of the string - extend it and share
835 expandCapacity(thisOffset + length + 1);
836 UChar *d = const_cast<UChar *>(data());
839 m_rep = Rep::create(m_rep, 0, length + 1);
842 // this is shared with someone using more capacity, gotta make a whole new string
843 size_t newCapacity = expandedSize(length + 1, 0);
844 UChar* d = allocChars(newCapacity);
848 memcpy(d, data(), length * sizeof(UChar));
850 m_rep = Rep::create(d, length + 1);
851 m_rep->capacity = newCapacity;
858 CString UString::cstring() const
863 char *UString::ascii() const
865 // Never make the buffer smaller than normalStatBufferSize.
866 // Thus we almost never need to reallocate.
868 int neededSize = length + 1;
869 if (neededSize < normalStatBufferSize) {
870 neededSize = normalStatBufferSize;
872 if (neededSize != statBufferSize) {
873 delete [] statBuffer;
874 statBuffer = new char [neededSize];
875 statBufferSize = neededSize;
878 const UChar *p = data();
879 char *q = statBuffer;
880 const UChar *limit = p + length;
882 *q = static_cast<char>(p->uc);
891 UString &UString::operator=(const char *c)
903 int l = static_cast<int>(strlen(c));
905 if (m_rep->rc == 1 && l <= m_rep->capacity && m_rep->baseIsSelf() && m_rep->offset == 0 && m_rep->preCapacity == 0) {
915 m_rep = Rep::create(d, l);
917 for (int i = 0; i < l; i++)
923 bool UString::is8Bit() const
925 const UChar *u = data();
926 const UChar *limit = u + size();
936 const UChar UString::operator[](int pos) const
943 double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
947 // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
948 // after the number, so is8Bit is too strict a check.
952 const char *c = ascii();
954 // skip leading white space
955 while (isASCIISpace(*c))
960 return tolerateEmptyString ? 0.0 : NaN;
963 if (*c == '0' && (*(c+1) == 'x' || *(c+1) == 'X')) {
964 const char* firstDigitPosition = c + 2;
968 if (*c >= '0' && *c <= '9')
969 d = d * 16.0 + *c - '0';
970 else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
971 d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
976 if (d >= mantissaOverflowLowerBound)
977 d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
981 d = kjs_strtod(c, &end);
982 if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
989 else if (*c == '-') {
994 // We used strtod() to do the conversion. However, strtod() handles
995 // infinite values slightly differently than JavaScript in that it
996 // converts the string "inf" with any capitalization to infinity,
997 // whereas the ECMA spec requires that it be converted to NaN.
999 if (c[0] == 'I' && c[1] == 'n' && c[2] == 'f' && c[3] == 'i' && c[4] == 'n' && c[5] == 'i' && c[6] == 't' && c[7] == 'y') {
1002 } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
1009 // allow trailing white space
1010 while (isASCIISpace(*c))
1012 // don't allow anything after - unless tolerant=true
1013 if (!tolerateTrailingJunk && *c != '\0')
1019 double UString::toDouble(bool tolerateTrailingJunk) const
1021 return toDouble(tolerateTrailingJunk, true);
1024 double UString::toDouble() const
1026 return toDouble(false, true);
1029 uint32_t UString::toUInt32(bool *ok) const
1031 double d = toDouble();
1034 if (d != static_cast<uint32_t>(d)) {
1042 return static_cast<uint32_t>(d);
1045 uint32_t UString::toUInt32(bool *ok, bool tolerateEmptyString) const
1047 double d = toDouble(false, tolerateEmptyString);
1050 if (d != static_cast<uint32_t>(d)) {
1058 return static_cast<uint32_t>(d);
1061 uint32_t UString::toStrictUInt32(bool *ok) const
1066 // Empty string is not OK.
1067 int len = m_rep->len;
1070 const UChar *p = m_rep->data();
1071 unsigned short c = p->unicode();
1073 // If the first digit is 0, only 0 itself is OK.
1080 // Convert to UInt32, checking for overflow.
1083 // Process character, turning it into a digit.
1084 if (c < '0' || c > '9')
1086 const unsigned d = c - '0';
1088 // Multiply by 10, checking for overflow out of 32 bits.
1089 if (i > 0xFFFFFFFFU / 10)
1093 // Add in the digit, checking for overflow out of 32 bits.
1094 const unsigned max = 0xFFFFFFFFU - d;
1099 // Handle end of string.
1106 // Get next character.
1107 c = (++p)->unicode();
1111 int UString::find(const UString &f, int pos) const
1121 const UChar *end = data() + sz - fsz;
1122 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1123 const UChar *fdata = f.data();
1124 unsigned short fchar = fdata->uc;
1126 for (const UChar *c = data() + pos; c <= end; c++)
1127 if (c->uc == fchar && !memcmp(c + 1, fdata, fsizeminusone))
1128 return static_cast<int>(c - data());
1133 int UString::find(UChar ch, int pos) const
1137 const UChar *end = data() + size();
1138 for (const UChar *c = data() + pos; c < end; c++)
1140 return static_cast<int>(c - data());
1145 int UString::rfind(const UString &f, int pos) const
1157 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1158 const UChar *fdata = f.data();
1159 for (const UChar *c = data() + pos; c >= data(); c--) {
1160 if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
1161 return static_cast<int>(c - data());
1167 int UString::rfind(UChar ch, int pos) const
1171 if (pos + 1 >= size())
1173 for (const UChar *c = data() + pos; c >= data(); c--) {
1175 return static_cast<int>(c-data());
1181 UString UString::substr(int pos, int len) const
1194 if (pos == 0 && len == s)
1197 return UString(Rep::create(m_rep, pos, len));
1200 bool operator==(const UString& s1, const UString& s2)
1202 if (s1.m_rep->len != s2.m_rep->len)
1205 return (memcmp(s1.m_rep->data(), s2.m_rep->data(),
1206 s1.m_rep->len * sizeof(UChar)) == 0);
1209 bool operator==(const UString& s1, const char *s2)
1212 return s1.isEmpty();
1215 const UChar *u = s1.data();
1216 const UChar *uend = u + s1.size();
1217 while (u != uend && *s2) {
1218 if (u->uc != (unsigned char)*s2)
1224 return u == uend && *s2 == 0;
1227 bool operator<(const UString& s1, const UString& s2)
1229 const int l1 = s1.size();
1230 const int l2 = s2.size();
1231 const int lmin = l1 < l2 ? l1 : l2;
1232 const UChar *c1 = s1.data();
1233 const UChar *c2 = s2.data();
1235 while (l < lmin && *c1 == *c2) {
1241 return (c1->uc < c2->uc);
1246 int compare(const UString& s1, const UString& s2)
1248 const int l1 = s1.size();
1249 const int l2 = s2.size();
1250 const int lmin = l1 < l2 ? l1 : l2;
1251 const UChar *c1 = s1.data();
1252 const UChar *c2 = s2.data();
1254 while (l < lmin && *c1 == *c2) {
1261 return (c1->uc > c2->uc) ? 1 : -1;
1266 return (l1 > l2) ? 1 : -1;
1269 CString UString::UTF8String(bool strict) const
1271 // Allocate a buffer big enough to hold all the characters.
1272 const int length = size();
1273 Vector<char, 1024> buffer(length * 3);
1275 // Convert to runs of 8-bit characters.
1276 char* p = buffer.data();
1277 const ::UChar* d = reinterpret_cast<const ::UChar*>(&data()->uc);
1278 ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
1279 if (result != conversionOK)
1282 return CString(buffer.data(), p - buffer.data());