]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/UString.cpp
54eac6a7228924eab3a5028d0d2587a0ce17a744
[apple/javascriptcore.git] / runtime / UString.cpp
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
5 * Copyright (C) 2009 Google Inc. All rights reserved.
6 *
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.
11 *
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.
16 *
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.
21 *
22 */
23
24 #include "config.h"
25 #include "UString.h"
26
27 #include "JSGlobalObjectFunctions.h"
28 #include "Collector.h"
29 #include "dtoa.h"
30 #include "Identifier.h"
31 #include "Operations.h"
32 #include <ctype.h>
33 #include <limits.h>
34 #include <limits>
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wtf/ASCIICType.h>
40 #include <wtf/Assertions.h>
41 #include <wtf/MathExtras.h>
42 #include <wtf/StringExtras.h>
43 #include <wtf/Vector.h>
44 #include <wtf/unicode/UTF8.h>
45 #include <wtf/StringExtras.h>
46
47 #if HAVE(STRINGS_H)
48 #include <strings.h>
49 #endif
50
51 using namespace WTF;
52 using namespace WTF::Unicode;
53 using namespace std;
54
55 namespace JSC {
56
57 extern const double NaN;
58 extern const double Inf;
59
60 CString::CString(const char* c)
61 : m_length(strlen(c))
62 , m_data(new char[m_length + 1])
63 {
64 memcpy(m_data, c, m_length + 1);
65 }
66
67 CString::CString(const char* c, size_t length)
68 : m_length(length)
69 , m_data(new char[length + 1])
70 {
71 memcpy(m_data, c, m_length);
72 m_data[m_length] = 0;
73 }
74
75 CString::CString(const CString& b)
76 {
77 m_length = b.m_length;
78 if (b.m_data) {
79 m_data = new char[m_length + 1];
80 memcpy(m_data, b.m_data, m_length + 1);
81 } else
82 m_data = 0;
83 }
84
85 CString::~CString()
86 {
87 delete [] m_data;
88 }
89
90 CString CString::adopt(char* c, size_t length)
91 {
92 CString s;
93 s.m_data = c;
94 s.m_length = length;
95 return s;
96 }
97
98 CString& CString::append(const CString& t)
99 {
100 char* n;
101 n = new char[m_length + t.m_length + 1];
102 if (m_length)
103 memcpy(n, m_data, m_length);
104 if (t.m_length)
105 memcpy(n + m_length, t.m_data, t.m_length);
106 m_length += t.m_length;
107 n[m_length] = 0;
108
109 delete [] m_data;
110 m_data = n;
111
112 return *this;
113 }
114
115 CString& CString::operator=(const char* c)
116 {
117 if (m_data)
118 delete [] m_data;
119 m_length = strlen(c);
120 m_data = new char[m_length + 1];
121 memcpy(m_data, c, m_length + 1);
122
123 return *this;
124 }
125
126 CString& CString::operator=(const CString& str)
127 {
128 if (this == &str)
129 return *this;
130
131 if (m_data)
132 delete [] m_data;
133 m_length = str.m_length;
134 if (str.m_data) {
135 m_data = new char[m_length + 1];
136 memcpy(m_data, str.m_data, m_length + 1);
137 } else
138 m_data = 0;
139
140 return *this;
141 }
142
143 bool operator==(const CString& c1, const CString& c2)
144 {
145 size_t len = c1.size();
146 return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0);
147 }
148
149 // These static strings are immutable, except for rc, whose initial value is chosen to
150 // reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
151 static UChar sharedEmptyChar;
152 UStringImpl* UStringImpl::s_null;
153 UStringImpl* UStringImpl::s_empty;
154 UString* UString::nullUString;
155
156 void initializeUString()
157 {
158 UStringImpl::s_null = new UStringImpl(0, 0, UStringImpl::ConstructStaticString);
159 UStringImpl::s_empty = new UStringImpl(&sharedEmptyChar, 0, UStringImpl::ConstructStaticString);
160 UString::nullUString = new UString;
161 }
162
163 static PassRefPtr<UString::Rep> createRep(const char* c)
164 {
165 if (!c)
166 return &UString::Rep::null();
167
168 if (!c[0])
169 return &UString::Rep::empty();
170
171 size_t length = strlen(c);
172 UChar* d;
173 PassRefPtr<UStringImpl> result = UStringImpl::tryCreateUninitialized(length, d);
174 if (!result)
175 return &UString::Rep::null();
176
177 for (size_t i = 0; i < length; i++)
178 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
179 return result;
180 }
181
182 static inline PassRefPtr<UString::Rep> createRep(const char* c, int length)
183 {
184 if (!c)
185 return &UString::Rep::null();
186
187 if (!length)
188 return &UString::Rep::empty();
189
190 UChar* d;
191 PassRefPtr<UStringImpl> result = UStringImpl::tryCreateUninitialized(length, d);
192 if (!result)
193 return &UString::Rep::null();
194
195 for (int i = 0; i < length; i++)
196 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
197 return result;
198 }
199
200 UString::UString(const char* c)
201 : m_rep(createRep(c))
202 {
203 }
204
205 UString::UString(const char* c, int length)
206 : m_rep(createRep(c, length))
207 {
208 }
209
210 UString::UString(const UChar* c, int length)
211 {
212 if (length == 0)
213 m_rep = &Rep::empty();
214 else
215 m_rep = Rep::create(c, length);
216 }
217
218 UString UString::createFromUTF8(const char* string)
219 {
220 if (!string)
221 return null();
222
223 size_t length = strlen(string);
224 Vector<UChar, 1024> buffer(length);
225 UChar* p = buffer.data();
226 if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
227 return null();
228
229 return UString(buffer.data(), p - buffer.data());
230 }
231
232 UString UString::from(int i)
233 {
234 UChar buf[1 + sizeof(i) * 3];
235 UChar* end = buf + sizeof(buf) / sizeof(UChar);
236 UChar* p = end;
237
238 if (i == 0)
239 *--p = '0';
240 else if (i == INT_MIN) {
241 char minBuf[1 + sizeof(i) * 3];
242 snprintf(minBuf, sizeof(minBuf), "%d", INT_MIN);
243 return UString(minBuf);
244 } else {
245 bool negative = false;
246 if (i < 0) {
247 negative = true;
248 i = -i;
249 }
250 while (i) {
251 *--p = static_cast<unsigned short>((i % 10) + '0');
252 i /= 10;
253 }
254 if (negative)
255 *--p = '-';
256 }
257
258 return UString(p, static_cast<int>(end - p));
259 }
260
261 UString UString::from(long long i)
262 {
263 UChar buf[1 + sizeof(i) * 3];
264 UChar* end = buf + sizeof(buf) / sizeof(UChar);
265 UChar* p = end;
266
267 if (i == 0)
268 *--p = '0';
269 else if (i == std::numeric_limits<long long>::min()) {
270 char minBuf[1 + sizeof(i) * 3];
271 #if OS(WINDOWS)
272 snprintf(minBuf, sizeof(minBuf) - 1, "%I64d", std::numeric_limits<long long>::min());
273 #elif PLATFORM(IPHONE)
274 snprintf(minBuf, sizeof(minBuf), "%lld", std::numeric_limits<long long>::min());
275 #else
276 snprintf(minBuf, sizeof(minBuf) - 1, "%lld", std::numeric_limits<long long>::min());
277 #endif
278 return UString(minBuf);
279 } else {
280 bool negative = false;
281 if (i < 0) {
282 negative = true;
283 i = -i;
284 }
285 while (i) {
286 *--p = static_cast<unsigned short>((i % 10) + '0');
287 i /= 10;
288 }
289 if (negative)
290 *--p = '-';
291 }
292
293 return UString(p, static_cast<int>(end - p));
294 }
295
296 UString UString::from(unsigned int u)
297 {
298 UChar buf[sizeof(u) * 3];
299 UChar* end = buf + sizeof(buf) / sizeof(UChar);
300 UChar* p = end;
301
302 if (u == 0)
303 *--p = '0';
304 else {
305 while (u) {
306 *--p = static_cast<unsigned short>((u % 10) + '0');
307 u /= 10;
308 }
309 }
310
311 return UString(p, static_cast<int>(end - p));
312 }
313
314 UString UString::from(long l)
315 {
316 UChar buf[1 + sizeof(l) * 3];
317 UChar* end = buf + sizeof(buf) / sizeof(UChar);
318 UChar* p = end;
319
320 if (l == 0)
321 *--p = '0';
322 else if (l == LONG_MIN) {
323 char minBuf[1 + sizeof(l) * 3];
324 snprintf(minBuf, sizeof(minBuf), "%ld", LONG_MIN);
325 return UString(minBuf);
326 } else {
327 bool negative = false;
328 if (l < 0) {
329 negative = true;
330 l = -l;
331 }
332 while (l) {
333 *--p = static_cast<unsigned short>((l % 10) + '0');
334 l /= 10;
335 }
336 if (negative)
337 *--p = '-';
338 }
339
340 return UString(p, static_cast<int>(end - p));
341 }
342
343 UString UString::from(double d)
344 {
345 DtoaBuffer buffer;
346 unsigned length;
347 doubleToStringInJavaScriptFormat(d, buffer, &length);
348 return UString(buffer, length);
349 }
350
351 UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
352 {
353 m_rep->checkConsistency();
354
355 if (rangeCount == 1 && separatorCount == 0) {
356 int thisSize = size();
357 int position = substringRanges[0].position;
358 int length = substringRanges[0].length;
359 if (position <= 0 && length >= thisSize)
360 return *this;
361 return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
362 }
363
364 int totalLength = 0;
365 for (int i = 0; i < rangeCount; i++)
366 totalLength += substringRanges[i].length;
367 for (int i = 0; i < separatorCount; i++)
368 totalLength += separators[i].size();
369
370 if (totalLength == 0)
371 return "";
372
373 UChar* buffer;
374 PassRefPtr<Rep> rep = Rep::tryCreateUninitialized(totalLength, buffer);
375 if (!rep)
376 return null();
377
378 int maxCount = max(rangeCount, separatorCount);
379 int bufferPos = 0;
380 for (int i = 0; i < maxCount; i++) {
381 if (i < rangeCount) {
382 UStringImpl::copyChars(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length);
383 bufferPos += substringRanges[i].length;
384 }
385 if (i < separatorCount) {
386 UStringImpl::copyChars(buffer + bufferPos, separators[i].data(), separators[i].size());
387 bufferPos += separators[i].size();
388 }
389 }
390
391 return rep;
392 }
393
394 UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
395 {
396 m_rep->checkConsistency();
397
398 int replacementLength = replacement.size();
399 int totalLength = size() - rangeLength + replacementLength;
400 if (totalLength == 0)
401 return "";
402
403 UChar* buffer;
404 PassRefPtr<Rep> rep = Rep::tryCreateUninitialized(totalLength, buffer);
405 if (!rep)
406 return null();
407
408 UStringImpl::copyChars(buffer, data(), rangeStart);
409 UStringImpl::copyChars(buffer + rangeStart, replacement.data(), replacementLength);
410 int rangeEnd = rangeStart + rangeLength;
411 UStringImpl::copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);
412
413 return rep;
414 }
415
416 bool UString::getCString(CStringBuffer& buffer) const
417 {
418 int length = size();
419 int neededSize = length + 1;
420 buffer.resize(neededSize);
421 char* buf = buffer.data();
422
423 UChar ored = 0;
424 const UChar* p = data();
425 char* q = buf;
426 const UChar* limit = p + length;
427 while (p != limit) {
428 UChar c = p[0];
429 ored |= c;
430 *q = static_cast<char>(c);
431 ++p;
432 ++q;
433 }
434 *q = '\0';
435
436 return !(ored & 0xFF00);
437 }
438
439 char* UString::ascii() const
440 {
441 static char* asciiBuffer = 0;
442
443 int length = size();
444 int neededSize = length + 1;
445 delete[] asciiBuffer;
446 asciiBuffer = new char[neededSize];
447
448 const UChar* p = data();
449 char* q = asciiBuffer;
450 const UChar* limit = p + length;
451 while (p != limit) {
452 *q = static_cast<char>(p[0]);
453 ++p;
454 ++q;
455 }
456 *q = '\0';
457
458 return asciiBuffer;
459 }
460
461 UString& UString::operator=(const char* c)
462 {
463 if (!c) {
464 m_rep = &Rep::null();
465 return *this;
466 }
467
468 if (!c[0]) {
469 m_rep = &Rep::empty();
470 return *this;
471 }
472
473 int l = static_cast<int>(strlen(c));
474 UChar* d = 0;
475 m_rep = Rep::tryCreateUninitialized(l, d);
476 if (m_rep) {
477 for (int i = 0; i < l; i++)
478 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
479 } else
480 makeNull();
481
482 return *this;
483 }
484
485 bool UString::is8Bit() const
486 {
487 const UChar* u = data();
488 const UChar* limit = u + size();
489 while (u < limit) {
490 if (u[0] > 0xFF)
491 return false;
492 ++u;
493 }
494
495 return true;
496 }
497
498 UChar UString::operator[](int pos) const
499 {
500 if (pos >= size())
501 return '\0';
502 return data()[pos];
503 }
504
505 double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
506 {
507 if (size() == 1) {
508 UChar c = data()[0];
509 if (isASCIIDigit(c))
510 return c - '0';
511 if (isASCIISpace(c) && tolerateEmptyString)
512 return 0;
513 return NaN;
514 }
515
516 // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
517 // after the number, so this is too strict a check.
518 CStringBuffer s;
519 if (!getCString(s))
520 return NaN;
521 const char* c = s.data();
522
523 // skip leading white space
524 while (isASCIISpace(*c))
525 c++;
526
527 // empty string ?
528 if (*c == '\0')
529 return tolerateEmptyString ? 0.0 : NaN;
530
531 double d;
532
533 // hex number ?
534 if (*c == '0' && (*(c + 1) == 'x' || *(c + 1) == 'X')) {
535 const char* firstDigitPosition = c + 2;
536 c++;
537 d = 0.0;
538 while (*(++c)) {
539 if (*c >= '0' && *c <= '9')
540 d = d * 16.0 + *c - '0';
541 else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
542 d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
543 else
544 break;
545 }
546
547 if (d >= mantissaOverflowLowerBound)
548 d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
549 } else {
550 // regular number ?
551 char* end;
552 d = WTF::strtod(c, &end);
553 if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
554 c = end;
555 } else {
556 double sign = 1.0;
557
558 if (*c == '+')
559 c++;
560 else if (*c == '-') {
561 sign = -1.0;
562 c++;
563 }
564
565 // We used strtod() to do the conversion. However, strtod() handles
566 // infinite values slightly differently than JavaScript in that it
567 // converts the string "inf" with any capitalization to infinity,
568 // whereas the ECMA spec requires that it be converted to NaN.
569
570 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') {
571 d = sign * Inf;
572 c += 8;
573 } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
574 c = end;
575 else
576 return NaN;
577 }
578 }
579
580 // allow trailing white space
581 while (isASCIISpace(*c))
582 c++;
583 // don't allow anything after - unless tolerant=true
584 if (!tolerateTrailingJunk && *c != '\0')
585 d = NaN;
586
587 return d;
588 }
589
590 double UString::toDouble(bool tolerateTrailingJunk) const
591 {
592 return toDouble(tolerateTrailingJunk, true);
593 }
594
595 double UString::toDouble() const
596 {
597 return toDouble(false, true);
598 }
599
600 uint32_t UString::toUInt32(bool* ok) const
601 {
602 double d = toDouble();
603 bool b = true;
604
605 if (d != static_cast<uint32_t>(d)) {
606 b = false;
607 d = 0;
608 }
609
610 if (ok)
611 *ok = b;
612
613 return static_cast<uint32_t>(d);
614 }
615
616 uint32_t UString::toUInt32(bool* ok, bool tolerateEmptyString) const
617 {
618 double d = toDouble(false, tolerateEmptyString);
619 bool b = true;
620
621 if (d != static_cast<uint32_t>(d)) {
622 b = false;
623 d = 0;
624 }
625
626 if (ok)
627 *ok = b;
628
629 return static_cast<uint32_t>(d);
630 }
631
632 uint32_t UString::toStrictUInt32(bool* ok) const
633 {
634 if (ok)
635 *ok = false;
636
637 // Empty string is not OK.
638 int len = m_rep->size();
639 if (len == 0)
640 return 0;
641 const UChar* p = m_rep->data();
642 unsigned short c = p[0];
643
644 // If the first digit is 0, only 0 itself is OK.
645 if (c == '0') {
646 if (len == 1 && ok)
647 *ok = true;
648 return 0;
649 }
650
651 // Convert to UInt32, checking for overflow.
652 uint32_t i = 0;
653 while (1) {
654 // Process character, turning it into a digit.
655 if (c < '0' || c > '9')
656 return 0;
657 const unsigned d = c - '0';
658
659 // Multiply by 10, checking for overflow out of 32 bits.
660 if (i > 0xFFFFFFFFU / 10)
661 return 0;
662 i *= 10;
663
664 // Add in the digit, checking for overflow out of 32 bits.
665 const unsigned max = 0xFFFFFFFFU - d;
666 if (i > max)
667 return 0;
668 i += d;
669
670 // Handle end of string.
671 if (--len == 0) {
672 if (ok)
673 *ok = true;
674 return i;
675 }
676
677 // Get next character.
678 c = *(++p);
679 }
680 }
681
682 int UString::find(const UString& f, int pos) const
683 {
684 int fsz = f.size();
685
686 if (pos < 0)
687 pos = 0;
688
689 if (fsz == 1) {
690 UChar ch = f[0];
691 const UChar* end = data() + size();
692 for (const UChar* c = data() + pos; c < end; c++) {
693 if (*c == ch)
694 return static_cast<int>(c - data());
695 }
696 return -1;
697 }
698
699 int sz = size();
700 if (sz < fsz)
701 return -1;
702 if (fsz == 0)
703 return pos;
704 const UChar* end = data() + sz - fsz;
705 int fsizeminusone = (fsz - 1) * sizeof(UChar);
706 const UChar* fdata = f.data();
707 unsigned short fchar = fdata[0];
708 ++fdata;
709 for (const UChar* c = data() + pos; c <= end; c++) {
710 if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone))
711 return static_cast<int>(c - data());
712 }
713
714 return -1;
715 }
716
717 int UString::find(UChar ch, int pos) const
718 {
719 if (pos < 0)
720 pos = 0;
721 const UChar* end = data() + size();
722 for (const UChar* c = data() + pos; c < end; c++) {
723 if (*c == ch)
724 return static_cast<int>(c - data());
725 }
726
727 return -1;
728 }
729
730 int UString::rfind(const UString& f, int pos) const
731 {
732 int sz = size();
733 int fsz = f.size();
734 if (sz < fsz)
735 return -1;
736 if (pos < 0)
737 pos = 0;
738 if (pos > sz - fsz)
739 pos = sz - fsz;
740 if (fsz == 0)
741 return pos;
742 int fsizeminusone = (fsz - 1) * sizeof(UChar);
743 const UChar* fdata = f.data();
744 for (const UChar* c = data() + pos; c >= data(); c--) {
745 if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
746 return static_cast<int>(c - data());
747 }
748
749 return -1;
750 }
751
752 int UString::rfind(UChar ch, int pos) const
753 {
754 if (isEmpty())
755 return -1;
756 if (pos + 1 >= size())
757 pos = size() - 1;
758 for (const UChar* c = data() + pos; c >= data(); c--) {
759 if (*c == ch)
760 return static_cast<int>(c - data());
761 }
762
763 return -1;
764 }
765
766 UString UString::substr(int pos, int len) const
767 {
768 int s = size();
769
770 if (pos < 0)
771 pos = 0;
772 else if (pos >= s)
773 pos = s;
774 if (len < 0)
775 len = s;
776 if (pos + len >= s)
777 len = s - pos;
778
779 if (pos == 0 && len == s)
780 return *this;
781
782 return UString(Rep::create(m_rep, pos, len));
783 }
784
785 bool operator==(const UString& s1, const char *s2)
786 {
787 if (s2 == 0)
788 return s1.isEmpty();
789
790 const UChar* u = s1.data();
791 const UChar* uend = u + s1.size();
792 while (u != uend && *s2) {
793 if (u[0] != (unsigned char)*s2)
794 return false;
795 s2++;
796 u++;
797 }
798
799 return u == uend && *s2 == 0;
800 }
801
802 bool operator<(const UString& s1, const UString& s2)
803 {
804 const int l1 = s1.size();
805 const int l2 = s2.size();
806 const int lmin = l1 < l2 ? l1 : l2;
807 const UChar* c1 = s1.data();
808 const UChar* c2 = s2.data();
809 int l = 0;
810 while (l < lmin && *c1 == *c2) {
811 c1++;
812 c2++;
813 l++;
814 }
815 if (l < lmin)
816 return (c1[0] < c2[0]);
817
818 return (l1 < l2);
819 }
820
821 bool operator>(const UString& s1, const UString& s2)
822 {
823 const int l1 = s1.size();
824 const int l2 = s2.size();
825 const int lmin = l1 < l2 ? l1 : l2;
826 const UChar* c1 = s1.data();
827 const UChar* c2 = s2.data();
828 int l = 0;
829 while (l < lmin && *c1 == *c2) {
830 c1++;
831 c2++;
832 l++;
833 }
834 if (l < lmin)
835 return (c1[0] > c2[0]);
836
837 return (l1 > l2);
838 }
839
840 int compare(const UString& s1, const UString& s2)
841 {
842 const int l1 = s1.size();
843 const int l2 = s2.size();
844 const int lmin = l1 < l2 ? l1 : l2;
845 const UChar* c1 = s1.data();
846 const UChar* c2 = s2.data();
847 int l = 0;
848 while (l < lmin && *c1 == *c2) {
849 c1++;
850 c2++;
851 l++;
852 }
853
854 if (l < lmin)
855 return (c1[0] > c2[0]) ? 1 : -1;
856
857 if (l1 == l2)
858 return 0;
859
860 return (l1 > l2) ? 1 : -1;
861 }
862
863 bool equal(const UString::Rep* r, const UString::Rep* b)
864 {
865 int length = r->size();
866 if (length != b->size())
867 return false;
868 const UChar* d = r->data();
869 const UChar* s = b->data();
870 for (int i = 0; i != length; ++i) {
871 if (d[i] != s[i])
872 return false;
873 }
874 return true;
875 }
876
877 CString UString::UTF8String(bool strict) const
878 {
879 // Allocate a buffer big enough to hold all the characters.
880 const int length = size();
881 Vector<char, 1024> buffer(length * 3);
882
883 // Convert to runs of 8-bit characters.
884 char* p = buffer.data();
885 const UChar* d = reinterpret_cast<const UChar*>(&data()[0]);
886 ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
887 if (result != conversionOK)
888 return CString();
889
890 return CString(buffer.data(), p - buffer.data());
891 }
892
893 // For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
894 NEVER_INLINE void UString::makeNull()
895 {
896 m_rep = &Rep::null();
897 }
898
899 // For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
900 NEVER_INLINE UString::Rep* UString::nullRep()
901 {
902 return &Rep::null();
903 }
904
905 } // namespace JSC