source: webkit/trunk/JavaScriptCore/runtime/UString.cpp@ 52346

Last change on this file since 52346 was 52346, checked in by [email protected], 15 years ago

Add createNonCopying method to UString to make replace constructor passed bool,
to make behaviour more explicit. Add createFromUTF8 to UString (wrapping method
on UString::Rep), since other cases of transliteration (e.g. from ascii) are
performed in UString constructors. Add/use setHash & size() accessors on Rep,
rather than accessing _hash/len directly.

Reviewed by Sam Weinig.

  • API/JSClassRef.cpp:

(OpaqueJSClass::OpaqueJSClass):

  • API/OpaqueJSString.cpp:

(OpaqueJSString::ustring):

(JSC::arrayProtoFuncToString):

  • runtime/Identifier.cpp:

(JSC::Identifier::equal):
(JSC::CStringTranslator::translate):
(JSC::UCharBufferTranslator::translate):
(JSC::Identifier::addSlowCase):

  • runtime/JSString.cpp:

(JSC::JSString::resolveRope):

  • runtime/JSString.h:

(JSC::JSString::Rope::Fiber::refAndGetLength):
(JSC::JSString::Rope::append):

  • runtime/StringBuilder.h:

(JSC::StringBuilder::release):

  • runtime/StringConstructor.cpp:

(JSC::stringFromCharCodeSlowCase):

  • runtime/StringPrototype.cpp:

(JSC::substituteBackreferencesSlow):
(JSC::stringProtoFuncToLowerCase):
(JSC::stringProtoFuncToUpperCase):
(JSC::stringProtoFuncFontsize):
(JSC::stringProtoFuncLink):

  • runtime/UString.cpp:

(JSC::UString::UString):
(JSC::UString::createNonCopying):
(JSC::UString::createFromUTF8):

  • runtime/UString.h:

(JSC::UString::Rep::setHash):
(JSC::UString::~UString):
(JSC::makeString):

  • Property svn:eol-style set to native
