Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions Source/WTF/wtf/text/StringBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,154 @@ bool StringBuilder::containsOnlyASCII() const
return StringView { *this }.containsOnlyASCII();
}

#if USE(BUN_JSC_ADDITIONS)

// Gives the builder a buffer of CharacterType that has this capacity and holds the characters of the builder.
template<typename CharacterType> bool StringBuilder::tryReallocateBuffer(unsigned capacity)
{
ASSERT(!hasOverflowed());
ASSERT(capacity);
ASSERT(capacity >= m_length);
ASSERT(sizeof(CharacterType) == sizeof(char16_t) || is8Bit());

if (m_buffer && m_buffer->is8Bit() == (sizeof(CharacterType) == sizeof(Latin1Character))) {
m_string = { }; // The string can hold a reference to the buffer.
if (m_buffer->hasOneRef()) {
std::span<CharacterType> characters;
auto buffer = StringImpl::tryReallocateOrKeep(m_buffer.releaseNonNull(), capacity, characters);
bool reallocated = buffer.has_value();
m_buffer = reallocated ? WTF::move(buffer.value()) : WTF::move(buffer.error());
return reallocated;
}
}

std::span<CharacterType> characters;
auto buffer = StringImpl::tryCreateUninitialized(capacity, characters);
if (!buffer) [[unlikely]]
return false;

if (is8Bit())
StringImpl::copyCharacters(characters, span8());
else
StringImpl::copyCharacters(characters, span16());

m_buffer = WTF::move(buffer);
m_string = { };
return true;
}

// As extendBufferForAppending, for a builder that is not 8-bit when CharacterType is char16_t.
template<typename CharacterType> ALWAYS_INLINE std::span<CharacterType> StringBuilder::tryExtendBufferForAppending(size_t additionalLength)
{
ASSERT(!hasOverflowed());
ASSERT(is8Bit() == (sizeof(CharacterType) == sizeof(Latin1Character)));

// A length that fits in the buffer is a valid length.
if (m_buffer && additionalLength <= m_buffer->length() - m_length) {
m_string = { };
return spanConstCast<CharacterType>(m_buffer->span<CharacterType>().subspan(std::exchange(m_length, m_length + static_cast<unsigned>(additionalLength))));
}
return tryExtendBufferForAppendingSlowCase<CharacterType>(additionalLength);
}

// Also for an 8-bit builder when CharacterType is char16_t. The buffer is 16-bit after that.
template<typename CharacterType> NEVER_INLINE std::span<CharacterType> StringBuilder::tryExtendBufferForAppendingSlowCase(size_t additionalLength)
{
ASSERT(!hasOverflowed());
ASSERT(additionalLength);

constexpr unsigned maxLength = StringImpl::maxValidLength<CharacterType>();
if (m_length > maxLength || additionalLength > maxLength - m_length) [[unlikely]]
return { };
unsigned requiredLength = m_length + static_cast<unsigned>(additionalLength);

// expandedCapacity() can return more than the longest string of CharacterType.
unsigned newCapacity = std::min(expandedCapacity(capacity(), requiredLength), maxLength);
// The buffer does not have to grow by its own size. Each capacity that is refused halves what the next one
// adds to the required length, so the buffer still grows by a part of its size when it can.
for (unsigned spareCapacity = newCapacity - requiredLength; !tryReallocateBuffer<CharacterType>(requiredLength + spareCapacity); spareCapacity /= 2) {
if (!spareCapacity) [[unlikely]]
return { };
}

return spanConstCast<CharacterType>(m_buffer->span<CharacterType>().subspan(std::exchange(m_length, requiredLength)));
}

bool StringBuilder::tryAppend(std::span<const Latin1Character> characters)
{
if (hasOverflowed()) [[unlikely]]
return false;
if (characters.empty())
return true;

if (is8Bit()) {
auto destination = tryExtendBufferForAppending<Latin1Character>(characters.size());
if (!destination.data()) [[unlikely]]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return false;
StringImpl::copyCharacters(destination, characters);
return true;
}

auto destination = tryExtendBufferForAppending<char16_t>(characters.size());
if (!destination.data()) [[unlikely]]
return false;
StringImpl::copyCharacters(destination, characters);
return true;
}

bool StringBuilder::tryAppend(std::span<const char16_t> characters)
{
if (hasOverflowed()) [[unlikely]]
return false;
if (characters.empty())
return true;

std::span<char16_t> destination;
if (is8Bit()) {
// As in append(), one Latin-1 character keeps an 8-bit builder 8-bit.
if (characters.size() == 1 && isLatin1(characters[0])) {
auto destination8 = tryExtendBufferForAppending<Latin1Character>(1);
if (!destination8.data()) [[unlikely]]
return false;
destination8[0] = static_cast<Latin1Character>(characters[0]);
return true;
}
destination = tryExtendBufferForAppendingSlowCase<char16_t>(characters.size());
} else
destination = tryExtendBufferForAppending<char16_t>(characters.size());

if (!destination.data()) [[unlikely]]
return false;
StringImpl::copyCharacters(destination, characters);
return true;
}

