X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..2656c66b5b30d5597e842a751c7f19ad6c2fe31a:/runtime/JSStringBuilder.h diff --git a/runtime/JSStringBuilder.h b/runtime/JSStringBuilder.h index 49d4a63..63e51a3 100644 --- a/runtime/JSStringBuilder.h +++ b/runtime/JSStringBuilder.h @@ -28,60 +28,94 @@ #include "ExceptionHelpers.h" #include "JSString.h" -#include "UStringConcatenate.h" -#include "Vector.h" +#include namespace JSC { +// FIXME: Should move the last few callers over from this to WTF::StringBuilder. class JSStringBuilder { public: JSStringBuilder() : m_okay(true) + , m_is8Bit(true) { } - void append(const UChar u) + void append(LChar character) { - m_okay &= buffer.tryAppend(&u, 1); + if (m_is8Bit) { + m_okay &= buffer8.tryAppend(&character, 1); + return; + } + UChar upconvertedCharacter = character; + m_okay &= buffer16.tryAppend(&upconvertedCharacter, 1); } - void append(const char* str) + void append(UChar character) { - append(str, strlen(str)); + if (m_is8Bit) { + if (character < 0x100) { + LChar narrowedCharacter = character; + m_okay &= buffer8.tryAppend(&narrowedCharacter, 1); + return; + } + upConvert(); + } + m_okay &= buffer16.tryAppend(&character, 1); } - void append(const char* str, size_t len) + void append(const char* str) { - m_okay &= buffer.tryReserveCapacity(buffer.size() + len); - for (size_t i = 0; i < len; i++) { - UChar u = static_cast(str[i]); - m_okay &= buffer.tryAppend(&u, 1); - } + append(reinterpret_cast(str), strlen(str)); } - void append(const UChar* str, size_t len) + JSValue build(ExecState* exec) { - m_okay &= buffer.tryAppend(str, len); + if (!m_okay) + return throwOutOfMemoryError(exec); + if (m_is8Bit) { + buffer8.shrinkToFit(); + if (!buffer8.data()) + return throwOutOfMemoryError(exec); + return jsString(exec, String::adopt(buffer8)); + } + buffer16.shrinkToFit(); + if (!buffer16.data()) + return throwOutOfMemoryError(exec); + return jsString(exec, String::adopt(buffer16)); } - void append(const UString& str) +private: + void append(const LChar* characters, size_t length) { - m_okay &= buffer.tryAppend(str.characters(), str.length()); + if (m_is8Bit) { + m_okay &= buffer8.tryAppend(characters, length); + return; + } + // FIXME: There must be a more efficient way of doing this. + m_okay &= buffer16.tryReserveCapacity(buffer16.size() + length); + for (size_t i = 0; i < length; i++) { + UChar upconvertedCharacter = characters[i]; + m_okay &= buffer16.tryAppend(&upconvertedCharacter, 1); + } } - JSValue build(ExecState* exec) + void upConvert() { - if (!m_okay) - return throwOutOfMemoryError(exec); - buffer.shrinkToFit(); - if (!buffer.data()) - return throwOutOfMemoryError(exec); - return jsString(exec, UString::adopt(buffer)); + ASSERT(m_is8Bit); + size_t len = buffer8.size(); + + for (size_t i = 0; i < len; i++) + buffer16.append(buffer8[i]); + + buffer8.clear(); + m_is8Bit = false; } -protected: - Vector buffer; + Vector buffer8; + Vector buffer16; bool m_okay; + bool m_is8Bit; }; template