1 // -*- c-basic-offset: 2 -*-
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 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::adopt(char* c, size_t len)
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 // These static strings are immutable, except for rc, whose initial value is chosen to reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
175 static UChar sharedEmptyChar;
176 UString::Rep UString::Rep::null = { 0, 0, INT_MAX / 2, 0, 0, &UString::Rep::null, true, 0, 0, 0, 0, 0, 0 };
177 UString::Rep UString::Rep::empty = { 0, 0, INT_MAX / 2, 0, 0, &UString::Rep::empty, true, 0, &sharedEmptyChar, 0, 0, 0, 0 };
179 static char* statBuffer = 0; // Only used for debugging via UString::ascii().
181 PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int l)
183 int sizeInBytes = l * sizeof(UChar);
184 UChar *copyD = static_cast<UChar *>(fastMalloc(sizeInBytes));
185 memcpy(copyD, d, sizeInBytes);
187 return create(copyD, l);
190 PassRefPtr<UString::Rep> UString::Rep::create(UChar *d, int l)
197 r->identifierTable = 0;
204 r->usedPreCapacity = 0;
207 // steal the single reference this Rep was created with
211 PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length)
215 int baseOffset = base->offset;
217 base = base->baseString;
219 ASSERT(-(offset + baseOffset) <= base->usedPreCapacity);
220 ASSERT(offset + baseOffset + length <= base->usedCapacity);
223 r->offset = baseOffset + offset;
227 r->identifierTable = 0;
228 r->baseString = base.releaseRef();
234 r->usedPreCapacity = 0;
237 // steal the single reference this Rep was created with
241 PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string)
244 return &UString::Rep::null;
246 size_t length = strlen(string);
247 Vector<UChar, 1024> buffer(length);
248 UChar* p = buffer.data();
249 if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
250 return &UString::Rep::null;
252 return UString::Rep::createCopying(buffer.data(), p - buffer.data());
255 void UString::Rep::destroy()
257 // Static null and empty strings can never be destroyed, but we cannot rely on reference counting, because ref/deref are not thread-safe.
260 Identifier::remove(this);
261 if (baseString == this)
270 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
271 // or anything like that.
272 const unsigned PHI = 0x9e3779b9U;
274 // Paul Hsieh's SuperFastHash
275 // http://www.azillionmonkeys.com/qed/hash.html
276 unsigned UString::Rep::computeHash(const UChar *s, int len)
288 tmp = (s[1] << 11) ^ hash;
289 hash = (hash << 16) ^ tmp;
301 // Force "avalanching" of final 127 bits
308 // this avoids ever returning a hash code of 0, since that is used to
309 // signal "hash not computed yet", using a value that is likely to be
310 // effectively the same as 0 when the low bits are masked
317 // Paul Hsieh's SuperFastHash
318 // http://www.azillionmonkeys.com/qed/hash.html
319 unsigned UString::Rep::computeHash(const char *s)
321 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
322 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
323 // were 16-bit chunks, which should give matching results
327 size_t l = strlen(s);
334 hash += (unsigned char)s[0];
335 tmp = ((unsigned char)s[1] << 11) ^ hash;
336 hash = (hash << 16) ^ tmp;
343 hash += (unsigned char)s[0];
348 // Force "avalanching" of final 127 bits
355 // this avoids ever returning a hash code of 0, since that is used to
356 // signal "hash not computed yet", using a value that is likely to be
357 // effectively the same as 0 when the low bits are masked
364 // put these early so they can be inlined
365 inline size_t UString::expandedSize(size_t size, size_t otherSize) const
367 // Do the size calculation in two parts, returning overflowIndicator if
368 // we overflow the maximum value that we can handle.
370 if (size > maxUChars())
371 return overflowIndicator();
373 size_t expandedSize = ((size + 10) / 10 * 11) + 1;
374 if (maxUChars() - expandedSize < otherSize)
375 return overflowIndicator();
377 return expandedSize + otherSize;
380 inline int UString::usedCapacity() const
382 return m_rep->baseString->usedCapacity;
385 inline int UString::usedPreCapacity() const
387 return m_rep->baseString->usedPreCapacity;
390 void UString::expandCapacity(int requiredLength)
392 Rep* r = m_rep->baseString;
394 if (requiredLength > r->capacity) {
395 size_t newCapacity = expandedSize(requiredLength, r->preCapacity);
396 UChar* oldBuf = r->buf;
397 r->buf = reallocChars(r->buf, newCapacity);
403 r->capacity = newCapacity - r->preCapacity;
405 if (requiredLength > r->usedCapacity) {
406 r->usedCapacity = requiredLength;
410 void UString::expandPreCapacity(int requiredPreCap)
412 Rep* r = m_rep->baseString;
414 if (requiredPreCap > r->preCapacity) {
415 size_t newCapacity = expandedSize(requiredPreCap, r->capacity);
416 int delta = newCapacity - r->capacity - r->preCapacity;
418 UChar* newBuf = allocChars(newCapacity);
423 memcpy(newBuf + delta, r->buf, (r->capacity + r->preCapacity) * sizeof(UChar));
427 r->preCapacity = newCapacity - r->capacity;
429 if (requiredPreCap > r->usedPreCapacity) {
430 r->usedPreCapacity = requiredPreCap;
434 UString::UString(const char *c)
446 size_t length = strlen(c);
447 UChar *d = allocChars(length);
451 for (size_t i = 0; i < length; i++)
452 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
453 m_rep = Rep::create(d, static_cast<int>(length));
457 UString::UString(const UChar *c, int length)
462 m_rep = Rep::createCopying(c, length);
465 UString::UString(UChar *c, int length, bool copy)
470 m_rep = Rep::createCopying(c, length);
472 m_rep = Rep::create(c, length);
475 UString::UString(const Vector<UChar>& buffer)
480 m_rep = Rep::createCopying(buffer.data(), buffer.size());
484 UString::UString(const UString &a, const UString &b)
486 int aSize = a.size();
487 int aOffset = a.m_rep->offset;
488 int bSize = b.size();
489 int bOffset = b.m_rep->offset;
490 int length = aSize + bSize;
497 } else if (bSize == 0) {
500 } else if (aOffset + aSize == a.usedCapacity() && aSize >= minShareSize && 4 * aSize >= bSize &&
501 (-bOffset != b.usedPreCapacity() || aSize >= bSize)) {
502 // - a reaches the end of its buffer so it qualifies for shared append
503 // - also, it's at least a quarter the length of b - appending to a much shorter
504 // string does more harm than good
505 // - however, if b qualifies for prepend and is longer than a, we'd rather prepend
507 x.expandCapacity(aOffset + length);
508 if (a.data() && x.data()) {
509 memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar));
510 m_rep = Rep::create(a.m_rep, 0, length);
513 } else if (-bOffset == b.usedPreCapacity() && bSize >= minShareSize && 4 * bSize >= aSize) {
514 // - b reaches the beginning of its buffer so it qualifies for shared prepend
515 // - also, it's at least a quarter the length of a - prepending to a much shorter
516 // string does more harm than good
518 y.expandPreCapacity(-bOffset + aSize);
519 if (b.data() && y.data()) {
520 memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar));
521 m_rep = Rep::create(b.m_rep, -aSize, length);
525 // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
526 size_t newCapacity = expandedSize(length, 0);
527 UChar* d = allocChars(newCapacity);
531 memcpy(d, a.data(), aSize * sizeof(UChar));
532 memcpy(d + aSize, b.data(), bSize * sizeof(UChar));
533 m_rep = Rep::create(d, length);
534 m_rep->capacity = newCapacity;
539 const UString& UString::null()
541 static UString* n = new UString; // Should be called from main thread at least once to be safely initialized.
545 UString UString::from(int i)
547 UChar buf[1 + sizeof(i) * 3];
548 UChar *end = buf + sizeof(buf) / sizeof(UChar);
553 } else if (i == INT_MIN) {
554 char minBuf[1 + sizeof(i) * 3];
555 sprintf(minBuf, "%d", INT_MIN);
556 return UString(minBuf);
558 bool negative = false;
564 *--p = (unsigned short)((i % 10) + '0');
572 return UString(p, static_cast<int>(end - p));
575 UString UString::from(unsigned int u)
577 UChar buf[sizeof(u) * 3];
578 UChar *end = buf + sizeof(buf) / sizeof(UChar);
585 *--p = (unsigned short)((u % 10) + '0');
590 return UString(p, static_cast<int>(end - p));
593 UString UString::from(long l)
595 UChar buf[1 + sizeof(l) * 3];
596 UChar *end = buf + sizeof(buf) / sizeof(UChar);
601 } else if (l == LONG_MIN) {
602 char minBuf[1 + sizeof(l) * 3];
603 sprintf(minBuf, "%ld", LONG_MIN);
604 return UString(minBuf);
606 bool negative = false;
612 *--p = (unsigned short)((l % 10) + '0');
620 return UString(p, static_cast<int>(end - p));
623 UString UString::from(double d)
625 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
633 char *result = dtoa(d, 0, &decimalPoint, &sign, NULL);
634 int length = static_cast<int>(strlen(result));
641 if (decimalPoint <= 0 && decimalPoint > -6) {
644 for (int j = decimalPoint; j < 0; j++) {
647 strcpy(buf + i, result);
648 } else if (decimalPoint <= 21 && decimalPoint > 0) {
649 if (length <= decimalPoint) {
650 strcpy(buf + i, result);
652 for (int j = 0; j < decimalPoint - length; j++) {
657 strncpy(buf + i, result, decimalPoint);
660 strcpy(buf + i, result + decimalPoint);
662 } else if (result[0] < '0' || result[0] > '9') {
663 strcpy(buf + i, result);
665 buf[i++] = result[0];
668 strcpy(buf + i, result + 1);
673 buf[i++] = (decimalPoint >= 0) ? '+' : '-';
674 // decimalPoint can't be more than 3 digits decimal given the
675 // nature of float representation
676 int exponential = decimalPoint - 1;
678 exponential = -exponential;
679 if (exponential >= 100)
680 buf[i++] = static_cast<char>('0' + exponential / 100);
681 if (exponential >= 10)
682 buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
683 buf[i++] = static_cast<char>('0' + exponential % 10);
692 UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
694 if (rangeCount == 1 && separatorCount == 0) {
695 int thisSize = size();
696 int position = substringRanges[0].position;
697 int length = substringRanges[0].length;
698 if (position <= 0 && length >= thisSize)
700 return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
704 for (int i = 0; i < rangeCount; i++)
705 totalLength += substringRanges[i].length;
706 for (int i = 0; i < separatorCount; i++)
707 totalLength += separators[i].size();
709 if (totalLength == 0)
712 UChar* buffer = allocChars(totalLength);
716 int maxCount = max(rangeCount, separatorCount);
718 for (int i = 0; i < maxCount; i++) {
719 if (i < rangeCount) {
720 memcpy(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length * sizeof(UChar));
721 bufferPos += substringRanges[i].length;
723 if (i < separatorCount) {
724 memcpy(buffer + bufferPos, separators[i].data(), separators[i].size() * sizeof(UChar));
725 bufferPos += separators[i].size();
729 return UString::Rep::create(buffer, totalLength);
732 UString& UString::append(const UString &t)
734 int thisSize = size();
735 int thisOffset = m_rep->offset;
736 int tSize = t.size();
737 int length = thisSize + tSize;
743 } else if (tSize == 0) {
745 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
746 // this is direct and has refcount of 1 (so we can just alter it directly)
747 expandCapacity(thisOffset + length);
749 memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar));
753 } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
754 // this reaches the end of the buffer - extend it if it's long enough to append to
755 expandCapacity(thisOffset + length);
757 memcpy(const_cast<UChar*>(data() + thisSize), t.data(), tSize * sizeof(UChar));
758 m_rep = Rep::create(m_rep, 0, length);
761 // this is shared with someone using more capacity, gotta make a whole new string
762 size_t newCapacity = expandedSize(length, 0);
763 UChar* d = allocChars(newCapacity);
767 memcpy(d, data(), thisSize * sizeof(UChar));
768 memcpy(const_cast<UChar*>(d + thisSize), t.data(), tSize * sizeof(UChar));
769 m_rep = Rep::create(d, length);
770 m_rep->capacity = newCapacity;
777 UString& UString::append(const char *t)
779 int thisSize = size();
780 int thisOffset = m_rep->offset;
781 int tSize = static_cast<int>(strlen(t));
782 int length = thisSize + tSize;
788 } else if (tSize == 0) {
789 // t is empty, we'll just return *this below.
790 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
791 // this is direct and has refcount of 1 (so we can just alter it directly)
792 expandCapacity(thisOffset + length);
793 UChar *d = const_cast<UChar *>(data());
795 for (int i = 0; i < tSize; ++i)
796 d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
800 } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
801 // this string reaches the end of the buffer - extend it
802 expandCapacity(thisOffset + length);
803 UChar *d = const_cast<UChar *>(data());
805 for (int i = 0; i < tSize; ++i)
806 d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
807 m_rep = Rep::create(m_rep, 0, length);
810 // this is shared with someone using more capacity, gotta make a whole new string
811 size_t newCapacity = expandedSize(length, 0);
812 UChar* d = allocChars(newCapacity);
816 memcpy(d, data(), thisSize * sizeof(UChar));
817 for (int i = 0; i < tSize; ++i)
818 d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
819 m_rep = Rep::create(d, length);
820 m_rep->capacity = newCapacity;
827 UString& UString::append(UChar c)
829 int thisOffset = m_rep->offset;
834 // this is empty - must make a new m_rep because we don't want to pollute the shared empty one
835 size_t newCapacity = expandedSize(1, 0);
836 UChar* d = allocChars(newCapacity);
841 m_rep = Rep::create(d, 1);
842 m_rep->capacity = newCapacity;
844 } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
845 // this is direct and has refcount of 1 (so we can just alter it directly)
846 expandCapacity(thisOffset + length + 1);
847 UChar *d = const_cast<UChar *>(data());
850 m_rep->len = length + 1;
853 } else if (thisOffset + length == usedCapacity() && length >= minShareSize) {
854 // this reaches the end of the string - extend it and share
855 expandCapacity(thisOffset + length + 1);
856 UChar *d = const_cast<UChar *>(data());
859 m_rep = Rep::create(m_rep, 0, length + 1);
862 // this is shared with someone using more capacity, gotta make a whole new string
863 size_t newCapacity = expandedSize(length + 1, 0);
864 UChar* d = allocChars(newCapacity);
868 memcpy(d, data(), length * sizeof(UChar));
870 m_rep = Rep::create(d, length + 1);
871 m_rep->capacity = newCapacity;
878 bool UString::getCString(CStringBuffer& buffer) const
881 int neededSize = length + 1;
882 buffer.resize(neededSize);
883 char* buf = buffer.data();
886 const UChar* p = data();
888 const UChar* limit = p + length;
892 *q = static_cast<char>(c);
898 return !(ored & 0xFF00);
901 char *UString::ascii() const
904 int neededSize = length + 1;
906 statBuffer = new char[neededSize];
908 const UChar *p = data();
909 char *q = statBuffer;
910 const UChar *limit = p + length;
912 *q = static_cast<char>(p[0]);
921 UString& UString::operator=(const char *c)
933 int l = static_cast<int>(strlen(c));
935 if (m_rep->rc == 1 && l <= m_rep->capacity && m_rep->baseIsSelf() && m_rep->offset == 0 && m_rep->preCapacity == 0) {
945 m_rep = Rep::create(d, l);
947 for (int i = 0; i < l; i++)
948 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
953 bool UString::is8Bit() const
955 const UChar *u = data();
956 const UChar *limit = u + size();
966 UChar UString::operator[](int pos) const
973 double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
977 // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
978 // after the number, so this is too strict a check.
982 const char* c = s.data();
984 // skip leading white space
985 while (isASCIISpace(*c))
990 return tolerateEmptyString ? 0.0 : NaN;
993 if (*c == '0' && (*(c+1) == 'x' || *(c+1) == 'X')) {
994 const char* firstDigitPosition = c + 2;
998 if (*c >= '0' && *c <= '9')
999 d = d * 16.0 + *c - '0';
1000 else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
1001 d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
1006 if (d >= mantissaOverflowLowerBound)
1007 d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
1011 d = strtod(c, &end);
1012 if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
1019 else if (*c == '-') {
1024 // We used strtod() to do the conversion. However, strtod() handles
1025 // infinite values slightly differently than JavaScript in that it
1026 // converts the string "inf" with any capitalization to infinity,
1027 // whereas the ECMA spec requires that it be converted to NaN.
1029 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') {
1032 } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
1039 // allow trailing white space
1040 while (isASCIISpace(*c))
1042 // don't allow anything after - unless tolerant=true
1043 if (!tolerateTrailingJunk && *c != '\0')
1049 double UString::toDouble(bool tolerateTrailingJunk) const
1051 return toDouble(tolerateTrailingJunk, true);
1054 double UString::toDouble() const
1056 return toDouble(false, true);
1059 uint32_t UString::toUInt32(bool *ok) const
1061 double d = toDouble();
1064 if (d != static_cast<uint32_t>(d)) {
1072 return static_cast<uint32_t>(d);
1075 uint32_t UString::toUInt32(bool *ok, bool tolerateEmptyString) const
1077 double d = toDouble(false, tolerateEmptyString);
1080 if (d != static_cast<uint32_t>(d)) {
1088 return static_cast<uint32_t>(d);
1091 uint32_t UString::toStrictUInt32(bool *ok) const
1096 // Empty string is not OK.
1097 int len = m_rep->len;
1100 const UChar *p = m_rep->data();
1101 unsigned short c = p[0];
1103 // If the first digit is 0, only 0 itself is OK.
1110 // Convert to UInt32, checking for overflow.
1113 // Process character, turning it into a digit.
1114 if (c < '0' || c > '9')
1116 const unsigned d = c - '0';
1118 // Multiply by 10, checking for overflow out of 32 bits.
1119 if (i > 0xFFFFFFFFU / 10)
1123 // Add in the digit, checking for overflow out of 32 bits.
1124 const unsigned max = 0xFFFFFFFFU - d;
1129 // Handle end of string.
1136 // Get next character.
1141 int UString::find(const UString &f, int pos) const
1151 const UChar *end = data() + sz - fsz;
1152 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1153 const UChar *fdata = f.data();
1154 unsigned short fchar = fdata[0];
1156 for (const UChar *c = data() + pos; c <= end; c++)
1157 if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone))
1158 return static_cast<int>(c - data());
1163 int UString::find(UChar ch, int pos) const
1167 const UChar *end = data() + size();
1168 for (const UChar *c = data() + pos; c < end; c++)
1170 return static_cast<int>(c - data());
1175 int UString::rfind(const UString &f, int pos) const
1187 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1188 const UChar *fdata = f.data();
1189 for (const UChar *c = data() + pos; c >= data(); c--) {
1190 if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
1191 return static_cast<int>(c - data());
1197 int UString::rfind(UChar ch, int pos) const
1201 if (pos + 1 >= size())
1203 for (const UChar *c = data() + pos; c >= data(); c--) {
1205 return static_cast<int>(c-data());
1211 UString UString::substr(int pos, int len) const
1224 if (pos == 0 && len == s)
1227 return UString(Rep::create(m_rep, pos, len));
1230 bool operator==(const UString& s1, const UString& s2)
1232 if (s1.m_rep->len != s2.m_rep->len)
1235 return (memcmp(s1.m_rep->data(), s2.m_rep->data(),
1236 s1.m_rep->len * sizeof(UChar)) == 0);
1239 bool operator==(const UString& s1, const char *s2)
1242 return s1.isEmpty();
1245 const UChar *u = s1.data();
1246 const UChar *uend = u + s1.size();
1247 while (u != uend && *s2) {
1248 if (u[0] != (unsigned char)*s2)
1254 return u == uend && *s2 == 0;
1257 bool operator<(const UString& s1, const UString& s2)
1259 const int l1 = s1.size();
1260 const int l2 = s2.size();
1261 const int lmin = l1 < l2 ? l1 : l2;
1262 const UChar *c1 = s1.data();
1263 const UChar *c2 = s2.data();
1265 while (l < lmin && *c1 == *c2) {
1271 return (c1[0] < c2[0]);
1276 int compare(const UString& s1, const UString& s2)
1278 const int l1 = s1.size();
1279 const int l2 = s2.size();
1280 const int lmin = l1 < l2 ? l1 : l2;
1281 const UChar *c1 = s1.data();
1282 const UChar *c2 = s2.data();
1284 while (l < lmin && *c1 == *c2) {
1291 return (c1[0] > c2[0]) ? 1 : -1;
1296 return (l1 > l2) ? 1 : -1;
1299 CString UString::UTF8String(bool strict) const
1301 // Allocate a buffer big enough to hold all the characters.
1302 const int length = size();
1303 Vector<char, 1024> buffer(length * 3);
1305 // Convert to runs of 8-bit characters.
1306 char* p = buffer.data();
1307 const UChar* d = reinterpret_cast<const UChar*>(&data()[0]);
1308 ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
1309 if (result != conversionOK)
1312 return CString(buffer.data(), p - buffer.data());