summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp')
-rw-r--r--src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp500
1 files changed, 303 insertions, 197 deletions
diff --git a/src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp b/src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp
index 45df248..0eb46da 100644
--- a/src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp
+++ b/src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp
@@ -1,7 +1,8 @@
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
+ * Copyright (C) 2009 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -61,6 +62,9 @@ namespace JSC {
extern const double NaN;
extern const double Inf;
+// This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
+static const int minLengthToShare = 30;
+
static inline size_t overflowIndicator() { return std::numeric_limits<size_t>::max(); }
static inline size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); }
@@ -92,7 +96,7 @@ static inline void copyChars(UChar* destination, const UChar* source, unsigned n
memcpy(destination, source, numCharacters * sizeof(UChar));
}
-COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes)
+COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes);
CString::CString(const char* c)
: m_length(strlen(c))
@@ -186,86 +190,59 @@ bool operator==(const CString& c1, const CString& c2)
// 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.
static UChar sharedEmptyChar;
-UString::Rep UString::Rep::null = { 0, 0, INT_MAX / 2, 0, 1, &UString::Rep::null, 0, 0, 0, 0, 0, 0 };
-UString::Rep UString::Rep::empty = { 0, 0, INT_MAX / 2, 0, 1, &UString::Rep::empty, 0, &sharedEmptyChar, 0, 0, 0, 0 };
-
-static char* statBuffer = 0; // Only used for debugging via UString::ascii().
+UString::BaseString* UString::Rep::nullBaseString;
+UString::BaseString* UString::Rep::emptyBaseString;
+UString* UString::nullUString;
-PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar* d, int l)
+static void initializeStaticBaseString(UString::BaseString& base)
{
- UChar* copyD = static_cast<UChar*>(fastMalloc(l * sizeof(UChar)));
- copyChars(copyD, d, l);
- return create(copyD, l);
+ base.rc = INT_MAX / 2;
+ base.m_identifierTableAndFlags.setFlag(UString::Rep::StaticFlag);
+ base.checkConsistency();
}
-PassRefPtr<UString::Rep> UString::Rep::create(UChar* d, int l)
+void initializeUString()
{
- Rep* r = new Rep;
- r->offset = 0;
- r->len = l;
- r->rc = 1;
- r->_hash = 0;
- r->m_identifierTable = 0;
- r->baseString = r;
- r->reportedCost = 0;
- r->buf = d;
- r->usedCapacity = l;
- r->capacity = l;
- r->usedPreCapacity = 0;
- r->preCapacity = 0;
+ UString::Rep::nullBaseString = new UString::BaseString(0, 0);
+ initializeStaticBaseString(*UString::Rep::nullBaseString);
- r->checkConsistency();
+ UString::Rep::emptyBaseString = new UString::BaseString(&sharedEmptyChar, 0);
+ initializeStaticBaseString(*UString::Rep::emptyBaseString);
- // steal the single reference this Rep was created with
- return adoptRef(r);
+ UString::nullUString = new UString;
}
-PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length)
-{
- ASSERT(base);
- base->checkConsistency();
-
- int baseOffset = base->offset;
-
- base = base->baseString;
-
- ASSERT(-(offset + baseOffset) <= base->usedPreCapacity);
- ASSERT(offset + baseOffset + length <= base->usedCapacity);
-
- Rep* r = new Rep;
- r->offset = baseOffset + offset;
- r->len = length;
- r->rc = 1;
- r->_hash = 0;
- r->m_identifierTable = 0;
- r->baseString = base.releaseRef();
- r->reportedCost = 0;
- r->buf = 0;
- r->usedCapacity = 0;
- r->capacity = 0;
- r->usedPreCapacity = 0;
- r->preCapacity = 0;
-
- r->checkConsistency();
+static char* statBuffer = 0; // Only used for debugging via UString::ascii().
- // steal the single reference this Rep was created with
- return adoptRef(r);
+PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar* d, int l)
+{
+ UChar* copyD = static_cast<UChar*>(fastMalloc(l * sizeof(UChar)));
+ copyChars(copyD, d, l);
+ return create(copyD, l);
}
PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string)
{
if (!string)
- return &UString::Rep::null;
+ return &UString::Rep::null();
size_t length = strlen(string);
Vector<UChar, 1024> buffer(length);
UChar* p = buffer.data();
if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
- return &UString::Rep::null;
+ return &UString::Rep::null();
return UString::Rep::createCopying(buffer.data(), p - buffer.data());
}
+PassRefPtr<UString::Rep> UString::Rep::create(UChar* string, int length, PassRefPtr<UString::SharedUChar> sharedBuffer)
+{
+ PassRefPtr<UString::Rep> rep = create(string, length);
+ rep->baseString()->setSharedBuffer(sharedBuffer);
+ rep->checkConsistency();
+ return rep;
+}
+
void UString::Rep::destroy()
{
checkConsistency();
@@ -275,10 +252,15 @@ void UString::Rep::destroy()
if (!isStatic()) {
if (identifierTable())
Identifier::remove(this);
- if (baseString == this)
- fastFree(buf);
- else
- baseString->deref();
+
+ UString::BaseString* base = baseString();
+ if (base == this) {
+ if (m_sharedBuffer)
+ m_sharedBuffer->deref();
+ else
+ fastFree(base->buf);
+ } else
+ base->deref();
delete this;
}
@@ -380,17 +362,10 @@ unsigned UString::Rep::computeHash(const char* s, int l)
#ifndef NDEBUG
void UString::Rep::checkConsistency() const
{
- // Only base strings have non-zero shared data.
- if (this != baseString) {
- ASSERT(!buf);
- ASSERT(!usedCapacity);
- ASSERT(!capacity);
- ASSERT(!usedPreCapacity);
- ASSERT(!preCapacity);
- }
+ const UString::BaseString* base = baseString();
// There is no recursion for base strings.
- ASSERT(baseString == baseString->baseString);
+ ASSERT(base == base->baseString());
if (isStatic()) {
// There are only two static strings: null and empty.
@@ -401,63 +376,148 @@ void UString::Rep::checkConsistency() const
}
// The string fits in buffer.
- ASSERT(baseString->usedPreCapacity <= baseString->preCapacity);
- ASSERT(baseString->usedCapacity <= baseString->capacity);
- ASSERT(-offset <= baseString->usedPreCapacity);
- ASSERT(offset + len <= baseString->usedCapacity);
+ ASSERT(base->usedPreCapacity <= base->preCapacity);
+ ASSERT(base->usedCapacity <= base->capacity);
+ ASSERT(-offset <= base->usedPreCapacity);
+ ASSERT(offset + len <= base->usedCapacity);
}
#endif
-// put these early so they can be inlined
-static inline size_t expandedSize(size_t size, size_t otherSize)
+UString::SharedUChar* UString::BaseString::sharedBuffer()
{
- // Do the size calculation in two parts, returning overflowIndicator if
- // we overflow the maximum value that we can handle.
- if (size > maxUChars())
- return overflowIndicator();
-
- size_t expandedSize = ((size + 10) / 10 * 11) + 1;
- if (maxUChars() - expandedSize < otherSize)
- return overflowIndicator();
+ if (len < minLengthToShare)
+ return 0;
- return expandedSize + otherSize;
+ if (!m_sharedBuffer)
+ setSharedBuffer(SharedUChar::create(new OwnFastMallocPtr<UChar>(buf)));
+ return m_sharedBuffer;
}
-inline int UString::usedCapacity() const
+void UString::BaseString::setSharedBuffer(PassRefPtr<UString::SharedUChar> sharedBuffer)
{
- return m_rep->baseString->usedCapacity;
+ // The manual steps below are because m_sharedBuffer can't be a RefPtr. m_sharedBuffer
+ // is in a union with another variable to avoid making BaseString any larger.
+ if (m_sharedBuffer)
+ m_sharedBuffer->deref();
+ m_sharedBuffer = sharedBuffer.releaseRef();
}
-inline int UString::usedPreCapacity() const
+bool UString::BaseString::slowIsBufferReadOnly()
{
- return m_rep->baseString->usedPreCapacity;
+ // The buffer may not be modified as soon as the underlying data has been shared with another class.
+ if (m_sharedBuffer->isShared())
+ return true;
+
+ // At this point, we know it that the underlying buffer isn't shared outside of this base class,
+ // so get rid of m_sharedBuffer.
+ OwnPtr<OwnFastMallocPtr<UChar> > mallocPtr(m_sharedBuffer->release());
+ UChar* unsharedBuf = const_cast<UChar*>(mallocPtr->release());
+ setSharedBuffer(0);
+ preCapacity += (buf - unsharedBuf);
+ buf = unsharedBuf;
+ return false;
}
+// Put these early so they can be inlined.
+static inline size_t expandedSize(size_t capacitySize, size_t precapacitySize)
+{
+ // Combine capacitySize & precapacitySize to produce a single size to allocate,
+ // check that doing so does not result in overflow.
+ size_t size = capacitySize + precapacitySize;
+ if (size < capacitySize)
+ return overflowIndicator();
+
+ // Small Strings (up to 4 pages):
+ // Expand the allocation size to 112.5% of the amount requested. This is largely sicking
+ // to our previous policy, however 112.5% is cheaper to calculate.
+ if (size < 0x4000) {
+ size_t expandedSize = ((size + (size >> 3)) | 15) + 1;
+ // Given the limited range within which we calculate the expansion in this
+ // fashion the above calculation should never overflow.
+ ASSERT(expandedSize >= size);
+ ASSERT(expandedSize < maxUChars());
+ return expandedSize;
+ }
+
+ // Medium Strings (up to 128 pages):
+ // For pages covering multiple pages over-allocation is less of a concern - any unused
+ // space will not be paged in if it is not used, so this is purely a VM overhead. For
+ // these strings allocate 2x the requested size.
+ if (size < 0x80000) {
+ size_t expandedSize = ((size + size) | 0xfff) + 1;
+ // Given the limited range within which we calculate the expansion in this
+ // fashion the above calculation should never overflow.
+ ASSERT(expandedSize >= size);
+ ASSERT(expandedSize < maxUChars());
+ return expandedSize;
+ }
+
+ // Large Strings (to infinity and beyond!):
+ // Revert to our 112.5% policy - probably best to limit the amount of unused VM we allow
+ // any individual string be responsible for.
+ size_t expandedSize = ((size + (size >> 3)) | 0xfff) + 1;
+
+ // Check for overflow - any result that is at least as large as requested (but
+ // still below the limit) is okay.
+ if ((expandedSize >= size) && (expandedSize < maxUChars()))
+ return expandedSize;
+ return overflowIndicator();
+}
static inline bool expandCapacity(UString::Rep* rep, int requiredLength)
{
rep->checkConsistency();
+ ASSERT(!rep->baseString()->isBufferReadOnly());
- UString::Rep* r = rep->baseString;
+ UString::BaseString* base = rep->baseString();
- if (requiredLength > r->capacity) {
- size_t newCapacity = expandedSize(requiredLength, r->preCapacity);
- UChar* oldBuf = r->buf;
- r->buf = reallocChars(r->buf, newCapacity);
- if (!r->buf) {
- r->buf = oldBuf;
+ if (requiredLength > base->capacity) {
+ size_t newCapacity = expandedSize(requiredLength, base->preCapacity);
+ UChar* oldBuf = base->buf;
+ base->buf = reallocChars(base->buf, newCapacity);
+ if (!base->buf) {
+ base->buf = oldBuf;
return false;
}
- r->capacity = newCapacity - r->preCapacity;
+ base->capacity = newCapacity - base->preCapacity;
}
- if (requiredLength > r->usedCapacity)
- r->usedCapacity = requiredLength;
+ if (requiredLength > base->usedCapacity)
+ base->usedCapacity = requiredLength;
rep->checkConsistency();
return true;
}
+bool UString::Rep::reserveCapacity(int capacity)
+{
+ // If this is an empty string there is no point 'growing' it - just allocate a new one.
+ // If the BaseString is shared with another string that is using more capacity than this
+ // string is, then growing the buffer won't help.
+ // If the BaseString's buffer is readonly, then it isn't allowed to grow.
+ UString::BaseString* base = baseString();
+ if (!base->buf || !base->capacity || (offset + len) != base->usedCapacity || base->isBufferReadOnly())
+ return false;
+
+ // If there is already sufficient capacity, no need to grow!
+ if (capacity <= base->capacity)
+ return true;
+
+ checkConsistency();
+
+ size_t newCapacity = expandedSize(capacity, base->preCapacity);
+ UChar* oldBuf = base->buf;
+ base->buf = reallocChars(base->buf, newCapacity);
+ if (!base->buf) {
+ base->buf = oldBuf;
+ return false;
+ }
+ base->capacity = newCapacity - base->preCapacity;
+
+ checkConsistency();
+ return true;
+}
+
void UString::expandCapacity(int requiredLength)
{
if (!JSC::expandCapacity(m_rep.get(), requiredLength))
@@ -467,42 +527,43 @@ void UString::expandCapacity(int requiredLength)
void UString::expandPreCapacity(int requiredPreCap)
{
m_rep->checkConsistency();
+ ASSERT(!m_rep->baseString()->isBufferReadOnly());
- Rep* r = m_rep->baseString;
+ BaseString* base = m_rep->baseString();
- if (requiredPreCap > r->preCapacity) {
- size_t newCapacity = expandedSize(requiredPreCap, r->capacity);
- int delta = newCapacity - r->capacity - r->preCapacity;
+ if (requiredPreCap > base->preCapacity) {
+ size_t newCapacity = expandedSize(requiredPreCap, base->capacity);
+ int delta = newCapacity - base->capacity - base->preCapacity;
UChar* newBuf = allocChars(newCapacity);
if (!newBuf) {
makeNull();
return;
}
- copyChars(newBuf + delta, r->buf, r->capacity + r->preCapacity);
- fastFree(r->buf);
- r->buf = newBuf;
+ copyChars(newBuf + delta, base->buf, base->capacity + base->preCapacity);
+ fastFree(base->buf);
+ base->buf = newBuf;
- r->preCapacity = newCapacity - r->capacity;
+ base->preCapacity = newCapacity - base->capacity;
}
- if (requiredPreCap > r->usedPreCapacity)
- r->usedPreCapacity = requiredPreCap;
+ if (requiredPreCap > base->usedPreCapacity)
+ base->usedPreCapacity = requiredPreCap;
m_rep->checkConsistency();
}
-PassRefPtr<UString::Rep> createRep(const char* c)
+static PassRefPtr<UString::Rep> createRep(const char* c)
{
if (!c)
- return &UString::Rep::null;
+ return &UString::Rep::null();
if (!c[0])
- return &UString::Rep::empty;
+ return &UString::Rep::empty();
size_t length = strlen(c);
UChar* d = allocChars(length);
if (!d)
- return &UString::Rep::null;
+ return &UString::Rep::null();
else {
for (size_t i = 0; i < length; i++)
d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
@@ -519,7 +580,7 @@ UString::UString(const char* c)
UString::UString(const UChar* c, int length)
{
if (length == 0)
- m_rep = &Rep::empty;
+ m_rep = &Rep::empty();
else
m_rep = Rep::createCopying(c, length);
}
@@ -527,7 +588,7 @@ UString::UString(const UChar* c, int length)
UString::UString(UChar* c, int length, bool copy)
{
if (length == 0)
- m_rep = &Rep::empty;
+ m_rep = &Rep::empty();
else if (copy)
m_rep = Rep::createCopying(c, length);
else
@@ -537,11 +598,22 @@ UString::UString(UChar* c, int length, bool copy)
UString::UString(const Vector<UChar>& buffer)
{
if (!buffer.size())
- m_rep = &Rep::empty;
+ m_rep = &Rep::empty();
else
m_rep = Rep::createCopying(buffer.data(), buffer.size());
}
+static ALWAYS_INLINE int newCapacityWithOverflowCheck(const int currentCapacity, const int extendLength, const bool plusOne = false)
+{
+ ASSERT_WITH_MESSAGE(extendLength >= 0, "extendedLength = %d", extendLength);
+
+ const int plusLength = plusOne ? 1 : 0;
+ if (currentCapacity > std::numeric_limits<int>::max() - extendLength - plusLength)
+ CRASH();
+
+ return currentCapacity + extendLength + plusLength;
+}
+
static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const UChar* tData, int tSize)
{
RefPtr<UString::Rep> rep = r;
@@ -551,6 +623,7 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re
int thisSize = rep->size();
int thisOffset = rep->offset;
int length = thisSize + tSize;
+ UString::BaseString* base = rep->baseString();
// possible cases:
if (tSize == 0) {
@@ -558,34 +631,34 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re
} else if (thisSize == 0) {
// this is empty
rep = UString::Rep::createCopying(tData, tSize);
- } else if (rep->baseIsSelf() && rep->rc == 1) {
+ } else if (rep == base && !base->isShared()) {
// this is direct and has refcount of 1 (so we can just alter it directly)
- if (!expandCapacity(rep.get(), thisOffset + length))
- rep = &UString::Rep::null;
+ if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
+ rep = &UString::Rep::null();
if (rep->data()) {
copyChars(rep->data() + thisSize, tData, tSize);
rep->len = length;
rep->_hash = 0;
}
- } else if (thisOffset + thisSize == rep->baseString->usedCapacity && thisSize >= minShareSize) {
+ } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
// this reaches the end of the buffer - extend it if it's long enough to append to
- if (!expandCapacity(rep.get(), thisOffset + length))
- rep = &UString::Rep::null;
+ if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
+ rep = &UString::Rep::null();
if (rep->data()) {
copyChars(rep->data() + thisSize, tData, tSize);
rep = UString::Rep::create(rep, 0, length);
}
} else {
- // this is shared with someone using more capacity, gotta make a whole new string
+ // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
size_t newCapacity = expandedSize(length, 0);
UChar* d = allocChars(newCapacity);
if (!d)
- rep = &UString::Rep::null;
+ rep = &UString::Rep::null();
else {
copyChars(d, rep->data(), thisSize);
copyChars(d + thisSize, tData, tSize);
rep = UString::Rep::create(d, length);
- rep->capacity = newCapacity;
+ rep->baseString()->capacity = newCapacity;
}
}
@@ -604,6 +677,7 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re
int thisOffset = rep->offset;
int tSize = static_cast<int>(strlen(t));
int length = thisSize + tSize;
+ UString::BaseString* base = rep->baseString();
// possible cases:
if (thisSize == 0) {
@@ -611,9 +685,9 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re
rep = createRep(t);
} else if (tSize == 0) {
// t is empty, we'll just return *this below.
- } else if (rep->baseIsSelf() && rep->rc == 1) {
+ } else if (rep == base && !base->isShared()) {
// this is direct and has refcount of 1 (so we can just alter it directly)
- expandCapacity(rep.get(), thisOffset + length);
+ expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
UChar* d = rep->data();
if (d) {
for (int i = 0; i < tSize; ++i)
@@ -621,9 +695,9 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re
rep->len = length;
rep->_hash = 0;
}
- } else if (thisOffset + thisSize == rep->baseString->usedCapacity && thisSize >= minShareSize) {
+ } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
// this string reaches the end of the buffer - extend it
- expandCapacity(rep.get(), thisOffset + length);
+ expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
UChar* d = rep->data();
if (d) {
for (int i = 0; i < tSize; ++i)
@@ -631,17 +705,17 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re
rep = UString::Rep::create(rep, 0, length);
}
} else {
- // this is shared with someone using more capacity, gotta make a whole new string
+ // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
size_t newCapacity = expandedSize(length, 0);
UChar* d = allocChars(newCapacity);
if (!d)
- rep = &UString::Rep::null;
+ rep = &UString::Rep::null();
else {
copyChars(d, rep->data(), thisSize);
for (int i = 0; i < tSize; ++i)
d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
rep = UString::Rep::create(d, length);
- rep->capacity = newCapacity;
+ rep->baseString()->capacity = newCapacity;
}
}
@@ -656,13 +730,19 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
b->checkConsistency();
int aSize = a->size();
- int aOffset = a->offset;
int bSize = b->size();
- int bOffset = b->offset;
- int length = aSize + bSize;
+ int aOffset = a->offset;
// possible cases:
+ UString::BaseString* aBase = a->baseString();
+ if (bSize == 1 && aOffset + aSize == aBase->usedCapacity && aOffset + aSize < aBase->capacity && !aBase->isBufferReadOnly()) {
+ // b is a single character (common fast case)
+ ++aBase->usedCapacity;
+ a->data()[aSize] = b->data()[0];
+ return UString::Rep::create(a, 0, aSize + 1);
+ }
+
// a is empty
if (aSize == 0)
return b;
@@ -670,21 +750,19 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
if (bSize == 0)
return a;
- if (bSize == 1 && aOffset + aSize == a->baseString->usedCapacity && aOffset + length <= a->baseString->capacity) {
- // b is a single character (common fast case)
- a->baseString->usedCapacity = aOffset + length;
- a->data()[aSize] = b->data()[0];
- return UString::Rep::create(a, 0, length);
- }
+ int bOffset = b->offset;
+ int length = aSize + bSize;
- if (aOffset + aSize == a->baseString->usedCapacity && aSize >= minShareSize && 4 * aSize >= bSize &&
- (-bOffset != b->baseString->usedPreCapacity || aSize >= bSize)) {
+ UString::BaseString* bBase = b->baseString();
+ if (aOffset + aSize == aBase->usedCapacity && aSize >= minShareSize && 4 * aSize >= bSize
+ && (-bOffset != bBase->usedPreCapacity || aSize >= bSize) && !aBase->isBufferReadOnly()) {
// - a reaches the end of its buffer so it qualifies for shared append
// - also, it's at least a quarter the length of b - appending to a much shorter
// string does more harm than good
// - however, if b qualifies for prepend and is longer than a, we'd rather prepend
+
UString x(a);
- x.expandCapacity(aOffset + length);
+ x.expandCapacity(newCapacityWithOverflowCheck(aOffset, length));
if (!a->data() || !x.data())
return 0;
copyChars(a->data() + aSize, b->data(), bSize);
@@ -697,7 +775,7 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
return result;
}
- if (-bOffset == b->baseString->usedPreCapacity && bSize >= minShareSize && 4 * bSize >= aSize) {
+ if (-bOffset == bBase->usedPreCapacity && bSize >= minShareSize && 4 * bSize >= aSize && !bBase->isBufferReadOnly()) {
// - b reaches the beginning of its buffer so it qualifies for shared prepend
// - also, it's at least a quarter the length of a - prepending to a much shorter
// string does more harm than good
@@ -723,7 +801,7 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
copyChars(d, a->data(), aSize);
copyChars(d + aSize, b->data(), bSize);
PassRefPtr<UString::Rep> result = UString::Rep::create(d, length);
- result->capacity = newCapacity;
+ result->baseString()->capacity = newCapacity;
a->checkConsistency();
b->checkConsistency();
@@ -775,7 +853,8 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, double d)
int decimalPoint;
int sign;
- char* result = WTF::dtoa(d, 0, &decimalPoint, &sign, NULL);
+ char result[80];
+ WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
int length = static_cast<int>(strlen(result));
int i = 0;
@@ -826,15 +905,7 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, double d)
buf[i++] = '\0';
}
- WTF::freedtoa(result);
-
- return concatenate(rep, buf);
-}
-
-const UString& UString::null()
-{
- static UString* n = new UString; // Should be called from main thread at least once to be safely initialized.
- return *n;
+ return concatenate(rep, buf);
}
UString UString::from(int i)
@@ -922,8 +993,9 @@ UString UString::from(double d)
char buf[80];
int decimalPoint;
int sign;
-
- char* result = WTF::dtoa(d, 0, &decimalPoint, &sign, NULL);
+
+ char result[80];
+ WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
int length = static_cast<int>(strlen(result));
int i = 0;
@@ -974,9 +1046,7 @@ UString UString::from(double d)
buf[i++] = '\0';
}
- WTF::freedtoa(result);
-
- return UString(buf);
+ return UString(buf);
}
UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
@@ -1021,6 +1091,28 @@ UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, in
return UString::Rep::create(buffer, totalLength);
}
+UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
+{
+ m_rep->checkConsistency();
+
+ int replacementLength = replacement.size();
+ int totalLength = size() - rangeLength + replacementLength;
+ if (totalLength == 0)
+ return "";
+
+ UChar* buffer = allocChars(totalLength);
+ if (!buffer)
+ return null();
+
+ copyChars(buffer, data(), rangeStart);
+ copyChars(buffer + rangeStart, replacement.data(), replacementLength);
+ int rangeEnd = rangeStart + rangeLength;
+ copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);
+
+ return UString::Rep::create(buffer, totalLength);
+}
+
+
UString& UString::append(const UString &t)
{
m_rep->checkConsistency();
@@ -1030,6 +1122,7 @@ UString& UString::append(const UString &t)
int thisOffset = m_rep->offset;
int tSize = t.size();
int length = thisSize + tSize;
+ BaseString* base = m_rep->baseString();
// possible cases:
if (thisSize == 0) {
@@ -1037,23 +1130,23 @@ UString& UString::append(const UString &t)
*this = t;
} else if (tSize == 0) {
// t is empty
- } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
+ } else if (m_rep == base && !base->isShared()) {
// this is direct and has refcount of 1 (so we can just alter it directly)
- expandCapacity(thisOffset + length);
+ expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
if (data()) {
copyChars(m_rep->data() + thisSize, t.data(), tSize);
m_rep->len = length;
m_rep->_hash = 0;
}
- } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) {
+ } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
// this reaches the end of the buffer - extend it if it's long enough to append to
- expandCapacity(thisOffset + length);
+ expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
if (data()) {
copyChars(m_rep->data() + thisSize, t.data(), tSize);
m_rep = Rep::create(m_rep, 0, length);
}
} else {
- // this is shared with someone using more capacity, gotta make a whole new string
+ // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
size_t newCapacity = expandedSize(length, 0);
UChar* d = allocChars(newCapacity);
if (!d)
@@ -1062,7 +1155,7 @@ UString& UString::append(const UString &t)
copyChars(d, data(), thisSize);
copyChars(d + thisSize, t.data(), tSize);
m_rep = Rep::create(d, length);
- m_rep->capacity = newCapacity;
+ m_rep->baseString()->capacity = newCapacity;
}
}
@@ -1078,6 +1171,18 @@ UString& UString::append(const UChar* tData, int tSize)
return *this;
}
+UString& UString::appendNumeric(int i)
+{
+ m_rep = concatenate(rep(), i);
+ return *this;
+}
+
+UString& UString::appendNumeric(double d)
+{
+ m_rep = concatenate(rep(), d);
+ return *this;
+}
+
UString& UString::append(const char* t)
{
m_rep = concatenate(m_rep.release(), t);
@@ -1090,6 +1195,7 @@ UString& UString::append(UChar c)
int thisOffset = m_rep->offset;
int length = size();
+ BaseString* base = m_rep->baseString();
// possible cases:
if (length == 0) {
@@ -1101,27 +1207,27 @@ UString& UString::append(UChar c)
else {
d[0] = c;
m_rep = Rep::create(d, 1);
- m_rep->capacity = newCapacity;
+ m_rep->baseString()->capacity = newCapacity;
}
- } else if (m_rep->baseIsSelf() && m_rep->rc == 1) {
+ } else if (m_rep == base && !base->isShared()) {
// this is direct and has refcount of 1 (so we can just alter it directly)
- expandCapacity(thisOffset + length + 1);
+ expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
UChar* d = m_rep->data();
if (d) {
d[length] = c;
m_rep->len = length + 1;
m_rep->_hash = 0;
}
- } else if (thisOffset + length == usedCapacity() && length >= minShareSize) {
+ } else if (thisOffset + length == base->usedCapacity && length >= minShareSize && !base->isBufferReadOnly()) {
// this reaches the end of the string - extend it and share
- expandCapacity(thisOffset + length + 1);
+ expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
UChar* d = m_rep->data();
if (d) {
d[length] = c;
m_rep = Rep::create(m_rep, 0, length + 1);
}
} else {
- // this is shared with someone using more capacity, gotta make a whole new string
+ // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
size_t newCapacity = expandedSize(length + 1, 0);
UChar* d = allocChars(newCapacity);
if (!d)
@@ -1130,7 +1236,7 @@ UString& UString::append(UChar c)
copyChars(d, data(), length);
d[length] = c;
m_rep = Rep::create(d, length + 1);
- m_rep->capacity = newCapacity;
+ m_rep->baseString()->capacity = newCapacity;
}
}
@@ -1185,19 +1291,20 @@ char* UString::ascii() const
UString& UString::operator=(const char* c)
{
if (!c) {
- m_rep = &Rep::null;
+ m_rep = &Rep::null();
return *this;
}
if (!c[0]) {
- m_rep = &Rep::empty;
+ m_rep = &Rep::empty();
return *this;
}
int l = static_cast<int>(strlen(c));
UChar* d;
- if (m_rep->rc == 1 && l <= m_rep->capacity && m_rep->baseIsSelf() && m_rep->offset == 0 && m_rep->preCapacity == 0) {
- d = m_rep->buf;
+ BaseString* base = m_rep->baseString();
+ if (!base->isShared() && l <= base->capacity && m_rep == base && m_rep->offset == 0 && base->preCapacity == 0) {
+ d = base->buf;
m_rep->_hash = 0;
m_rep->len = l;
} else {
@@ -1413,12 +1520,24 @@ uint32_t UString::toStrictUInt32(bool* ok) const
int UString::find(const UString& f, int pos) const
{
- int sz = size();
int fsz = f.size();
- if (sz < fsz)
- return -1;
+
if (pos < 0)
pos = 0;
+
+ if (fsz == 1) {
+ UChar ch = f[0];
+ const UChar* end = data() + size();
+ for (const UChar* c = data() + pos; c < end; c++) {
+ if (*c == ch)
+ return static_cast<int>(c - data());
+ }
+ return -1;
+ }
+
+ int sz = size();
+ if (sz < fsz)
+ return -1;
if (fsz == 0)
return pos;
const UChar* end = data() + sz - fsz;
@@ -1502,19 +1621,6 @@ UString UString::substr(int pos, int len) const
return UString(Rep::create(m_rep, pos, len));
}
-bool operator==(const UString& s1, const UString& s2)
-{
- int size = s1.size();
- switch (size) {
- case 0:
- return !s2.size();
- case 1:
- return s2.size() == 1 && s1.data()[0] == s2.data()[0];
- default:
- return s2.size() == size && memcmp(s1.data(), s2.data(), size * sizeof(UChar)) == 0;
- }
-}
-
bool operator==(const UString& s1, const char *s2)
{
if (s2 == 0)
@@ -1626,13 +1732,13 @@ CString UString::UTF8String(bool strict) const
// For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
NEVER_INLINE void UString::makeNull()
{
- m_rep = &Rep::null;
+ m_rep = &Rep::null();
}
// For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
NEVER_INLINE UString::Rep* UString::nullRep()
{
- return &Rep::null;
+ return &Rep::null();
}
} // namespace JSC