bool StringBuilder::tryReserveCapacity(unsigned newCapacity)
{
if (hasOverflowed())
return false;
if (newCapacity <= (m_buffer ? m_buffer->length() : m_length))
return true;

if (is8Bit())
return tryReallocateBuffer<Latin1Character>(newCapacity);
return tryReallocateBuffer<char16_t>(newCapacity);
}

String StringBuilder::tryToString()
{
if (hasOverflowed())
return { };

if (m_string.isNull()) {
// A buffer that cannot shrink stays as it is, and the string is a part of it.
if (m_length && shouldShrinkToFit() && (is8Bit() ? tryReallocateBuffer<Latin1Character>(m_length) : tryReallocateBuffer<char16_t>(m_length)))
m_string = std::exchange(m_buffer, nullptr);
reifyString();
Comment thread
robobun marked this conversation as resolved.
Resolved
}
return m_string;
}

#endif // USE(BUN_JSC_ADDITIONS)

} // namespace WTF
47 changes: 47 additions & 0 deletions Source/WTF/wtf/text/StringBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ class StringBuilder {

WTF_EXPORT_PRIVATE bool NODELETE containsOnlyASCII() const;

#if USE(BUN_JSC_ADDITIONS)
// For these, a length that no string can have and a buffer that the allocator refuses are not an overflow
// and not a crash, whatever the overflow policy is. After a false or a null return the builder holds the
// characters it held before the call.
bool tryAppend(const String&);
bool tryAppend(StringView);
Comment thread
robobun marked this conversation as resolved.
Resolved
bool tryAppend(ASCIILiteral);
bool tryAppend(const AtomString& string) { return tryAppend(string.string()); }
WTF_EXPORT_PRIVATE bool tryAppend(std::span<const Latin1Character>);
WTF_EXPORT_PRIVATE bool tryAppend(std::span<const char16_t>);
WTF_EXPORT_PRIVATE bool tryReserveCapacity(unsigned newCapacity);
// Null only for a builder that append() or reserveCapacity() made overflow.
WTF_EXPORT_PRIVATE String tryToString();
#endif

private:
static unsigned expandedCapacity(unsigned capacity, unsigned requiredCapacity);

Expand All @@ -115,6 +130,12 @@ class StringBuilder {

WTF_EXPORT_PRIVATE void reifyString() const;

#if USE(BUN_JSC_ADDITIONS)
template<typename CharacterType> bool tryReallocateBuffer(unsigned capacity);
template<typename CharacterType> std::span<CharacterType> tryExtendBufferForAppending(size_t additionalLength);
template<typename CharacterType> std::span<CharacterType> tryExtendBufferForAppendingSlowCase(size_t additionalLength);
#endif

void appendFromAdapters() { /* empty base case */ }
template<typename StringTypeAdapter, typename... StringTypeAdapters> void appendFromAdaptersSlow(const StringTypeAdapter&, const StringTypeAdapters&...);
template<typename StringTypeAdapter> void appendFromAdapterSlow(const StringTypeAdapter&);
Expand Down Expand Up @@ -227,6 +248,32 @@ inline void StringBuilder::append(ASCIILiteral string)
append(string.span8());
}

#if USE(BUN_JSC_ADDITIONS)
inline bool StringBuilder::tryAppend(const String& string)
{
// As in append(const String&), an empty builder that has no buffer retains the string.
if (!m_length && !m_buffer) {
m_string = string;
m_length = string.length();
return true;
}

return tryAppend(StringView { string });
}

inline bool StringBuilder::tryAppend(StringView string)
{
if (string.is8Bit())
return tryAppend(string.span8());
return tryAppend(string.span16());
}

inline bool StringBuilder::tryAppend(ASCIILiteral string)
{
return tryAppend(string.span8());
}
#endif

inline void StringBuilder::appendSubstring(const String& string, unsigned offset, unsigned length)
{
append(StringView { string }.substring(offset, length));
Expand Down
36 changes: 36 additions & 0 deletions Source/WTF/wtf/text/StringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,42 @@ std::expected<Ref<StringImpl>, UTF8ConversionError> StringImpl::tryReallocate(Re
return reallocateInternal(WTF::move(originalString), length, data);
}

#if USE(BUN_JSC_ADDITIONS)
template<typename CharacterType> inline std::expected<Ref<StringImpl>, Ref<StringImpl>> StringImpl::reallocateOrKeepInternal(Ref<StringImpl>&& originalString, unsigned length, std::span<CharacterType>& data)
{
ASSERT(originalString->hasOneRef());
ASSERT(originalString->bufferOwnership() == BufferInternal);
ASSERT(length);

if (!isValidLength<CharacterType>(length))
return makeUnexpected(WTF::move(originalString));

unsigned originalLength = originalString->length();
originalString->~StringImpl();
SUPPRESS_UNCOUNTED_LOCAL auto* rawPointer = &originalString.leakRef();
SUPPRESS_UNCOUNTED_LOCAL auto* string = static_cast<StringImpl*>(StringImplMalloc::tryRealloc(rawPointer, allocationSize<CharacterType>(length)));
if (!string) {
// The block is where it was, and its characters follow the header. A new header makes it the string it was.
return makeUnexpected(constructInternal<CharacterType>(*rawPointer, originalLength));
}

data = unsafeMakeSpan(string->tailPointer<CharacterType>(), length);
return constructInternal<CharacterType>(*string, length);
}

std::expected<Ref<StringImpl>, Ref<StringImpl>> StringImpl::tryReallocateOrKeep(Ref<StringImpl>&& originalString, unsigned length, std::span<Latin1Character>& data)
{
ASSERT(originalString->is8Bit());
return reallocateOrKeepInternal(WTF::move(originalString), length, data);
}

std::expected<Ref<StringImpl>, Ref<StringImpl>> StringImpl::tryReallocateOrKeep(Ref<StringImpl>&& originalString, unsigned length, std::span<char16_t>& data)
{
ASSERT(!originalString->is8Bit());
return reallocateOrKeepInternal(WTF::move(originalString), length, data);
}
#endif

template<typename CharacterType> inline Ref<StringImpl> StringImpl::createInternal(std::span<const CharacterType> characters)
{
if (characters.empty())
Expand Down
20 changes: 18 additions & 2 deletions Source/WTF/wtf/text/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class StringImpl : private StringImplShape, public NoVirtualDestructorBase {

// Prefer using isValidLength over MaxLength when the character type is known.
template<typename> static constexpr bool isValidLength(size_t);
// The longest length isValidLength accepts. For char16_t it is less than MaxLength.
template<typename> static constexpr unsigned maxValidLength();

static constexpr unsigned MaxLength = StringImplShape::MaxLength;

Expand Down Expand Up @@ -301,6 +303,11 @@ class StringImpl : private StringImplShape, public NoVirtualDestructorBase {
static Ref<StringImpl> reallocate(Ref<StringImpl>&& originalString, unsigned length, char16_t*& data);
static std::expected<Ref<StringImpl>, UTF8ConversionError> tryReallocate(Ref<StringImpl>&& originalString, unsigned length, Latin1Character*& data);
static std::expected<Ref<StringImpl>, UTF8ConversionError> tryReallocate(Ref<StringImpl>&& originalString, unsigned length, char16_t*& data);
#if USE(BUN_JSC_ADDITIONS)
// As tryReallocate, for a length that is not 0. A failure gives the original string back: the same characters, the same length and one owner.
static std::expected<Ref<StringImpl>, Ref<StringImpl>> tryReallocateOrKeep(Ref<StringImpl>&& originalString, unsigned length, std::span<Latin1Character>& data);
static std::expected<Ref<StringImpl>, Ref<StringImpl>> tryReallocateOrKeep(Ref<StringImpl>&& originalString, unsigned length, std::span<char16_t>& data);
#endif

static constexpr unsigned flagsOffset() { return OBJECT_OFFSETOF(StringImpl, m_hashAndFlags); }
static constexpr unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
Expand Down Expand Up @@ -589,6 +596,9 @@ class StringImpl : private StringImplShape, public NoVirtualDestructorBase {
template<typename CharacterType> static Ref<StringImpl> createUninitializedInternal(size_t, std::span<CharacterType>&);
template<typename CharacterType> static Ref<StringImpl> createUninitializedInternalNonEmpty(size_t, std::span<CharacterType>&);
template<typename CharacterType> static std::expected<Ref<StringImpl>, UTF8ConversionError> reallocateInternal(Ref<StringImpl>&&, unsigned, CharacterType*&);
#if USE(BUN_JSC_ADDITIONS)
template<typename CharacterType> static std::expected<Ref<StringImpl>, Ref<StringImpl>> reallocateOrKeepInternal(Ref<StringImpl>&&, unsigned, std::span<CharacterType>&);
#endif
template<typename CharacterType> static Ref<StringImpl> createInternal(std::span<const CharacterType>);
WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
Ref<StringImpl> convertToUppercaseWithoutLocaleUpconvert();
Expand Down Expand Up @@ -1263,10 +1273,16 @@ template<typename T> inline size_t StringImpl::allocationSize(Checked<size_t> ta
}

template<typename CharacterType>
inline constexpr bool StringImpl::isValidLength(size_t length)
inline constexpr unsigned StringImpl::maxValidLength()
{
// In order to not overflow the unsigned length, the check for (std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) is needed when sizeof(CharacterType) == 2.
constexpr size_t max = std::min(static_cast<size_t>(MaxLength), (std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharacterType));
return static_cast<unsigned>(std::min(static_cast<size_t>(MaxLength), (std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharacterType)));
}

template<typename CharacterType>
inline constexpr bool StringImpl::isValidLength(size_t length)
{
constexpr size_t max = maxValidLength<CharacterType>();
return length <= max;
}

Expand Down
Loading
Loading