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 // we'd rather not do shared substring append for small strings, since
81 // this runs too much risk of a tiny initial string holding down a
82 // huge buffer. This is also tuned to match the extra cost size, so we
83 // don't ever share a buffer that wouldn't be over the extra cost
85 // FIXME: this should be size_t but that would cause warnings until we
86 // fix UString sizes to be size_t instad of int
87 static const int minShareSize = Collector::minExtraCostSize / sizeof(UChar);
89 COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes)
91 CString::CString(const char *c)
94 data = new char[length+1];
95 memcpy(data, c, length + 1);
98 CString::CString(const char *c, size_t len)
101 data = new char[len+1];
102 memcpy(data, c, len);
106 CString::CString(const CString &b)
110 data = new char[length+1];
111 memcpy(data, b.data, length + 1);
122 CString &CString::append(const CString &t)
125 n = new char[length+t.length+1];
127 memcpy(n, data, length);
129 memcpy(n+length, t.data, t.length);
139 CString &CString::operator=(const char *c)
144 data = new char[length+1];
145 memcpy(data, c, length + 1);
150 CString &CString::operator=(const CString &str)
159 data = new char[length + 1];
160 memcpy(data, str.data, length + 1);
168 bool operator==(const CString& c1, const CString& c2)
170 size_t len = c1.size();
171 return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0);
174 // Hack here to avoid a global with a constructor; point to an unsigned short instead of a UChar.
175 static unsigned short almostUChar;
176 UString::Rep UString::Rep::null = { 0, 0, 1, 0, 0, &UString::Rep::null, 0, 0, 0, 0, 0 };
177 UString::Rep UString::Rep::empty = { 0, 0, 1, 0, 0, &UString::Rep::empty, reinterpret_cast<UChar*>(&almostUChar), 0, 0, 0, 0 };
178 const int normalStatBufferSize = 4096;
179 static char *statBuffer = 0; // FIXME: This buffer is never deallocated.
180 static int statBufferSize = 0;
182 PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int l)
184 ASSERT(JSLock::lockCount() > 0);
186 int sizeInBytes = l * sizeof(UChar);
187 UChar *copyD = static_cast<UChar *>(fastMalloc(sizeInBytes));
188 memcpy(copyD, d, sizeInBytes);
190 return create(copyD, l);
193 PassRefPtr<UString::Rep> UString::Rep::create(UChar *d, int l)
195 ASSERT(JSLock::lockCount() > 0);
207 r->usedPreCapacity = 0;
210 // steal the single reference this Rep was created with
214 PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length)
216 ASSERT(JSLock::lockCount() > 0);
219 int baseOffset = base->offset;
221 base = base->baseString;
223 ASSERT(-(offset + baseOffset) <= base->usedPreCapacity);
224 ASSERT(offset + baseOffset + length <= base->usedCapacity);
227 r->offset = baseOffset + offset;
232 r->baseString = base.releaseRef();
236 r->usedPreCapacity = 0;
239 // steal the single reference this Rep was created with
243 void UString::Rep::destroy()
245 ASSERT(JSLock::lockCount() > 0);
248 Identifier::remove(this);
249 if (baseString != this) {
257 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
258 // or anything like that.
259 const unsigned PHI = 0x9e3779b9U;
261 // Paul Hsieh's SuperFastHash
262 // http://www.azillionmonkeys.com/qed/hash.html
263 unsigned UString::Rep::computeHash(const UChar *s, int len)
275 tmp = (s[1].uc << 11) ^ hash;
276 hash = (hash << 16) ^ tmp;
288 // Force "avalanching" of final 127 bits
295 // this avoids ever returning a hash code of 0, since that is used to
296 // signal "hash not computed yet", using a value that is likely to be
297 // effectively the same as 0 when the low bits are masked
304 // Paul Hsieh's SuperFastHash
305 // http://www.azillionmonkeys.com/qed/hash.html
306 unsigned UString::Rep::computeHash(const char *s)
308 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
309 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
310 // were 16-bit chunks, which should give matching results
314 size_t l = strlen(s);
321 hash += (unsigned char)s[0];
322 tmp = ((unsigned char)s[1] << 11) ^ hash;
323 hash = (hash << 16) ^ tmp;
330 hash += (unsigned char)s[0];
335 // Force "avalanching" of final 127 bits
342 // this avoids ever returning a hash code of 0, since that is used to
343 // signal "hash not computed yet", using a value that is likely to be
344 // effectively the same as 0 when the low bits are masked
351 // put these early so they can be inlined
352 inline size_t UString::expandedSize(size_t size, size_t otherSize) const
354 // Do the size calculation in two parts, returning overflowIndicator if
355 // we overflow the maximum value that we can handle.
357 if (size > maxUChars())
358 return overflowIndicator();
360 size_t expandedSize = ((size + 10) / 10 * 11) + 1;
361 if (maxUChars() - expandedSize < otherSize)
362 return overflowIndicator();
364 return expandedSize + otherSize;
367 inline int UString::usedCapacity() const
369 return m_rep->baseString->usedCapacity;
372 inline int UString::usedPreCapacity() const
374 return m_rep->baseString->usedPreCapacity;
377 void UString::expandCapacity(int requiredLength)
379 Rep* r = m_rep->baseString;
381 if (requiredLength > r->capacity) {
382 size_t newCapacity = expandedSize(requiredLength, r->preCapacity);
383 UChar* oldBuf = r->buf;
384 r->buf = reallocChars(r->buf, newCapacity);
390 r->capacity = newCapacity - r->preCapacity;
392 if (requiredLength > r->usedCapacity) {
393 r->usedCapacity = requiredLength;
397 void UString::expandPreCapacity(int requiredPreCap)
399 Rep* r = m_rep->baseString;
401 if (requiredPreCap > r->preCapacity) {
402 size_t newCapacity = expandedSize(requiredPreCap, r->capacity);
403 int delta = newCapacity - r->capacity - r->preCapacity;
405 UChar* newBuf = allocChars(newCapacity);
410 memcpy(newBuf + delta, r->buf, (r->capacity + r->preCapacity) * sizeof(UChar));
414 r->preCapacity = newCapacity - r->capacity;
416 if (requiredPreCap > r->usedPreCapacity) {
417 r->usedPreCapacity = requiredPreCap;
421 UString::UString(const char *c)
433 size_t length = strlen(c);
434 UChar *d = allocChars(length);
438 for (size_t i = 0; i < length; i++)
440 m_rep = Rep::create(d, static_cast<int>(length));
444 UString::UString(const UChar *c, int length)
449 m_rep = Rep::createCopying(c, length);
452 UString::UString(UChar *c, int length, bool copy)
457 m_rep = Rep::createCopying(c, length);
459 m_rep = Rep::create(c, length);
462 UString::UString(const UString &a, const UString &b)
464 int aSize = a.size();
465 int aOffset = a.m_rep->offset;
466 int bSize = b.size();
467 int bOffset = b.m_rep->offset;
468 int length = aSize + bSize;
475 } else if (bSize == 0) {
478 } else if (aOffset + aSize == a.usedCapacity() && aSize >= minShareSize && 4 * aSize >= bSize &&
479 (-bOffset != b.usedPreCapacity() || aSize >= bSize)) {
480 // - a reaches the end of its buffer so it qualifies for shared append
481 // - also, it's at least a quarter the length of b - appending to a much shorter
482 // string does more harm than good
483 // - however, if b qualifies for prepend and is longer than a, we'd rather prepend
485 x.expandCapacity(aOffset + length);
486 if (a.data() && x.data()) {
487 memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar));
488 m_rep = Rep::create(a.m_rep, 0, length);
491 } else if (-bOffset == b.usedPreCapacity() && bSize >= minShareSize && 4 * bSize >= aSize) {
492 // - b reaches the beginning of its buffer so it qualifies for shared prepend
493 // - also, it's at least a quarter the length of a - prepending to a much shorter
494 // string does more harm than good
496 y.expandPreCapacity(-bOffset + aSize);
497 if (b.data() && y.data()) {
498 memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar));
499 m_rep = Rep::create(b.m_rep, -aSize, length);
503 // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
504 size_t newCapacity = expandedSize(length, 0);
505 UChar* d = allocChars(newCapacity);
509 memcpy(d, a.data(), aSize * sizeof(UChar));
510 memcpy(d + aSize, b.data(), bSize * sizeof(UChar));
511 m_rep = Rep::create(d, length);
512 m_rep->capacity = newCapacity;
517 const UString& UString::null()
519 static UString* n = new UString;
523 UString UString::from(int i)
525 UChar buf[1 + sizeof(i) * 3];
526 UChar *end = buf + sizeof(buf) / sizeof(UChar);
531 } else if (i == INT_MIN) {
532 char minBuf[1 + sizeof(i) * 3];
533 sprintf(minBuf, "%d", INT_MIN);
534 return UString(minBuf);
536 bool negative = false;
542 *--p = (unsigned short)((i % 10) + '0');
550 return UString(p, static_cast<int>(end - p));
553 UString UString::from(unsigned int u)
555 UChar buf[sizeof(u) * 3];
556 UChar *end = buf + sizeof(buf) / sizeof(UChar);
563 *--p = (unsigned short)((u % 10) + '0');
568 return UString(p, static_cast<int>(end - p));
571 UString UString::from(long l)
573 UChar buf[1 + sizeof(l) * 3];
574 UChar *end = buf + sizeof(buf) / sizeof(UChar);
579 } else if (l == LONG_MIN) {
580 char minBuf[1 + sizeof(l) * 3];
581 sprintf(minBuf, "%ld", LONG_MIN);
582 return UString(minBuf);
584 bool negative = false;
590 *--p = (unsigned short)((l % 10) + '0');
598 return UString(p, static_cast<int>(end - p));
601 UString UString::from(double d)
603 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
611 char *result = kjs_dtoa(d, 0, 0, &decimalPoint, &sign, NULL);
612 int length = static_cast<int>(strlen(result));
619 if (decimalPoint <= 0 && decimalPoint > -6) {
622 for (int j = decimalPoint; j < 0; j++) {
625 strcpy(buf + i, result);
626 } else if (decimalPoint <= 21 && decimalPoint > 0) {
627 if (length <= decimalPoint) {
628 strcpy(buf + i, result);
630 for (int j = 0; j < decimalPoint - length; j++) {
635 strncpy(buf + i, result, decimalPoint);
638 strcpy(buf + i, result + decimalPoint);
640 } else if (result[0] < '0' || result[0] > '9') {
641 strcpy(buf + i, result);
643 buf[i++] = result[0];
646 strcpy(buf + i, result + 1);
651 buf[i++] = (decimalPoint >= 0) ? '+' : '-';
652 // decimalPoint can't be more than 3 digits decimal given the
653 // nature of float representation
654 int exponential = decimalPoint - 1;
656 exponential = -exponential;
657 if (exponential >= 100)
658 buf[i++] = static_cast<char>('0' + exponential / 100);
659 if (exponential >= 10)
660 buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
661 buf[i++] = static_cast<char>('0' + exponential % 10);
665 kjs_freedtoa(result);
670 UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
672 if (rangeCount == 1 && separatorCount == 0) {
673 int thisSize = size();
674 int position = substringRanges[0].position;
675 int length = substringRanges[0].length;
676 if (position <= 0 && length >= thisSize)
678 return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
682 for (int i = 0; i < rangeCount; i++)
683 totalLength += substringRanges[i].length;
684 for (int i = 0; i < separatorCount; i++)
685 totalLength += separators[i].size();
687 if (totalLength == 0)
690 UChar* buffer = allocChars(totalLength);
694 int maxCount = max(rangeCount, separatorCount);
696 for (int i = 0; i < maxCount; i++) {
697 if (i < rangeCount) {
698 memcpy(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length * sizeof(UChar));
699 bufferPos += substringRanges[i].length;
701 if (i < separatorCount) {
702 memcpy(buffer + bufferPos, separators[i].data(), separators[i].size() * sizeof(UChar));
703 bufferPos += separators[i].size();
707 return UString::Rep::create(buffer, totalLength);
710 UString &UString::append(const UString &t)
712 int thisSize = size();
713 int thisOffset = m_rep->offset;
714 int tSize = t.size();
715 int length = thisSize + tSize;
721 } else if (tSize == 0) {
723 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
724 // this is direct and has refcount of 1 (so we can just alter it directly)
725 expandCapacity(thisOffset + length);
727 memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar));
731 } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
732 // this reaches the end of the buffer - extend it if it's long enough to append to
733 expandCapacity(thisOffset + length);
735 memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar));
736 m_rep = Rep::create(m_rep, 0, length);
739 // this is shared with someone using more capacity, gotta make a whole new string
740 size_t newCapacity = expandedSize(length, 0);
741 UChar* d = allocChars(newCapacity);
745 memcpy(d, data(), thisSize * sizeof(UChar));
746 memcpy(const_cast<UChar*>(d + thisSize), t.data(), tSize * sizeof(UChar));
747 m_rep = Rep::create(d, length);
748 m_rep->capacity = newCapacity;
755 UString &UString::append(const char *t)
757 int thisSize = size();
758 int thisOffset = m_rep->offset;
759 int tSize = static_cast<int>(strlen(t));
760 int length = thisSize + tSize;
766 } else if (tSize == 0) {
767 // t is empty, we'll just return *this below.
768 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
769 // this is direct and has refcount of 1 (so we can just alter it directly)
770 expandCapacity(thisOffset + length);
771 UChar *d = const_cast<UChar *>(data());
773 for (int i = 0; i < tSize; ++i)
774 d[thisSize + i] = t[i];
778 } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
779 // this string reaches the end of the buffer - extend it
780 expandCapacity(thisOffset + length);
781 UChar *d = const_cast<UChar *>(data());
783 for (int i = 0; i < tSize; ++i)
784 d[thisSize + i] = t[i];
785 m_rep = Rep::create(m_rep, 0, length);
788 // this is shared with someone using more capacity, gotta make a whole new string
789 size_t newCapacity = expandedSize(length, 0);
790 UChar* d = allocChars(newCapacity);
794 memcpy(d, data(), thisSize * sizeof(UChar));
795 for (int i = 0; i < tSize; ++i)
796 d[thisSize + i] = t[i];
797 m_rep = Rep::create(d, length);
798 m_rep->capacity = newCapacity;
805 UString &UString::append(unsigned short c)
807 int thisOffset = m_rep->offset;
812 // this is empty - must make a new m_rep because we don't want to pollute the shared empty one
813 size_t newCapacity = expandedSize(1, 0);
814 UChar* d = allocChars(newCapacity);
819 m_rep = Rep::create(d, 1);
820 m_rep->capacity = newCapacity;
822 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
823 // this is direct and has refcount of 1 (so we can just alter it directly)
824 expandCapacity(thisOffset + length + 1);
825 UChar *d = const_cast<UChar *>(data());
828 m_rep->len = length + 1;
831 } else if (thisOffset + length == usedCapacity() && length >= minShareSize) {
832 // this reaches the end of the string - extend it and share
833 expandCapacity(thisOffset + length + 1);
834 UChar *d = const_cast<UChar *>(data());
837 m_rep = Rep::create(m_rep, 0, length + 1);
840 // this is shared with someone using more capacity, gotta make a whole new string
841 size_t newCapacity = expandedSize(length + 1, 0);
842 UChar* d = allocChars(newCapacity);
846 memcpy(d, data(), length * sizeof(UChar));
848 m_rep = Rep::create(d, length + 1);
849 m_rep->capacity = newCapacity;
856 CString UString::cstring() const
861 char *UString::ascii() const
863 // Never make the buffer smaller than normalStatBufferSize.
864 // Thus we almost never need to reallocate.
866 int neededSize = length + 1;
867 if (neededSize < normalStatBufferSize) {
868 neededSize = normalStatBufferSize;
870 if (neededSize != statBufferSize) {
871 delete [] statBuffer;
872 statBuffer = new char [neededSize];
873 statBufferSize = neededSize;
876 const UChar *p = data();
877 char *q = statBuffer;
878 const UChar *limit = p + length;
880 *q = static_cast<char>(p->uc);
889 UString &UString::operator=(const char *c)
901 int l = static_cast<int>(strlen(c));
903 if (m_rep->rc == 1 && l <= m_rep->capacity && m_rep->baseIsSelf() && m_rep->offset == 0 && m_rep->preCapacity == 0) {
913 m_rep = Rep::create(d, l);
915 for (int i = 0; i < l; i++)
921 bool UString::is8Bit() const
923 const UChar *u = data();
924 const UChar *limit = u + size();
934 const UChar UString::operator[](int pos) const
941 double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
945 // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
946 // after the number, so is8Bit is too strict a check.
950 const char *c = ascii();
952 // skip leading white space
953 while (isASCIISpace(*c))
958 return tolerateEmptyString ? 0.0 : NaN;
961 if (*c == '0' && (*(c+1) == 'x' || *(c+1) == 'X')) {
962 const char* firstDigitPosition = c + 2;
966 if (*c >= '0' && *c <= '9')
967 d = d * 16.0 + *c - '0';
968 else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
969 d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
974 if (d >= mantissaOverflowLowerBound)
975 d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
979 d = kjs_strtod(c, &end);
980 if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
987 else if (*c == '-') {
992 // We used strtod() to do the conversion. However, strtod() handles
993 // infinite values slightly differently than JavaScript in that it
994 // converts the string "inf" with any capitalization to infinity,
995 // whereas the ECMA spec requires that it be converted to NaN.
997 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') {
1000 } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
1007 // allow trailing white space
1008 while (isASCIISpace(*c))
1010 // don't allow anything after - unless tolerant=true
1011 if (!tolerateTrailingJunk && *c != '\0')
1017 double UString::toDouble(bool tolerateTrailingJunk) const
1019 return toDouble(tolerateTrailingJunk, true);
1022 double UString::toDouble() const
1024 return toDouble(false, true);
1027 uint32_t UString::toUInt32(bool *ok) const
1029 double d = toDouble();
1032 if (d != static_cast<uint32_t>(d)) {
1040 return static_cast<uint32_t>(d);
1043 uint32_t UString::toUInt32(bool *ok, bool tolerateEmptyString) const
1045 double d = toDouble(false, tolerateEmptyString);
1048 if (d != static_cast<uint32_t>(d)) {
1056 return static_cast<uint32_t>(d);
1059 uint32_t UString::toStrictUInt32(bool *ok) const
1064 // Empty string is not OK.
1065 int len = m_rep->len;
1068 const UChar *p = m_rep->data();
1069 unsigned short c = p->unicode();
1071 // If the first digit is 0, only 0 itself is OK.
1078 // Convert to UInt32, checking for overflow.
1081 // Process character, turning it into a digit.
1082 if (c < '0' || c > '9')
1084 const unsigned d = c - '0';
1086 // Multiply by 10, checking for overflow out of 32 bits.
1087 if (i > 0xFFFFFFFFU / 10)
1091 // Add in the digit, checking for overflow out of 32 bits.
1092 const unsigned max = 0xFFFFFFFFU - d;
1097 // Handle end of string.
1104 // Get next character.
1105 c = (++p)->unicode();
1109 int UString::find(const UString &f, int pos) const
1119 const UChar *end = data() + sz - fsz;
1120 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1121 const UChar *fdata = f.data();
1122 unsigned short fchar = fdata->uc;
1124 for (const UChar *c = data() + pos; c <= end; c++)
1125 if (c->uc == fchar && !memcmp(c + 1, fdata, fsizeminusone))
1126 return static_cast<int>(c - data());
1131 int UString::find(UChar ch, int pos) const
1135 const UChar *end = data() + size();
1136 for (const UChar *c = data() + pos; c < end; c++)
1138 return static_cast<int>(c - data());
1143 int UString::rfind(const UString &f, int pos) const
1155 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1156 const UChar *fdata = f.data();
1157 for (const UChar *c = data() + pos; c >= data(); c--) {
1158 if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
1159 return static_cast<int>(c - data());
1165 int UString::rfind(UChar ch, int pos) const
1169 if (pos + 1 >= size())
1171 for (const UChar *c = data() + pos; c >= data(); c--) {
1173 return static_cast<int>(c-data());
1179 UString UString::substr(int pos, int len) const
1192 if (pos == 0 && len == s)
1195 return UString(Rep::create(m_rep, pos, len));
1198 bool operator==(const UString& s1, const UString& s2)
1200 if (s1.m_rep->len != s2.m_rep->len)
1203 return (memcmp(s1.m_rep->data(), s2.m_rep->data(),
1204 s1.m_rep->len * sizeof(UChar)) == 0);
1207 bool operator==(const UString& s1, const char *s2)
1210 return s1.isEmpty();
1213 const UChar *u = s1.data();
1214 const UChar *uend = u + s1.size();
1215 while (u != uend && *s2) {
1216 if (u->uc != (unsigned char)*s2)
1222 return u == uend && *s2 == 0;
1225 bool operator<(const UString& s1, const UString& s2)
1227 const int l1 = s1.size();
1228 const int l2 = s2.size();
1229 const int lmin = l1 < l2 ? l1 : l2;
1230 const UChar *c1 = s1.data();
1231 const UChar *c2 = s2.data();
1233 while (l < lmin && *c1 == *c2) {
1239 return (c1->uc < c2->uc);
1244 int compare(const UString& s1, const UString& s2)
1246 const int l1 = s1.size();
1247 const int l2 = s2.size();
1248 const int lmin = l1 < l2 ? l1 : l2;
1249 const UChar *c1 = s1.data();
1250 const UChar *c2 = s2.data();
1252 while (l < lmin && *c1 == *c2) {
1259 return (c1->uc > c2->uc) ? 1 : -1;
1264 return (l1 > l2) ? 1 : -1;
1267 CString UString::UTF8String(bool strict) const
1269 // Allocate a buffer big enough to hold all the characters.
1270 const int length = size();
1271 Vector<char, 1024> buffer(length * 3);
1273 // Convert to runs of 8-bit characters.
1274 char* p = buffer.data();
1275 const ::UChar* d = reinterpret_cast<const ::UChar*>(&data()->uc);
1276 ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
1277 if (result != conversionOK)
1280 return CString(buffer.data(), p - buffer.data());