StringBuilderJSON.cpp [plain text]
#include "config.h"
#include "StringBuilder.h"
#include "WTFString.h"
namespace WTF {
static const constexpr LChar escapedFormsForJSON[0x100] = {
'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',
'b', 't', 'n', 'u', 'f', 'r', 'u', 'u',
'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',
'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',
0, 0, '\"', 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, '\\', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
template<typename OutputCharacterType, typename InputCharacterType>
ALWAYS_INLINE static void appendQuotedJSONStringInternal(OutputCharacterType*& output, const InputCharacterType* input, unsigned length)
{
for (auto* end = input + length; input != end; ++input) {
auto character = *input;
auto escaped = escapedFormsForJSON[character & 0xFF];
if (LIKELY(!escaped || character > 0xFF)) {
*output++ = character;
continue;
}
*output++ = '\\';
*output++ = escaped;
if (UNLIKELY(escaped == 'u')) {
*output++ = '0';
*output++ = '0';
*output++ = upperNibbleToLowercaseASCIIHexDigit(character);
*output++ = lowerNibbleToLowercaseASCIIHexDigit(character);
}
}
}
bool StringBuilder::appendQuotedJSONString(const String& string)
{
Checked<unsigned, RecordOverflow> stringLength = string.length();
Checked<unsigned, RecordOverflow> maximumCapacityRequired = length();
maximumCapacityRequired += 2 + stringLength * 6;
unsigned allocationSize;
if (CheckedState::DidOverflow == maximumCapacityRequired.safeGet(allocationSize))
return false;
allocationSize = std::max(allocationSize, roundUpToPowerOfTwo(allocationSize));
if (allocationSize >= 0x80000000)
return false;
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);
return true;
}
}