2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
#include <AK/ByteStringImpl.h>
|
2021-05-14 10:21:50 -03:00
|
|
|
#include <AK/StringHash.h>
|
2020-03-08 08:34:33 -03:00
|
|
|
#include <AK/kmalloc.h>
|
2018-12-28 00:09:45 -02:00
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
namespace AK {
|
|
|
|
|
|
2026-01-23 06:31:09 -03:00
|
|
|
// Static storage for the empty ByteStringImpl, avoiding heap allocation
|
|
|
|
|
// and runtime initialization. The struct layout matches ByteStringImpl
|
|
|
|
|
// with space for the NUL terminator in the inline buffer.
|
|
|
|
|
struct EmptyByteStringImpl {
|
|
|
|
|
// Members from AtomicRefCountedBase
|
|
|
|
|
Atomic<unsigned> ref_count { 1 };
|
|
|
|
|
|
|
|
|
|
// Members from ByteStringImpl
|
|
|
|
|
size_t length { 0 };
|
|
|
|
|
unsigned hash { 0 };
|
|
|
|
|
bool has_hash { false };
|
|
|
|
|
char inline_buffer[1] { '\0' };
|
|
|
|
|
|
|
|
|
|
constexpr EmptyByteStringImpl() = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The inline_buffer[1] fits within the trailing padding of ByteStringImpl's layout.
|
|
|
|
|
static_assert(sizeof(EmptyByteStringImpl) == sizeof(ByteStringImpl));
|
|
|
|
|
static_assert(alignof(EmptyByteStringImpl) == alignof(ByteStringImpl));
|
|
|
|
|
|
|
|
|
|
static constinit EmptyByteStringImpl s_the_empty_stringimpl;
|
2018-10-22 08:10:08 -03:00
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
ByteStringImpl& ByteStringImpl::the_empty_stringimpl()
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2026-01-23 06:31:09 -03:00
|
|
|
return reinterpret_cast<ByteStringImpl&>(s_the_empty_stringimpl);
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
ByteStringImpl::ByteStringImpl(ConstructWithInlineBufferTag, size_t length)
|
2018-12-28 00:09:45 -02:00
|
|
|
: m_length(length)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
ByteStringImpl::~ByteStringImpl() = default;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create_uninitialized(size_t length, char*& buffer)
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(length);
|
2019-01-31 14:31:23 -02:00
|
|
|
void* slot = kmalloc(allocation_size_for_stringimpl(length));
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(slot);
|
2025-04-06 11:26:37 -03:00
|
|
|
auto new_stringimpl = adopt_ref(*new (slot) ByteStringImpl(ConstructWithInlineBuffer, length));
|
2019-06-20 08:21:56 -03:00
|
|
|
buffer = const_cast<char*>(new_stringimpl->characters());
|
2018-10-10 06:53:07 -03:00
|
|
|
buffer[length] = '\0';
|
2019-01-31 14:31:23 -02:00
|
|
|
return new_stringimpl;
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp)
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2019-04-07 15:19:16 -03:00
|
|
|
if (should_chomp) {
|
|
|
|
|
while (length) {
|
|
|
|
|
char last_ch = cstring[length - 1];
|
|
|
|
|
if (!last_ch || last_ch == '\n' || last_ch == '\r')
|
|
|
|
|
--length;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 13:11:28 -02:00
|
|
|
if (!length)
|
|
|
|
|
return the_empty_stringimpl();
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
char* buffer;
|
2019-01-31 14:31:23 -02:00
|
|
|
auto new_stringimpl = create_uninitialized(length, buffer);
|
2018-10-10 06:53:07 -03:00
|
|
|
memcpy(buffer, cstring, length * sizeof(char));
|
|
|
|
|
|
2019-01-31 14:31:23 -02:00
|
|
|
return new_stringimpl;
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create(char const* cstring, ShouldChomp shouldChomp)
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2023-10-10 08:30:58 -03:00
|
|
|
if (!cstring || !*cstring)
|
2021-10-25 08:16:59 -03:00
|
|
|
return the_empty_stringimpl();
|
|
|
|
|
|
2018-11-06 21:19:35 -02:00
|
|
|
return create(cstring, strlen(cstring), shouldChomp);
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp)
|
2020-08-05 05:37:34 -03:00
|
|
|
{
|
2025-04-06 11:26:37 -03:00
|
|
|
return ByteStringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp);
|
2020-08-05 05:37:34 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
unsigned ByteStringImpl::case_insensitive_hash() const
|
2022-02-18 18:56:53 -03:00
|
|
|
{
|
|
|
|
|
return case_insensitive_string_hash(characters(), length());
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
void ByteStringImpl::compute_hash() const
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2019-03-11 20:56:33 -03:00
|
|
|
if (!length())
|
2018-10-10 06:53:07 -03:00
|
|
|
m_hash = 0;
|
2019-03-11 20:56:33 -03:00
|
|
|
else
|
2019-06-20 08:21:56 -03:00
|
|
|
m_hash = string_hash(characters(), m_length);
|
|
|
|
|
m_has_hash = true;
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|