File size: 48.2 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Cameron Zwarich ([email protected])
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
51using namespace WTF;
52using namespace WTF::Unicode;
53using namespace std;
54
55// This can be tuned differently per platform by putting platform #ifs right here.
56// If you don't define this macro at all, then copyChars will just call directly
57// to memcpy.
58#define USTRING_COPY_CHARS_INLINE_CUTOFF 20
59
60namespace JSC {
61
62extern const double NaN;
63extern const double Inf;
64
65// This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
66static const int minLengthToShare = 10;
67
68static inline size_t overflowIndicator() { return std::numeric_limits<size_t>::max(); }
69static inline size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); }
70
71static inline PossiblyNull<UChar*> allocChars(size_t length)
72{
73 ASSERT(length);
74 if (length > maxUChars())
75 return 0;
76 return tryFastMalloc(sizeof(UChar) * length);
77}
78
79static inline PossiblyNull<UChar*> reallocChars(UChar* buffer, size_t length)
80{
81 ASSERT(length);
82 if (length > maxUChars())
83 return 0;
84 return tryFastRealloc(buffer, sizeof(UChar) * length);
85}
86
87static inline void copyChars(UChar* destination, const UChar* source, unsigned numCharacters)
88{
89#ifdef USTRING_COPY_CHARS_INLINE_CUTOFF
90 if (numCharacters <= USTRING_COPY_CHARS_INLINE_CUTOFF) {
91 for (unsigned i = 0; i < numCharacters; ++i)
92 destination[i] = source[i];
93 return;
94 }
95#endif
96 memcpy(destination, source, numCharacters * sizeof(UChar));
97}
98
99COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes);
100
101CString::CString(const char* c)
102 : m_length(strlen(c))
103 , m_data(new char[m_length + 1])
104{
105 memcpy(m_data, c, m_length + 1);
106}
107
108CString::CString(const char* c, size_t length)
109 : m_length(length)
110 , m_data(new char[length + 1])
111{
112 memcpy(m_data, c, m_length);
113 m_data[m_length] = 0;
114}
115
116CString::CString(const CString& b)
117{
118 m_length = b.m_length;
119 if (b.m_data) {
120 m_data = new char[m_length + 1];
121 memcpy(m_data, b.m_data, m_length + 1);
122 } else
123 m_data = 0;
124}
125
126CString::~CString()
127{
128 delete [] m_data;
129}
130
131CString CString::adopt(char* c, size_t length)
132{
133 CString s;
134 s.m_data = c;
135 s.m_length = length;
136 return s;
137}
138
139CString& CString::append(const CString& t)
140{
141 char* n;
142 n = new char[m_length + t.m_length + 1];
143 if (m_length)
144 memcpy(n, m_data, m_length);
145 if (t.m_length)
146 memcpy(n + m_length, t.m_data, t.m_length);
147 m_length += t.m_length;
148 n[m_length] = 0;
149
150 delete [] m_data;
151 m_data = n;
152
153 return *this;
154}
155
156CString& CString::operator=(const char* c)
157{
158 if (m_data)
159 delete [] m_data;
160 m_length = strlen(c);
161 m_data = new char[m_length + 1];
162 memcpy(m_data, c, m_length + 1);
163
164 return *this;
165}
166
167CString& CString::operator=(const CString& str)
168{
169 if (this == &str)
170 return *this;
171
172 if (m_data)
173 delete [] m_data;
174 m_length = str.m_length;
175 if (str.m_data) {
176 m_data = new char[m_length + 1];
177 memcpy(m_data, str.m_data, m_length + 1);
178 } else
179 m_data = 0;
180
181 return *this;
182}
183
184bool operator==(const CString& c1, const CString& c2)
185{
186 size_t len = c1.size();
187 return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0);
188}
189
190// These static strings are immutable, except for rc, whose initial value is chosen to
191// reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
192static UChar sharedEmptyChar;
193UString::BaseString* UString::Rep::nullBaseString;
194UString::BaseString* UString::Rep::emptyBaseString;
195UString* UString::nullUString;
196
197static void initializeStaticBaseString(UString::BaseString& base)
198{
199 base.rc = INT_MAX / 2;
200 base.m_identifierTableAndFlags.setFlag(UString::Rep::StaticFlag);
201 base.checkConsistency();
202}
203
204void initializeUString()
205{
206 UString::Rep::nullBaseString = new UString::BaseString(0, 0);
207 initializeStaticBaseString(*UString::Rep::nullBaseString);
208
209 UString::Rep::emptyBaseString = new UString::BaseString(&sharedEmptyChar, 0);
210 initializeStaticBaseString(*UString::Rep::emptyBaseString);
211
212 UString::nullUString = new UString;
213}
214
215static char* statBuffer = 0; // Only used for debugging via UString::ascii().
216
217PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar* d, int l)
218{
219 UChar* copyD = static_cast<UChar*>(fastMalloc(l * sizeof(UChar)));
220 copyChars(copyD, d, l);
221 return create(copyD, l);
222}
223
224PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string)
225{
226 if (!string)
227 return &UString::Rep::null();
228
229 size_t length = strlen(string);
230 Vector<UChar, 1024> buffer(length);
231 UChar* p = buffer.data();
232 if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
233 return &UString::Rep::null();
234
235 return UString::Rep::createCopying(buffer.data(), p - buffer.data());
236}
237
238PassRefPtr<UString::Rep> UString::Rep::create(UChar* string, int length, PassRefPtr<UString::SharedUChar> sharedBuffer)
239{
240 PassRefPtr<UString::Rep> rep = create(string, length);
241 rep->baseString()->setSharedBuffer(sharedBuffer);
242 rep->checkConsistency();
243 return rep;
244}
245
246UString::SharedUChar* UString::Rep::sharedBuffer()
247{
248 UString::BaseString* base = baseString();
249 if (len < minLengthToShare)
250 return 0;
251
252 return base->sharedBuffer();
253}
254
255void UString::Rep::destroy()
256{
257 checkConsistency();
258
259 // Static null and empty strings can never be destroyed, but we cannot rely on
260 // reference counting, because ref/deref are not thread-safe.
261 if (!isStatic()) {
262 if (identifierTable())
263 Identifier::remove(this);
264
265 UString::BaseString* base = baseString();
266 if (base == this) {
267 if (m_sharedBuffer)
268 m_sharedBuffer->deref();
269 else
270 fastFree(base->buf);
271 } else
272 base->deref();
273
274 delete this;
275 }
276}
277
278// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
279// or anything like that.
280const unsigned PHI = 0x9e3779b9U;
281
282// Paul Hsieh's SuperFastHash
283// http://www.azillionmonkeys.com/qed/hash.html
284unsigned UString::Rep::computeHash(const UChar* s, int len)
285{
286 unsigned l = len;
287 uint32_t hash = PHI;
288 uint32_t tmp;
289
290 int rem = l & 1;
291 l >>= 1;
292
293 // Main loop
294 for (; l > 0; l--) {
295 hash += s[0];
296 tmp = (s[1] << 11) ^ hash;
297 hash = (hash << 16) ^ tmp;
298 s += 2;
299 hash += hash >> 11;
300 }
301
302 // Handle end case
303 if (rem) {
304 hash += s[0];
305 hash ^= hash << 11;
306 hash += hash >> 17;
307 }
308
309 // Force "avalanching" of final 127 bits
310 hash ^= hash << 3;
311 hash += hash >> 5;
312 hash ^= hash << 2;
313 hash += hash >> 15;
314 hash ^= hash << 10;
315
316 // this avoids ever returning a hash code of 0, since that is used to
317 // signal "hash not computed yet", using a value that is likely to be
318 // effectively the same as 0 when the low bits are masked
319 if (hash == 0)
320 hash = 0x80000000;
321
322 return hash;
323}
324
325// Paul Hsieh's SuperFastHash
326// http://www.azillionmonkeys.com/qed/hash.html
327unsigned UString::Rep::computeHash(const char* s, int l)
328{
329 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
330 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
331 // were 16-bit chunks, which should give matching results
332
333 uint32_t hash = PHI;
334 uint32_t tmp;
335
336 size_t rem = l & 1;
337 l >>= 1;
338
339 // Main loop
340 for (; l > 0; l--) {
341 hash += static_cast<unsigned char>(s[0]);
342 tmp = (static_cast<unsigned char>(s[1]) << 11) ^ hash;
343 hash = (hash << 16) ^ tmp;
344 s += 2;
345 hash += hash >> 11;
346 }
347
348 // Handle end case
349 if (rem) {
350 hash += static_cast<unsigned char>(s[0]);
351 hash ^= hash << 11;
352 hash += hash >> 17;
353 }
354
355 // Force "avalanching" of final 127 bits
356 hash ^= hash << 3;
357 hash += hash >> 5;
358 hash ^= hash << 2;
359 hash += hash >> 15;
360 hash ^= hash << 10;
361
362 // this avoids ever returning a hash code of 0, since that is used to
363 // signal "hash not computed yet", using a value that is likely to be
364 // effectively the same as 0 when the low bits are masked
365 if (hash == 0)
366 hash = 0x80000000;
367
368 return hash;
369}
370
371#ifndef NDEBUG
372void UString::Rep::checkConsistency() const
373{
374 const UString::BaseString* base = baseString();
375
376 // There is no recursion for base strings.
377 ASSERT(base == base->baseString());
378
379 if (isStatic()) {
380 // There are only two static strings: null and empty.
381 ASSERT(!len);
382
383 // Static strings cannot get in identifier tables, because they are globally shared.
384 ASSERT(!identifierTable());
385 }
386
387 // The string fits in buffer.
388 ASSERT(base->usedPreCapacity <= base->preCapacity);
389 ASSERT(base->usedCapacity <= base->capacity);
390 ASSERT(-offset <= base->usedPreCapacity);
391 ASSERT(offset + len <= base->usedCapacity);
392}
393#endif
394
395UString::SharedUChar* UString::BaseString::sharedBuffer()
396{
397 if (!m_sharedBuffer)
398 setSharedBuffer(SharedUChar::create(new OwnFastMallocPtr<UChar>(buf)));
399 return m_sharedBuffer;
400}
401
402void UString::BaseString::setSharedBuffer(PassRefPtr<UString::SharedUChar> sharedBuffer)
403{
404 // The manual steps below are because m_sharedBuffer can't be a RefPtr. m_sharedBuffer
405 // is in a union with another variable to avoid making BaseString any larger.
406 if (m_sharedBuffer)
407 m_sharedBuffer->deref();
408 m_sharedBuffer = sharedBuffer.releaseRef();
409}
410
411bool UString::BaseString::slowIsBufferReadOnly()
412{
413 // The buffer may not be modified as soon as the underlying data has been shared with another class.
414 if (m_sharedBuffer->isShared())
415 return true;
416
417 // At this point, we know it that the underlying buffer isn't shared outside of this base class,
418 // so get rid of m_sharedBuffer.
419 OwnPtr<OwnFastMallocPtr<UChar> > mallocPtr(m_sharedBuffer->release());
420 UChar* unsharedBuf = const_cast<UChar*>(mallocPtr->release());
421 setSharedBuffer(0);
422 preCapacity += (buf - unsharedBuf);
423 buf = unsharedBuf;
424 return false;
425}
426
427// Put these early so they can be inlined.
428static inline size_t expandedSize(size_t capacitySize, size_t precapacitySize)
429{
430 // Combine capacitySize & precapacitySize to produce a single size to allocate,
431 // check that doing so does not result in overflow.
432 size_t size = capacitySize + precapacitySize;
433 if (size < capacitySize)
434 return overflowIndicator();
435
436 // Small Strings (up to 4 pages):
437 // Expand the allocation size to 112.5% of the amount requested. This is largely sicking
438 // to our previous policy, however 112.5% is cheaper to calculate.
439 if (size < 0x4000) {
440 size_t expandedSize = ((size + (size >> 3)) | 15) + 1;
441 // Given the limited range within which we calculate the expansion in this
442 // fashion the above calculation should never overflow.
443 ASSERT(expandedSize >= size);
444 ASSERT(expandedSize < maxUChars());
445 return expandedSize;
446 }
447
448 // Medium Strings (up to 128 pages):
449 // For pages covering multiple pages over-allocation is less of a concern - any unused
450 // space will not be paged in if it is not used, so this is purely a VM overhead. For
451 // these strings allocate 2x the requested size.
452 if (size < 0x80000) {
453 size_t expandedSize = ((size + size) | 0xfff) + 1;
454 // Given the limited range within which we calculate the expansion in this
455 // fashion the above calculation should never overflow.
456 ASSERT(expandedSize >= size);
457 ASSERT(expandedSize < maxUChars());
458 return expandedSize;
459 }
460
461 // Large Strings (to infinity and beyond!):
462 // Revert to our 112.5% policy - probably best to limit the amount of unused VM we allow
463 // any individual string be responsible for.
464 size_t expandedSize = ((size + (size >> 3)) | 0xfff) + 1;
465
466 // Check for overflow - any result that is at least as large as requested (but
467 // still below the limit) is okay.
468 if ((expandedSize >= size) && (expandedSize < maxUChars()))
469 return expandedSize;
470 return overflowIndicator();
471}
472
473static inline bool expandCapacity(UString::Rep* rep, int requiredLength)
474{
475 rep->checkConsistency();
476 ASSERT(!rep->baseString()->isBufferReadOnly());
477
478 UString::BaseString* base = rep->baseString();
479
480 if (requiredLength > base->capacity) {
481 size_t newCapacity = expandedSize(requiredLength, base->preCapacity);
482 UChar* oldBuf = base->buf;
483 if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) {
484 base->buf = oldBuf;
485 return false;
486 }
487 base->capacity = newCapacity - base->preCapacity;
488 }
489 if (requiredLength > base->usedCapacity)
490 base->usedCapacity = requiredLength;
491
492 rep->checkConsistency();
493 return true;
494}
495
496bool UString::Rep::reserveCapacity(int capacity)
497{
498 // If this is an empty string there is no point 'growing' it - just allocate a new one.
499 // If the BaseString is shared with another string that is using more capacity than this
500 // string is, then growing the buffer won't help.
501 // If the BaseString's buffer is readonly, then it isn't allowed to grow.
502 UString::BaseString* base = baseString();
503 if (!base->buf || !base->capacity || (offset + len) != base->usedCapacity || base->isBufferReadOnly())
504 return false;
505
506 // If there is already sufficient capacity, no need to grow!
507 if (capacity <= base->capacity)
508 return true;
509
510 checkConsistency();
511
512 size_t newCapacity = expandedSize(capacity, base->preCapacity);
513 UChar* oldBuf = base->buf;
514 if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) {
515 base->buf = oldBuf;
516 return false;
517 }
518 base->capacity = newCapacity - base->preCapacity;
519
520 checkConsistency();
521 return true;
522}
523
524void UString::expandCapacity(int requiredLength)
525{
526 if (!JSC::expandCapacity(m_rep.get(), requiredLength))
527 makeNull();
528}
529
530void UString::expandPreCapacity(int requiredPreCap)
531{
532 m_rep->checkConsistency();
533 ASSERT(!m_rep->baseString()->isBufferReadOnly());
534
535 BaseString* base = m_rep->baseString();
536
537 if (requiredPreCap > base->preCapacity) {
538 size_t newCapacity = expandedSize(requiredPreCap, base->capacity);
539 int delta = newCapacity - base->capacity - base->preCapacity;
540
541 UChar* newBuf;
542 if (!allocChars(newCapacity).getValue(newBuf)) {
543 makeNull();
544 return;
545 }
546 copyChars(newBuf + delta, base->buf, base->capacity + base->preCapacity);
547 fastFree(base->buf);
548 base->buf = newBuf;
549
550 base->preCapacity = newCapacity - base->capacity;
551 }
552 if (requiredPreCap > base->usedPreCapacity)
553 base->usedPreCapacity = requiredPreCap;
554
555 m_rep->checkConsistency();
556}
557
558static PassRefPtr<UString::Rep> createRep(const char* c)
559{
560 if (!c)
561 return &UString::Rep::null();
562
563 if (!c[0])
564 return &UString::Rep::empty();
565
566 size_t length = strlen(c);
567 UChar* d;
568 if (!allocChars(length).getValue(d))
569 return &UString::Rep::null();
570 else {
571 for (size_t i = 0; i < length; i++)
572 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
573 return UString::Rep::create(d, static_cast<int>(length));
574 }
575
576}
577
578static inline PassRefPtr<UString::Rep> createRep(const char* c, int length)
579{
580 if (!c)
581 return &UString::Rep::null();
582
583 if (!length)
584 return &UString::Rep::empty();
585
586 UChar* d;
587 if (!allocChars(length).getValue(d))
588 return &UString::Rep::null();
589
590 for (int i = 0; i < length; i++)
591 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
592 return UString::Rep::create(d, length);
593}
594
595UString::UString(const char* c)
596 : m_rep(createRep(c))
597{
598}
599
600UString::UString(const char* c, int length)
601 : m_rep(createRep(c, length))
602{
603}
604
605UString::UString(const UChar* c, int length)
606{
607 if (length == 0)
608 m_rep = &Rep::empty();
609 else
610 m_rep = Rep::createCopying(c, length);
611}
612
613UString::UString(const Vector<UChar>& buffer)
614{
615 if (!buffer.size())
616 m_rep = &Rep::empty();
617 else
618 m_rep = Rep::createCopying(buffer.data(), buffer.size());
619}
620
621UString UString::createNonCopying(UChar* c, int length)
622{
623 if (length == 0)
624 return UString(&Rep::empty());
625 else
626 return Rep::create(c, length);
627}
628
629UString UString::createFromUTF8(const char* string)
630{
631 return Rep::createFromUTF8(string);
632}
633
634static ALWAYS_INLINE int newCapacityWithOverflowCheck(const int currentCapacity, const int extendLength, const bool plusOne = false)
635{
636 ASSERT_WITH_MESSAGE(extendLength >= 0, "extendedLength = %d", extendLength);
637
638 const int plusLength = plusOne ? 1 : 0;
639 if (currentCapacity > std::numeric_limits<int>::max() - extendLength - plusLength)
640 CRASH();
641
642 return currentCapacity + extendLength + plusLength;
643}
644
645static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const UChar* tData, int tSize)
646{
647 RefPtr<UString::Rep> rep = r;
648
649 rep->checkConsistency();
650
651 int thisSize = rep->size();
652 int thisOffset = rep->offset;
653 int length = thisSize + tSize;
654 UString::BaseString* base = rep->baseString();
655
656 // possible cases:
657 if (tSize == 0) {
658 // t is empty
659 } else if (thisSize == 0) {
660 // this is empty
661 rep = UString::Rep::createCopying(tData, tSize);
662 } else if (rep == base && !base->isShared()) {
663 // this is direct and has refcount of 1 (so we can just alter it directly)
664 if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
665 rep = &UString::Rep::null();
666 if (rep->data()) {
667 copyChars(rep->data() + thisSize, tData, tSize);
668 rep->len = length;
669 rep->_hash = 0;
670 }
671 } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
672 // this reaches the end of the buffer - extend it if it's long enough to append to
673 if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
674 rep = &UString::Rep::null();
675 if (rep->data()) {
676 copyChars(rep->data() + thisSize, tData, tSize);
677 rep = UString::Rep::create(rep, 0, length);
678 }
679 } else {
680 // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
681 size_t newCapacity = expandedSize(length, 0);
682 UChar* d;
683 if (!allocChars(newCapacity).getValue(d))
684 rep = &UString::Rep::null();
685 else {
686 copyChars(d, rep->data(), thisSize);
687 copyChars(d + thisSize, tData, tSize);
688 rep = UString::Rep::create(d, length);
689 rep->baseString()->capacity = newCapacity;
690 }
691 }
692
693 rep->checkConsistency();
694
695 return rep.release();
696}
697
698static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const char* t)
699{
700 RefPtr<UString::Rep> rep = r;
701
702 rep->checkConsistency();
703
704 int thisSize = rep->size();
705 int thisOffset = rep->offset;
706 int tSize = static_cast<int>(strlen(t));
707 int length = thisSize + tSize;
708 UString::BaseString* base = rep->baseString();
709
710 // possible cases:
711 if (thisSize == 0) {
712 // this is empty
713 rep = createRep(t);
714 } else if (tSize == 0) {
715 // t is empty, we'll just return *this below.
716 } else if (rep == base && !base->isShared()) {
717 // this is direct and has refcount of 1 (so we can just alter it directly)
718 expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
719 UChar* d = rep->data();
720 if (d) {
721 for (int i = 0; i < tSize; ++i)
722 d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
723 rep->len = length;
724 rep->_hash = 0;
725 }
726 } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
727 // this string reaches the end of the buffer - extend it
728 expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
729 UChar* d = rep->data();
730 if (d) {
731 for (int i = 0; i < tSize; ++i)
732 d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
733 rep = UString::Rep::create(rep, 0, length);
734 }
735 } else {
736 // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
737 size_t newCapacity = expandedSize(length, 0);
738 UChar* d;
739 if (!allocChars(newCapacity).getValue(d))
740 rep = &UString::Rep::null();
741 else {
742 copyChars(d, rep->data(), thisSize);
743 for (int i = 0; i < tSize; ++i)
744 d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
745 rep = UString::Rep::create(d, length);
746 rep->baseString()->capacity = newCapacity;
747 }
748 }
749
750 rep->checkConsistency();
751
752 return rep.release();
753}
754
755PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
756{
757 a->checkConsistency();
758 b->checkConsistency();
759
760 int aSize = a->size();
761 int bSize = b->size();
762 int aOffset = a->offset;
763
764 // possible cases:
765
766 UString::BaseString* aBase = a->baseString();
767 if (bSize == 1 && aOffset + aSize == aBase->usedCapacity && aOffset + aSize < aBase->capacity && !aBase->isBufferReadOnly()) {
768 // b is a single character (common fast case)
769 ++aBase->usedCapacity;
770 a->data()[aSize] = b->data()[0];
771 return UString::Rep::create(a, 0, aSize + 1);
772 }
773
774 // a is empty
775 if (aSize == 0)
776 return b;
777 // b is empty
778 if (bSize == 0)
779 return a;
780
781 int bOffset = b->offset;
782 int length = aSize + bSize;
783
784 UString::BaseString* bBase = b->baseString();
785 if (aOffset + aSize == aBase->usedCapacity && aSize >= minShareSize && 4 * aSize >= bSize
786 && (-bOffset != bBase->usedPreCapacity || aSize >= bSize) && !aBase->isBufferReadOnly()) {
787 // - a reaches the end of its buffer so it qualifies for shared append
788 // - also, it's at least a quarter the length of b - appending to a much shorter
789 // string does more harm than good
790 // - however, if b qualifies for prepend and is longer than a, we'd rather prepend
791
792 UString x(a);
793 x.expandCapacity(newCapacityWithOverflowCheck(aOffset, length));
794 if (!a->data() || !x.data())
795 return 0;
796 copyChars(a->data() + aSize, b->data(), bSize);
797 PassRefPtr<UString::Rep> result = UString::Rep::create(a, 0, length);
798
799 a->checkConsistency();
800 b->checkConsistency();
801 result->checkConsistency();
802
803 return result;
804 }
805
806 if (-bOffset == bBase->usedPreCapacity && bSize >= minShareSize && 4 * bSize >= aSize && !bBase->isBufferReadOnly()) {
807 // - b reaches the beginning of its buffer so it qualifies for shared prepend
808 // - also, it's at least a quarter the length of a - prepending to a much shorter
809 // string does more harm than good
810 UString y(b);
811 y.expandPreCapacity(-bOffset + aSize);
812 if (!b->data() || !y.data())
813 return 0;
814 copyChars(b->data() - aSize, a->data(), aSize);
815 PassRefPtr<UString::Rep> result = UString::Rep::create(b, -aSize, length);
816
817 a->checkConsistency();
818 b->checkConsistency();
819 result->checkConsistency();
820
821 return result;
822 }
823
824 // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
825 size_t newCapacity = expandedSize(length, 0);
826 UChar* d;
827 if (!allocChars(newCapacity).getValue(d))
828 return 0;
829 copyChars(d, a->data(), aSize);
830 copyChars(d + aSize, b->data(), bSize);
831 PassRefPtr<UString::Rep> result = UString::Rep::create(d, length);
832 result->baseString()->capacity = newCapacity;
833
834 a->checkConsistency();
835 b->checkConsistency();
836 result->checkConsistency();
837
838 return result;
839}
840
841PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, int i)
842{
843 UChar buf[1 + sizeof(i) * 3];
844 UChar* end = buf + sizeof(buf) / sizeof(UChar);
845 UChar* p = end;
846
847 if (i == 0)
848 *--p = '0';
849 else if (i == INT_MIN) {
850 char minBuf[1 + sizeof(i) * 3];
851 sprintf(minBuf, "%d", INT_MIN);
852 return concatenate(rep, minBuf);
853 } else {
854 bool negative = false;
855 if (i < 0) {
856 negative = true;
857 i = -i;
858 }
859 while (i) {
860 *--p = static_cast<unsigned short>((i % 10) + '0');
861 i /= 10;
862 }
863 if (negative)
864 *--p = '-';
865 }
866
867 return concatenate(rep, p, static_cast<int>(end - p));
868
869}
870
871PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, double d)
872{
873 // avoid ever printing -NaN, in JS conceptually there is only one NaN value
874 if (isnan(d))
875 return concatenate(rep, "NaN");
876
877 if (d == 0.0) // stringify -0 as 0
878 d = 0.0;
879
880 char buf[80];
881 int decimalPoint;
882 int sign;
883
884 char result[80];
885 WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
886 int length = static_cast<int>(strlen(result));
887
888 int i = 0;
889 if (sign)
890 buf[i++] = '-';
891
892 if (decimalPoint <= 0 && decimalPoint > -6) {
893 buf[i++] = '0';
894 buf[i++] = '.';
895 for (int j = decimalPoint; j < 0; j++)
896 buf[i++] = '0';
897 strcpy(buf + i, result);
898 } else if (decimalPoint <= 21 && decimalPoint > 0) {
899 if (length <= decimalPoint) {
900 strcpy(buf + i, result);
901 i += length;
902 for (int j = 0; j < decimalPoint - length; j++)
903 buf[i++] = '0';
904 buf[i] = '\0';
905 } else {
906 strncpy(buf + i, result, decimalPoint);
907 i += decimalPoint;
908 buf[i++] = '.';
909 strcpy(buf + i, result + decimalPoint);
910 }
911 } else if (result[0] < '0' || result[0] > '9')
912 strcpy(buf + i, result);
913 else {
914 buf[i++] = result[0];
915 if (length > 1) {
916 buf[i++] = '.';
917 strcpy(buf + i, result + 1);
918 i += length - 1;
919 }
920
921 buf[i++] = 'e';
922 buf[i++] = (decimalPoint >= 0) ? '+' : '-';
923 // decimalPoint can't be more than 3 digits decimal given the
924 // nature of float representation
925 int exponential = decimalPoint - 1;
926 if (exponential < 0)
927 exponential = -exponential;
928 if (exponential >= 100)
929 buf[i++] = static_cast<char>('0' + exponential / 100);
930 if (exponential >= 10)
931 buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
932 buf[i++] = static_cast<char>('0' + exponential % 10);
933 buf[i++] = '\0';
934 }
935
936 return concatenate(rep, buf);
937}
938
939UString UString::from(int i)
940{
941 UChar buf[1 + sizeof(i) * 3];
942 UChar* end = buf + sizeof(buf) / sizeof(UChar);
943 UChar* p = end;
944
945 if (i == 0)
946 *--p = '0';
947 else if (i == INT_MIN) {
948 char minBuf[1 + sizeof(i) * 3];
949 sprintf(minBuf, "%d", INT_MIN);
950 return UString(minBuf);
951 } else {
952 bool negative = false;
953 if (i < 0) {
954 negative = true;
955 i = -i;
956 }
957 while (i) {
958 *--p = static_cast<unsigned short>((i % 10) + '0');
959 i /= 10;
960 }
961 if (negative)
962 *--p = '-';
963 }
964
965 return UString(p, static_cast<int>(end - p));
966}
967
968UString UString::from(long long i)
969{
970 UChar buf[1 + sizeof(i) * 3];
971 UChar* end = buf + sizeof(buf) / sizeof(UChar);
972 UChar* p = end;
973
974 if (i == 0)
975 *--p = '0';
976 else if (i == std::numeric_limits<long long>::min()) {
977 char minBuf[1 + sizeof(i) * 3];
978#if PLATFORM(WIN_OS)
979 snprintf(minBuf, sizeof(minBuf) - 1, "%I64d", std::numeric_limits<long long>::min());
980#else
981 snprintf(minBuf, sizeof(minBuf) - 1, "%lld", std::numeric_limits<long long>::min());
982#endif
983 return UString(minBuf);
984 } else {
985 bool negative = false;
986 if (i < 0) {
987 negative = true;
988 i = -i;
989 }
990 while (i) {
991 *--p = static_cast<unsigned short>((i % 10) + '0');
992 i /= 10;
993 }
994 if (negative)
995 *--p = '-';
996 }
997
998 return UString(p, static_cast<int>(end - p));
999}
1000
1001UString UString::from(unsigned int u)
1002{
1003 UChar buf[sizeof(u) * 3];
1004 UChar* end = buf + sizeof(buf) / sizeof(UChar);
1005 UChar* p = end;
1006
1007 if (u == 0)
1008 *--p = '0';
1009 else {
1010 while (u) {
1011 *--p = static_cast<unsigned short>((u % 10) + '0');
1012 u /= 10;
1013 }
1014 }
1015
1016 return UString(p, static_cast<int>(end - p));
1017}
1018
1019UString UString::from(long l)
1020{
1021 UChar buf[1 + sizeof(l) * 3];
1022 UChar* end = buf + sizeof(buf) / sizeof(UChar);
1023 UChar* p = end;
1024
1025 if (l == 0)
1026 *--p = '0';
1027 else if (l == LONG_MIN) {
1028 char minBuf[1 + sizeof(l) * 3];
1029 sprintf(minBuf, "%ld", LONG_MIN);
1030 return UString(minBuf);
1031 } else {
1032 bool negative = false;
1033 if (l < 0) {
1034 negative = true;
1035 l = -l;
1036 }
1037 while (l) {
1038 *--p = static_cast<unsigned short>((l % 10) + '0');
1039 l /= 10;
1040 }
1041 if (negative)
1042 *--p = '-';
1043 }
1044
1045 return UString(p, static_cast<int>(end - p));
1046}
1047
1048UString UString::from(double d)
1049{
1050 DtoaBuffer buffer;
1051 unsigned length;
1052 doubleToStringInJavaScriptFormat(d, buffer, &length);
1053 return UString(buffer, length);
1054}
1055
1056UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
1057{
1058 m_rep->checkConsistency();
1059
1060 if (rangeCount == 1 && separatorCount == 0) {
1061 int thisSize = size();
1062 int position = substringRanges[0].position;
1063 int length = substringRanges[0].length;
1064 if (position <= 0 && length >= thisSize)
1065 return *this;
1066 return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
1067 }
1068
1069 int totalLength = 0;
1070 for (int i = 0; i < rangeCount; i++)
1071 totalLength += substringRanges[i].length;
1072 for (int i = 0; i < separatorCount; i++)
1073 totalLength += separators[i].size();
1074
1075 if (totalLength == 0)
1076 return "";
1077
1078 UChar* buffer;
1079 if (!allocChars(totalLength).getValue(buffer))
1080 return null();
1081
1082 int maxCount = max(rangeCount, separatorCount);
1083 int bufferPos = 0;
1084 for (int i = 0; i < maxCount; i++) {
1085 if (i < rangeCount) {
1086 copyChars(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length);
1087 bufferPos += substringRanges[i].length;
1088 }
1089 if (i < separatorCount) {
1090 copyChars(buffer + bufferPos, separators[i].data(), separators[i].size());
1091 bufferPos += separators[i].size();
1092 }
1093 }
1094
1095 return UString::Rep::create(buffer, totalLength);
1096}
1097
1098UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
1099{
1100 m_rep->checkConsistency();
1101
1102 int replacementLength = replacement.size();
1103 int totalLength = size() - rangeLength + replacementLength;
1104 if (totalLength == 0)
1105 return "";
1106
1107 UChar* buffer;
1108 if (!allocChars(totalLength).getValue(buffer))
1109 return null();
1110
1111 copyChars(buffer, data(), rangeStart);
1112 copyChars(buffer + rangeStart, replacement.data(), replacementLength);
1113 int rangeEnd = rangeStart + rangeLength;
1114 copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);
1115
1116 return UString::Rep::create(buffer, totalLength);
1117}
1118
1119
1120UString& UString::append(const UString &t)
1121{
1122 m_rep->checkConsistency();
1123 t.rep()->checkConsistency();
1124
1125 int thisSize = size();
1126 int thisOffset = m_rep->offset;
1127 int tSize = t.size();
1128 int length = thisSize + tSize;
1129 BaseString* base = m_rep->baseString();
1130
1131 // possible cases:
1132 if (thisSize == 0) {
1133 // this is empty
1134 *this = t;
1135 } else if (tSize == 0) {
1136 // t is empty
1137 } else if (m_rep == base && !base->isShared()) {
1138 // this is direct and has refcount of 1 (so we can just alter it directly)
1139 expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
1140 if (data()) {
1141 copyChars(m_rep->data() + thisSize, t.data(), tSize);
1142 m_rep->len = length;
1143 m_rep->_hash = 0;
1144 }
1145 } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
1146 // this reaches the end of the buffer - extend it if it's long enough to append to
1147 expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
1148 if (data()) {
1149 copyChars(m_rep->data() + thisSize, t.data(), tSize);
1150 m_rep = Rep::create(m_rep, 0, length);
1151 }
1152 } else {
1153 // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
1154 size_t newCapacity = expandedSize(length, 0);
1155 UChar* d;
1156 if (!allocChars(newCapacity).getValue(d))
1157 makeNull();
1158 else {
1159 copyChars(d, data(), thisSize);
1160 copyChars(d + thisSize, t.data(), tSize);
1161 m_rep = Rep::create(d, length);
1162 m_rep->baseString()->capacity = newCapacity;
1163 }
1164 }
1165
1166 m_rep->checkConsistency();
1167 t.rep()->checkConsistency();
1168
1169 return *this;
1170}
1171
1172UString& UString::append(const UChar* tData, int tSize)
1173{
1174 m_rep = concatenate(m_rep.release(), tData, tSize);
1175 return *this;
1176}
1177
1178UString& UString::append(const char* t)
1179{
1180 m_rep = concatenate(m_rep.release(), t);
1181 return *this;
1182}
1183
1184UString& UString::append(UChar c)
1185{
1186 m_rep->checkConsistency();
1187
1188 int thisOffset = m_rep->offset;
1189 int length = size();
1190 BaseString* base = m_rep->baseString();
1191
1192 // possible cases:
1193 if (length == 0) {
1194 // this is empty - must make a new m_rep because we don't want to pollute the shared empty one
1195 size_t newCapacity = expandedSize(1, 0);
1196 UChar* d;
1197 if (!allocChars(newCapacity).getValue(d))
1198 makeNull();
1199 else {
1200 d[0] = c;
1201 m_rep = Rep::create(d, 1);
1202 m_rep->baseString()->capacity = newCapacity;
1203 }
1204 } else if (m_rep == base && !base->isShared()) {
1205 // this is direct and has refcount of 1 (so we can just alter it directly)
1206 expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
1207 UChar* d = m_rep->data();
1208 if (d) {
1209 d[length] = c;
1210 m_rep->len = length + 1;
1211 m_rep->_hash = 0;
1212 }
1213 } else if (thisOffset + length == base->usedCapacity && length >= minShareSize && !base->isBufferReadOnly()) {
1214 // this reaches the end of the string - extend it and share
1215 expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
1216 UChar* d = m_rep->data();
1217 if (d) {
1218 d[length] = c;
1219 m_rep = Rep::create(m_rep, 0, length + 1);
1220 }
1221 } else {
1222 // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
1223 size_t newCapacity = expandedSize(length + 1, 0);
1224 UChar* d;
1225 if (!allocChars(newCapacity).getValue(d))
1226 makeNull();
1227 else {
1228 copyChars(d, data(), length);
1229 d[length] = c;
1230 m_rep = Rep::create(d, length + 1);
1231 m_rep->baseString()->capacity = newCapacity;
1232 }
1233 }
1234
1235 m_rep->checkConsistency();
1236
1237 return *this;
1238}
1239
1240bool UString::getCString(CStringBuffer& buffer) const
1241{
1242 int length = size();
1243 int neededSize = length + 1;
1244 buffer.resize(neededSize);
1245 char* buf = buffer.data();
1246
1247 UChar ored = 0;
1248 const UChar* p = data();
1249 char* q = buf;
1250 const UChar* limit = p + length;
1251 while (p != limit) {
1252 UChar c = p[0];
1253 ored |= c;
1254 *q = static_cast<char>(c);
1255 ++p;
1256 ++q;
1257 }
1258 *q = '\0';
1259
1260 return !(ored & 0xFF00);
1261}
1262
1263char* UString::ascii() const
1264{
1265 int length = size();
1266 int neededSize = length + 1;
1267 delete[] statBuffer;
1268 statBuffer = new char[neededSize];
1269
1270 const UChar* p = data();
1271 char* q = statBuffer;
1272 const UChar* limit = p + length;
1273 while (p != limit) {
1274 *q = static_cast<char>(p[0]);
1275 ++p;
1276 ++q;
1277 }
1278 *q = '\0';
1279
1280 return statBuffer;
1281}
1282
1283UString& UString::operator=(const char* c)
1284{
1285 if (!c) {
1286 m_rep = &Rep::null();
1287 return *this;
1288 }
1289
1290 if (!c[0]) {
1291 m_rep = &Rep::empty();
1292 return *this;
1293 }
1294
1295 int l = static_cast<int>(strlen(c));
1296 UChar* d;
1297 BaseString* base = m_rep->baseString();
1298 if (!base->isShared() && l <= base->capacity && m_rep == base && m_rep->offset == 0 && base->preCapacity == 0) {
1299 d = base->buf;
1300 m_rep->_hash = 0;
1301 m_rep->len = l;
1302 } else {
1303 if (!allocChars(l).getValue(d)) {
1304 makeNull();
1305 return *this;
1306 }
1307 m_rep = Rep::create(d, l);
1308 }
1309 for (int i = 0; i < l; i++)
1310 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
1311
1312 return *this;
1313}
1314
1315bool UString::is8Bit() const
1316{
1317 const UChar* u = data();
1318 const UChar* limit = u + size();
1319 while (u < limit) {
1320 if (u[0] > 0xFF)
1321 return false;
1322 ++u;
1323 }
1324
1325 return true;
1326}
1327
1328UChar UString::operator[](int pos) const
1329{
1330 if (pos >= size())
1331 return '\0';
1332 return data()[pos];
1333}
1334
1335double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
1336{
1337 if (size() == 1) {
1338 UChar c = data()[0];
1339 if (isASCIIDigit(c))
1340 return c - '0';
1341 if (isASCIISpace(c) && tolerateEmptyString)
1342 return 0;
1343 return NaN;
1344 }
1345
1346 // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
1347 // after the number, so this is too strict a check.
1348 CStringBuffer s;
1349 if (!getCString(s))
1350 return NaN;
1351 const char* c = s.data();
1352
1353 // skip leading white space
1354 while (isASCIISpace(*c))
1355 c++;
1356
1357 // empty string ?
1358 if (*c == '\0')
1359 return tolerateEmptyString ? 0.0 : NaN;
1360
1361 double d;
1362
1363 // hex number ?
1364 if (*c == '0' && (*(c + 1) == 'x' || *(c + 1) == 'X')) {
1365 const char* firstDigitPosition = c + 2;
1366 c++;
1367 d = 0.0;
1368 while (*(++c)) {
1369 if (*c >= '0' && *c <= '9')
1370 d = d * 16.0 + *c - '0';
1371 else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
1372 d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
1373 else
1374 break;
1375 }
1376
1377 if (d >= mantissaOverflowLowerBound)
1378 d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
1379 } else {
1380 // regular number ?
1381 char* end;
1382 d = WTF::strtod(c, &end);
1383 if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
1384 c = end;
1385 } else {
1386 double sign = 1.0;
1387
1388 if (*c == '+')
1389 c++;
1390 else if (*c == '-') {
1391 sign = -1.0;
1392 c++;
1393 }
1394
1395 // We used strtod() to do the conversion. However, strtod() handles
1396 // infinite values slightly differently than JavaScript in that it
1397 // converts the string "inf" with any capitalization to infinity,
1398 // whereas the ECMA spec requires that it be converted to NaN.
1399
1400 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') {
1401 d = sign * Inf;
1402 c += 8;
1403 } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
1404 c = end;
1405 else
1406 return NaN;
1407 }
1408 }
1409
1410 // allow trailing white space
1411 while (isASCIISpace(*c))
1412 c++;
1413 // don't allow anything after - unless tolerant=true
1414 if (!tolerateTrailingJunk && *c != '\0')
1415 d = NaN;
1416
1417 return d;
1418}
1419
1420double UString::toDouble(bool tolerateTrailingJunk) const
1421{
1422 return toDouble(tolerateTrailingJunk, true);
1423}
1424
1425double UString::toDouble() const
1426{
1427 return toDouble(false, true);
1428}
1429
1430uint32_t UString::toUInt32(bool* ok) const
1431{
1432 double d = toDouble();
1433 bool b = true;
1434
1435 if (d != static_cast<uint32_t>(d)) {
1436 b = false;
1437 d = 0;
1438 }
1439
1440 if (ok)
1441 *ok = b;
1442
1443 return static_cast<uint32_t>(d);
1444}
1445
1446uint32_t UString::toUInt32(bool* ok, bool tolerateEmptyString) const
1447{
1448 double d = toDouble(false, tolerateEmptyString);
1449 bool b = true;
1450
1451 if (d != static_cast<uint32_t>(d)) {
1452 b = false;
1453 d = 0;
1454 }
1455
1456 if (ok)
1457 *ok = b;
1458
1459 return static_cast<uint32_t>(d);
1460}
1461
1462uint32_t UString::toStrictUInt32(bool* ok) const
1463{
1464 if (ok)
1465 *ok = false;
1466
1467 // Empty string is not OK.
1468 int len = m_rep->len;
1469 if (len == 0)
1470 return 0;
1471 const UChar* p = m_rep->data();
1472 unsigned short c = p[0];
1473
1474 // If the first digit is 0, only 0 itself is OK.
1475 if (c == '0') {
1476 if (len == 1 && ok)
1477 *ok = true;
1478 return 0;
1479 }
1480
1481 // Convert to UInt32, checking for overflow.
1482 uint32_t i = 0;
1483 while (1) {
1484 // Process character, turning it into a digit.
1485 if (c < '0' || c > '9')
1486 return 0;
1487 const unsigned d = c - '0';
1488
1489 // Multiply by 10, checking for overflow out of 32 bits.
1490 if (i > 0xFFFFFFFFU / 10)
1491 return 0;
1492 i *= 10;
1493
1494 // Add in the digit, checking for overflow out of 32 bits.
1495 const unsigned max = 0xFFFFFFFFU - d;
1496 if (i > max)
1497 return 0;
1498 i += d;
1499
1500 // Handle end of string.
1501 if (--len == 0) {
1502 if (ok)
1503 *ok = true;
1504 return i;
1505 }
1506
1507 // Get next character.
1508 c = *(++p);
1509 }
1510}
1511
1512int UString::find(const UString& f, int pos) const
1513{
1514 int fsz = f.size();
1515
1516 if (pos < 0)
1517 pos = 0;
1518
1519 if (fsz == 1) {
1520 UChar ch = f[0];
1521 const UChar* end = data() + size();
1522 for (const UChar* c = data() + pos; c < end; c++) {
1523 if (*c == ch)
1524 return static_cast<int>(c - data());
1525 }
1526 return -1;
1527 }
1528
1529 int sz = size();
1530 if (sz < fsz)
1531 return -1;
1532 if (fsz == 0)
1533 return pos;
1534 const UChar* end = data() + sz - fsz;
1535 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1536 const UChar* fdata = f.data();
1537 unsigned short fchar = fdata[0];
1538 ++fdata;
1539 for (const UChar* c = data() + pos; c <= end; c++) {
1540 if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone))
1541 return static_cast<int>(c - data());
1542 }
1543
1544 return -1;
1545}
1546
1547int UString::find(UChar ch, int pos) const
1548{
1549 if (pos < 0)
1550 pos = 0;
1551 const UChar* end = data() + size();
1552 for (const UChar* c = data() + pos; c < end; c++) {
1553 if (*c == ch)
1554 return static_cast<int>(c - data());
1555 }
1556
1557 return -1;
1558}
1559
1560int UString::rfind(const UString& f, int pos) const
1561{
1562 int sz = size();
1563 int fsz = f.size();
1564 if (sz < fsz)
1565 return -1;
1566 if (pos < 0)
1567 pos = 0;
1568 if (pos > sz - fsz)
1569 pos = sz - fsz;
1570 if (fsz == 0)
1571 return pos;
1572 int fsizeminusone = (fsz - 1) * sizeof(UChar);
1573 const UChar* fdata = f.data();
1574 for (const UChar* c = data() + pos; c >= data(); c--) {
1575 if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
1576 return static_cast<int>(c - data());
1577 }
1578
1579 return -1;
1580}
1581
1582int UString::rfind(UChar ch, int pos) const
1583{
1584 if (isEmpty())
1585 return -1;
1586 if (pos + 1 >= size())
1587 pos = size() - 1;
1588 for (const UChar* c = data() + pos; c >= data(); c--) {
1589 if (*c == ch)
1590 return static_cast<int>(c - data());
1591 }
1592
1593 return -1;
1594}
1595
1596UString UString::substr(int pos, int len) const
1597{
1598 int s = size();
1599
1600 if (pos < 0)
1601 pos = 0;
1602 else if (pos >= s)
1603 pos = s;
1604 if (len < 0)
1605 len = s;
1606 if (pos + len >= s)
1607 len = s - pos;
1608
1609 if (pos == 0 && len == s)
1610 return *this;
1611
1612 return UString(Rep::create(m_rep, pos, len));
1613}
1614
1615bool operator==(const UString& s1, const char *s2)
1616{
1617 if (s2 == 0)
1618 return s1.isEmpty();
1619
1620 const UChar* u = s1.data();
1621 const UChar* uend = u + s1.size();
1622 while (u != uend && *s2) {
1623 if (u[0] != (unsigned char)*s2)
1624 return false;
1625 s2++;
1626 u++;
1627 }
1628
1629 return u == uend && *s2 == 0;
1630}
1631
1632bool operator<(const UString& s1, const UString& s2)
1633{
1634 const int l1 = s1.size();
1635 const int l2 = s2.size();
1636 const int lmin = l1 < l2 ? l1 : l2;
1637 const UChar* c1 = s1.data();
1638 const UChar* c2 = s2.data();
1639 int l = 0;
1640 while (l < lmin && *c1 == *c2) {
1641 c1++;
1642 c2++;
1643 l++;
1644 }
1645 if (l < lmin)
1646 return (c1[0] < c2[0]);
1647
1648 return (l1 < l2);
1649}
1650
1651bool operator>(const UString& s1, const UString& s2)
1652{
1653 const int l1 = s1.size();
1654 const int l2 = s2.size();
1655 const int lmin = l1 < l2 ? l1 : l2;
1656 const UChar* c1 = s1.data();
1657 const UChar* c2 = s2.data();
1658 int l = 0;
1659 while (l < lmin && *c1 == *c2) {
1660 c1++;
1661 c2++;
1662 l++;
1663 }
1664 if (l < lmin)
1665 return (c1[0] > c2[0]);
1666
1667 return (l1 > l2);
1668}
1669
1670int compare(const UString& s1, const UString& s2)
1671{
1672 const int l1 = s1.size();
1673 const int l2 = s2.size();
1674 const int lmin = l1 < l2 ? l1 : l2;
1675 const UChar* c1 = s1.data();
1676 const UChar* c2 = s2.data();
1677 int l = 0;
1678 while (l < lmin && *c1 == *c2) {
1679 c1++;
1680 c2++;
1681 l++;
1682 }
1683
1684 if (l < lmin)
1685 return (c1[0] > c2[0]) ? 1 : -1;
1686
1687 if (l1 == l2)
1688 return 0;
1689
1690 return (l1 > l2) ? 1 : -1;
1691}
1692
1693bool equal(const UString::Rep* r, const UString::Rep* b)
1694{
1695 int length = r->len;
1696 if (length != b->len)
1697 return false;
1698 const UChar* d = r->data();
1699 const UChar* s = b->data();
1700 for (int i = 0; i != length; ++i) {
1701 if (d[i] != s[i])
1702 return false;
1703 }
1704 return true;
1705}
1706
1707CString UString::UTF8String(bool strict) const
1708{
1709 // Allocate a buffer big enough to hold all the characters.
1710 const int length = size();
1711 Vector<char, 1024> buffer(length * 3);
1712
1713 // Convert to runs of 8-bit characters.
1714 char* p = buffer.data();
1715 const UChar* d = reinterpret_cast<const UChar*>(&data()[0]);
1716 ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
1717 if (result != conversionOK)
1718 return CString();
1719
1720 return CString(buffer.data(), p - buffer.data());
1721}
1722
1723// For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
1724NEVER_INLINE void UString::makeNull()
1725{
1726 m_rep = &Rep::null();
1727}
1728
1729// For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
1730NEVER_INLINE UString::Rep* UString::nullRep()
1731{
1732 return &Rep::null();
1733}
1734
1735} // namespace JSC
Note: See TracBrowser for help on using the repository browser.