This source file includes following definitions.
- expandedCapacity
- reifyString
- reifySubstring
- resize
- allocateBuffer
- allocateBuffer
- allocateBufferUpConvert
- reserveCapacity
- appendUninitialized
- appendUninitializedSlow
- append
- append
- appendNumber
- appendNumber
- appendNumber
- appendNumber
- appendNumber
- appendNumber
- expandLCharToUCharInplace
- appendNumber
- canShrink
- shrinkToFit
#include "config.h"
#include "StringBuilder.h"
#include "IntegerToStringConversion.h"
#include "WTFString.h"
#include "wtf/dtoa.h"
namespace WTF {
static unsigned expandedCapacity(unsigned capacity, unsigned requiredLength)
{
static const unsigned minimumCapacity = 16;
return std::max(requiredLength, std::max(minimumCapacity, capacity * 2));
}
void StringBuilder::reifyString()
{
if (!m_string.isNull()) {
ASSERT(m_string.length() == m_length);
return;
}
if (!m_length) {
m_string = StringImpl::empty();
return;
}
ASSERT(m_buffer && m_length <= m_buffer->length());
if (m_length == m_buffer->length()) {
m_string = m_buffer.release();
return;
}
if (m_buffer->hasOneRef()) {
m_buffer->truncateAssumingIsolated(m_length);
m_string = m_buffer.release();
return;
}
m_string = m_buffer->substring(0, m_length);
}
String StringBuilder::reifySubstring(unsigned position, unsigned length) const
{
ASSERT(m_string.isNull());
ASSERT(m_buffer);
unsigned substringLength = std::min(length, m_length - position);
return m_buffer->substring(position, substringLength);
}
void StringBuilder::resize(unsigned newSize)
{
ASSERT(newSize <= m_length);
if (newSize == m_length)
return;
ASSERT(m_length);
if (m_buffer) {
m_string = String();
if (!m_buffer->hasOneRef()) {
if (m_buffer->is8Bit())
allocateBuffer(m_buffer->characters8(), m_buffer->length());
else
allocateBuffer(m_buffer->characters16(), m_buffer->length());
}
m_length = newSize;
return;
}
ASSERT(!m_string.isEmpty());
ASSERT(m_length == m_string.length());
ASSERT(newSize < m_string.length());
m_length = newSize;
RefPtr<StringImpl> string = m_string.releaseImpl();
if (string->hasOneRef()) {
m_buffer = string;
} else {
m_buffer = string->substring(0, m_length);
}
}
void StringBuilder::allocateBuffer(const LChar* currentCharacters, unsigned requiredLength)
{
ASSERT(m_is8Bit);
RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters8);
memcpy(m_bufferCharacters8, currentCharacters, static_cast<size_t>(m_length) * sizeof(LChar));
m_buffer = buffer.release();
m_string = String();
}
void StringBuilder::allocateBuffer(const UChar* currentCharacters, unsigned requiredLength)
{
ASSERT(!m_is8Bit);
RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
memcpy(m_bufferCharacters16, currentCharacters, static_cast<size_t>(m_length) * sizeof(UChar));
m_buffer = buffer.release();
m_string = String();
}
void StringBuilder::allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength)
{
ASSERT(m_is8Bit);
RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
for (unsigned i = 0; i < m_length; ++i)
m_bufferCharacters16[i] = currentCharacters[i];
m_is8Bit = false;
m_buffer = buffer.release();
m_string = String();
}
template <>
void StringBuilder::reallocateBuffer<LChar>(unsigned requiredLength)
{
m_string = String();
ASSERT(m_is8Bit);
ASSERT(m_buffer->is8Bit());
if (m_buffer->hasOneRef()) {
m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength);
m_bufferCharacters8 = const_cast<LChar*>(m_buffer->characters8());
} else
allocateBuffer(m_buffer->characters8(), requiredLength);
}
template <>
void StringBuilder::reallocateBuffer<UChar>(unsigned requiredLength)
{
m_string = String();
if (m_buffer->is8Bit()) {
allocateBufferUpConvert(m_buffer->characters8(), requiredLength);
} else if (m_buffer->hasOneRef()) {
m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength);
m_bufferCharacters16 = const_cast<UChar*>(m_buffer->characters16());
} else
allocateBuffer(m_buffer->characters16(), requiredLength);
}
void StringBuilder::reserveCapacity(unsigned newCapacity)
{
if (m_buffer) {
if (newCapacity > m_buffer->length()) {
if (m_buffer->is8Bit())
reallocateBuffer<LChar>(newCapacity);
else
reallocateBuffer<UChar>(newCapacity);
}
} else {
if (newCapacity > m_length) {
if (!m_length) {
LChar* nullPlaceholder = 0;
allocateBuffer(nullPlaceholder, newCapacity);
} else if (m_string.is8Bit())
allocateBuffer(m_string.characters8(), newCapacity);
else
allocateBuffer(m_string.characters16(), newCapacity);
}
}
}
template <typename CharType>
ALWAYS_INLINE CharType* StringBuilder::appendUninitialized(unsigned length)
{
ASSERT(length);
unsigned requiredLength = length + m_length;
RELEASE_ASSERT(requiredLength >= length);
if ((m_buffer) && (requiredLength <= m_buffer->length())) {
ASSERT(m_buffer->length() >= m_length);
unsigned currentLength = m_length;
m_string = String();
m_length = requiredLength;
return getBufferCharacters<CharType>() + currentLength;
}
return appendUninitializedSlow<CharType>(requiredLength);
}
template <typename CharType>
CharType* StringBuilder::appendUninitializedSlow(unsigned requiredLength)
{
ASSERT(requiredLength);
if (m_buffer) {
ASSERT(m_buffer->length() >= m_length);
reallocateBuffer<CharType>(expandedCapacity(capacity(), requiredLength));
} else {
ASSERT(m_string.length() == m_length);
allocateBuffer(m_length ? m_string.getCharacters<CharType>() : 0, expandedCapacity(capacity(), requiredLength));
}
CharType* result = getBufferCharacters<CharType>() + m_length;
m_length = requiredLength;
return result;
}
void StringBuilder::append(const UChar* characters, unsigned length)
{
if (!length)
return;
ASSERT(characters);
if (m_is8Bit) {
if (length == 1 && !(*characters & ~0xff)) {
LChar lChar = static_cast<LChar>(*characters);
append(&lChar, 1);
return;
}
unsigned requiredLength = length + m_length;
RELEASE_ASSERT(requiredLength >= length);
if (m_buffer) {
ASSERT(m_buffer->length() >= m_length);
allocateBufferUpConvert(m_buffer->characters8(), expandedCapacity(capacity(), requiredLength));
} else {
ASSERT(m_string.length() == m_length);
allocateBufferUpConvert(m_string.isNull() ? 0 : m_string.characters8(), expandedCapacity(capacity(), requiredLength));
}
memcpy(m_bufferCharacters16 + m_length, characters, static_cast<size_t>(length) * sizeof(UChar));
m_length = requiredLength;
} else
memcpy(appendUninitialized<UChar>(length), characters, static_cast<size_t>(length) * sizeof(UChar));
}
void StringBuilder::append(const LChar* characters, unsigned length)
{
if (!length)
return;
ASSERT(characters);
if (m_is8Bit) {
LChar* dest = appendUninitialized<LChar>(length);
if (length > 8)
memcpy(dest, characters, static_cast<size_t>(length) * sizeof(LChar));
else {
const LChar* end = characters + length;
while (characters < end)
*(dest++) = *(characters++);
}
} else {
UChar* dest = appendUninitialized<UChar>(length);
const LChar* end = characters + length;
while (characters < end)
*(dest++) = *(characters++);
}
}
void StringBuilder::appendNumber(int number)
{
numberToStringSigned<StringBuilder>(number, this);
}
void StringBuilder::appendNumber(unsigned number)
{
numberToStringUnsigned<StringBuilder>(number, this);
}
void StringBuilder::appendNumber(long number)
{
numberToStringSigned<StringBuilder>(number, this);
}
void StringBuilder::appendNumber(unsigned long number)
{
numberToStringUnsigned<StringBuilder>(number, this);
}
void StringBuilder::appendNumber(long long number)
{
numberToStringSigned<StringBuilder>(number, this);
}
void StringBuilder::appendNumber(unsigned long long number)
{
numberToStringUnsigned<StringBuilder>(number, this);
}
static void expandLCharToUCharInplace(UChar* buffer, size_t length)
{
const LChar* sourceEnd = reinterpret_cast<LChar*>(buffer) + length;
UChar* current = buffer + length;
while (current != buffer)
*--current = *--sourceEnd;
}
void StringBuilder::appendNumber(double number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
{
bool truncateTrailingZeros = trailingZerosTruncatingPolicy == TruncateTrailingZeros;
size_t numberLength;
if (m_is8Bit) {
LChar* dest = appendUninitialized<LChar>(NumberToStringBufferLength);
const char* result = numberToFixedPrecisionString(number, precision, reinterpret_cast<char*>(dest), truncateTrailingZeros);
numberLength = strlen(result);
} else {
UChar* dest = appendUninitialized<UChar>(NumberToStringBufferLength);
const char* result = numberToFixedPrecisionString(number, precision, reinterpret_cast<char*>(dest), truncateTrailingZeros);
numberLength = strlen(result);
expandLCharToUCharInplace(dest, numberLength);
}
ASSERT(m_length >= NumberToStringBufferLength);
m_length -= NumberToStringBufferLength;
ASSERT(numberLength <= NumberToStringBufferLength);
m_length += numberLength;
}
bool StringBuilder::canShrink() const
{
return m_buffer && m_buffer->length() > (m_length + (m_length >> 2));
}
void StringBuilder::shrinkToFit()
{
if (!canShrink())
return;
if (m_is8Bit)
reallocateBuffer<LChar>(m_length);
else
reallocateBuffer<UChar>(m_length);
m_string = m_buffer.release();
}
}