#include "config.h"
#include "StringBuilder.h"
#include "IntegerToStringConversion.h"
#include "MathExtras.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() const
{
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.get();
else
m_string = StringImpl::createSubstringSharingImpl(*m_buffer, 0, m_length);
}
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;
ASSERT(m_buffer->length() >= m_length);
return;
}
ASSERT(!m_string.isEmpty());
ASSERT(m_length == m_string.length());
ASSERT(newSize < m_string.length());
m_length = newSize;
m_string = StringImpl::createSubstringSharingImpl(*m_string.impl(), 0, newSize);
}
void StringBuilder::allocateBuffer(const LChar* currentCharacters, unsigned requiredLength)
{
ASSERT(m_is8Bit);
auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters8);
memcpy(m_bufferCharacters8, currentCharacters, static_cast<size_t>(m_length) * sizeof(LChar));
m_buffer = WTFMove(buffer);
m_string = String();
ASSERT(m_buffer->length() == requiredLength);
}
void StringBuilder::allocateBuffer(const UChar* currentCharacters, unsigned requiredLength)
{
ASSERT(!m_is8Bit);
auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
memcpy(m_bufferCharacters16, currentCharacters, static_cast<size_t>(m_length) * sizeof(UChar));
m_buffer = WTFMove(buffer);
m_string = String();
ASSERT(m_buffer->length() == requiredLength);
}
void StringBuilder::allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength)
{
ASSERT(m_is8Bit);
ASSERT(requiredLength >= m_length);
auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
for (unsigned i = 0; i < m_length; ++i)
m_bufferCharacters16[i] = currentCharacters[i];
m_is8Bit = false;
m_buffer = WTFMove(buffer);
m_string = String();
ASSERT(m_buffer->length() == requiredLength);
}
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.releaseNonNull(), requiredLength, m_bufferCharacters8);
else
allocateBuffer(m_buffer->characters8(), requiredLength);
ASSERT(m_buffer->length() == 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.releaseNonNull(), requiredLength, m_bufferCharacters16);
else
allocateBuffer(m_buffer->characters16(), requiredLength);
ASSERT(m_buffer->length() == 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);
}
}
ASSERT(!newCapacity || m_buffer->length() >= newCapacity);
}
template <typename CharType>
ALWAYS_INLINE CharType* StringBuilder::appendUninitialized(unsigned length)
{
ASSERT(length);
unsigned requiredLength = length + m_length;
if (requiredLength < length)
CRASH();
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.characters<CharType>() : 0, expandedCapacity(capacity(), requiredLength));
}
CharType* result = getBufferCharacters<CharType>() + m_length;
m_length = requiredLength;
ASSERT(m_buffer->length() >= m_length);
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;
if (requiredLength < length)
CRASH();
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));
ASSERT(m_buffer->length() >= m_length);
}
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++);
}
}
#if USE(CF)
void StringBuilder::append(CFStringRef string)
{
if (auto* characters = CFStringGetCStringPtr(string, kCFStringEncodingISOLatin1)) {
append(reinterpret_cast<const LChar*>(characters), CFStringGetLength(string));
return;
}
append(String(string));
}
#endif
void StringBuilder::appendNumber(int number)
{
numberToStringSigned<StringBuilder>(number, this);
}
void StringBuilder::appendNumber(unsigned int 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);
}
void StringBuilder::appendNumber(double number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
{
NumberToStringBuffer buffer;
append(numberToFixedPrecisionString(number, precision, buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros));
}
void StringBuilder::appendECMAScriptNumber(double number)
{
NumberToStringBuffer buffer;
append(numberToString(number, buffer));
}
void StringBuilder::appendFixedWidthNumber(double number, unsigned decimalPlaces)
{
NumberToStringBuffer buffer;
append(numberToFixedWidthString(number, decimalPlaces, buffer));
}
bool StringBuilder::canShrink() const
{
return m_buffer && m_buffer->length() > (m_length + (m_length >> 2));
}
void StringBuilder::shrinkToFit()
{
if (canShrink()) {
if (m_is8Bit)
reallocateBuffer<LChar>(m_length);
else
reallocateBuffer<UChar>(m_length);
m_string = WTFMove(m_buffer);
}
}
template <typename OutputCharacterType, typename InputCharacterType>
static void appendQuotedJSONStringInternalSlow(OutputCharacterType*& output, const InputCharacterType character)
{
switch (character) {
case '\t':
*output++ = '\\';
*output++ = 't';
break;
case '\r':
*output++ = '\\';
*output++ = 'r';
break;
case '\n':
*output++ = '\\';
*output++ = 'n';
break;
case '\f':
*output++ = '\\';
*output++ = 'f';
break;
case '\b':
*output++ = '\\';
*output++ = 'b';
break;
default:
ASSERT(!(character & 0xFF00));
*output++ = '\\';
*output++ = 'u';
*output++ = '0';
*output++ = '0';
*output++ = upperNibbleToLowercaseASCIIHexDigit(character);
*output++ = lowerNibbleToLowercaseASCIIHexDigit(character);
break;
}
}
template <typename OutputCharacterType, typename InputCharacterType>
static void appendQuotedJSONStringInternal(OutputCharacterType*& output, const InputCharacterType* input, unsigned length)
{
for (const InputCharacterType* end = input + length; input != end; ++input) {
const InputCharacterType character = *input;
if (LIKELY(character != '"' && character != '\\' && character > 0x1F)) {
*output++ = character;
continue;
}
if (character == '"' || character == '\\') {
*output++ = '\\';
*output++ = character;
continue;
}
appendQuotedJSONStringInternalSlow(output, character);
}
}
void StringBuilder::appendQuotedJSONString(const String& string)
{
Checked<unsigned> stringLength = string.length();
Checked<unsigned> maximumCapacityRequired = length();
maximumCapacityRequired += 2 + stringLength * 6;
unsigned allocationSize = maximumCapacityRequired.unsafeGet();
allocationSize = std::max(allocationSize, roundUpToPowerOfTwo(allocationSize));
if (is8Bit() && !string.is8Bit())
allocateBufferUpConvert(m_bufferCharacters8, allocationSize);
else
reserveCapacity(allocationSize);
ASSERT(m_buffer->length() >= allocationSize);
if (is8Bit()) {
ASSERT(string.is8Bit());
LChar* output = m_bufferCharacters8 + m_length;
*output++ = '"';
appendQuotedJSONStringInternal(output, string.characters8(), string.length());
*output++ = '"';
m_length = output - m_bufferCharacters8;
} else {
UChar* output = m_bufferCharacters16 + m_length;
*output++ = '"';
if (string.is8Bit())
appendQuotedJSONStringInternal(output, string.characters8(), string.length());
else
appendQuotedJSONStringInternal(output, string.characters16(), string.length());
*output++ = '"';
m_length = output - m_bufferCharacters16;
}
ASSERT(m_buffer->length() >= m_length);
}
}