diff options
Diffstat (limited to 'JavaScriptCore/runtime/UString.cpp')
-rw-r--r-- | JavaScriptCore/runtime/UString.cpp | 92 |
1 files changed, 30 insertions, 62 deletions
diff --git a/JavaScriptCore/runtime/UString.cpp b/JavaScriptCore/runtime/UString.cpp index 9977f12..118751e 100644 --- a/JavaScriptCore/runtime/UString.cpp +++ b/JavaScriptCore/runtime/UString.cpp @@ -32,14 +32,12 @@ #include <ctype.h> #include <float.h> #include <limits.h> -#include <limits> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <wtf/ASCIICType.h> #include <wtf/Assertions.h> #include <wtf/MathExtras.h> -#include <wtf/StringExtras.h> #include <wtf/Vector.h> #include <wtf/unicode/UTF8.h> @@ -70,20 +68,20 @@ static const int minLengthToShare = 10; 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); } -static inline PossiblyNull<UChar*> allocChars(size_t length) +static inline UChar* allocChars(size_t length) { ASSERT(length); if (length > maxUChars()) return 0; - return tryFastMalloc(sizeof(UChar) * length); + return static_cast<UChar*>(tryFastMalloc(sizeof(UChar) * length)); } -static inline PossiblyNull<UChar*> reallocChars(UChar* buffer, size_t length) +static inline UChar* reallocChars(UChar* buffer, size_t length) { ASSERT(length); if (length > maxUChars()) return 0; - return tryFastRealloc(buffer, sizeof(UChar) * length); + return static_cast<UChar*>(tryFastRealloc(buffer, sizeof(UChar) * length)); } static inline void copyChars(UChar* destination, const UChar* source, unsigned numCharacters) @@ -482,7 +480,8 @@ static inline bool expandCapacity(UString::Rep* rep, int requiredLength) if (requiredLength > base->capacity) { size_t newCapacity = expandedSize(requiredLength, base->preCapacity); UChar* oldBuf = base->buf; - if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) { + base->buf = reallocChars(base->buf, newCapacity); + if (!base->buf) { base->buf = oldBuf; return false; } @@ -513,7 +512,8 @@ bool UString::Rep::reserveCapacity(int capacity) size_t newCapacity = expandedSize(capacity, base->preCapacity); UChar* oldBuf = base->buf; - if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) { + base->buf = reallocChars(base->buf, newCapacity); + if (!base->buf) { base->buf = oldBuf; return false; } @@ -540,8 +540,8 @@ void UString::expandPreCapacity(int requiredPreCap) size_t newCapacity = expandedSize(requiredPreCap, base->capacity); int delta = newCapacity - base->capacity - base->preCapacity; - UChar* newBuf; - if (!allocChars(newCapacity).getValue(newBuf)) { + UChar* newBuf = allocChars(newCapacity); + if (!newBuf) { makeNull(); return; } @@ -566,8 +566,8 @@ static PassRefPtr<UString::Rep> createRep(const char* c) return &UString::Rep::empty(); size_t length = strlen(c); - UChar* d; - if (!allocChars(length).getValue(d)) + UChar* d = allocChars(length); + if (!d) return &UString::Rep::null(); else { for (size_t i = 0; i < length; i++) @@ -656,8 +656,8 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re } else { // 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; - if (!allocChars(newCapacity).getValue(d)) + UChar* d = allocChars(newCapacity); + if (!d) rep = &UString::Rep::null(); else { copyChars(d, rep->data(), thisSize); @@ -712,8 +712,8 @@ static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Re } else { // 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; - if (!allocChars(newCapacity).getValue(d)) + UChar* d = allocChars(newCapacity); + if (!d) rep = &UString::Rep::null(); else { copyChars(d, rep->data(), thisSize); @@ -800,8 +800,8 @@ PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b) // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string size_t newCapacity = expandedSize(length, 0); - UChar* d; - if (!allocChars(newCapacity).getValue(d)) + UChar* d = allocChars(newCapacity); + if (!d) return 0; copyChars(d, a->data(), aSize); copyChars(d + aSize, b->data(), bSize); @@ -942,39 +942,6 @@ UString UString::from(int i) return UString(p, static_cast<int>(end - p)); } -UString UString::from(long long i) -{ - UChar buf[1 + sizeof(i) * 3]; - UChar* end = buf + sizeof(buf) / sizeof(UChar); - UChar* p = end; - - if (i == 0) - *--p = '0'; - else if (i == std::numeric_limits<long long>::min()) { - char minBuf[1 + sizeof(i) * 3]; -#if PLATFORM(WIN_OS) - snprintf(minBuf, sizeof(minBuf) - 1, "%I64d", std::numeric_limits<long long>::min()); -#else - snprintf(minBuf, sizeof(minBuf) - 1, "%lld", std::numeric_limits<long long>::min()); -#endif - return UString(minBuf); - } else { - bool negative = false; - if (i < 0) { - negative = true; - i = -i; - } - while (i) { - *--p = static_cast<unsigned short>((i % 10) + '0'); - i /= 10; - } - if (negative) - *--p = '-'; - } - - return UString(p, static_cast<int>(end - p)); -} - UString UString::from(unsigned int u) { UChar buf[sizeof(u) * 3]; @@ -1109,8 +1076,8 @@ UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, in if (totalLength == 0) return ""; - UChar* buffer; - if (!allocChars(totalLength).getValue(buffer)) + UChar* buffer = allocChars(totalLength); + if (!buffer) return null(); int maxCount = max(rangeCount, separatorCount); @@ -1138,8 +1105,8 @@ UString UString::replaceRange(int rangeStart, int rangeLength, const UString& re if (totalLength == 0) return ""; - UChar* buffer; - if (!allocChars(totalLength).getValue(buffer)) + UChar* buffer = allocChars(totalLength); + if (!buffer) return null(); copyChars(buffer, data(), rangeStart); @@ -1186,8 +1153,8 @@ UString& UString::append(const UString &t) } else { // 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; - if (!allocChars(newCapacity).getValue(d)) + UChar* d = allocChars(newCapacity); + if (!d) makeNull(); else { copyChars(d, data(), thisSize); @@ -1239,8 +1206,8 @@ UString& UString::append(UChar c) if (length == 0) { // this is empty - must make a new m_rep because we don't want to pollute the shared empty one size_t newCapacity = expandedSize(1, 0); - UChar* d; - if (!allocChars(newCapacity).getValue(d)) + UChar* d = allocChars(newCapacity); + if (!d) makeNull(); else { d[0] = c; @@ -1267,8 +1234,8 @@ UString& UString::append(UChar c) } else { // 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; - if (!allocChars(newCapacity).getValue(d)) + UChar* d = allocChars(newCapacity); + if (!d) makeNull(); else { copyChars(d, data(), length); @@ -1346,7 +1313,8 @@ UString& UString::operator=(const char* c) m_rep->_hash = 0; m_rep->len = l; } else { - if (!allocChars(l).getValue(d)) { + d = allocChars(l); + if (!d) { makeNull(); return *this; } |