2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-03-22 09:12:55 +00:00
|
|
|
#include <AK/Badge.h>
|
2019-09-13 12:37:25 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2020-03-10 08:13:29 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2020-07-27 12:15:37 +00:00
|
|
|
#include <AK/Span.h>
|
2019-06-18 07:26:36 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum ShouldChomp {
|
2019-05-28 09:53:16 +00:00
|
|
|
NoChomp,
|
|
|
|
Chomp
|
|
|
|
};
|
2018-11-06 23:19:35 +00:00
|
|
|
|
2021-07-11 11:23:13 +00:00
|
|
|
size_t allocation_size_for_stringimpl(size_t length);
|
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
class StringImpl : public RefCounted<StringImpl> {
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2023-02-19 21:59:26 +00:00
|
|
|
static NonnullRefPtr<StringImpl const> create_uninitialized(size_t length, char*& buffer);
|
|
|
|
static RefPtr<StringImpl const> create(char const* cstring, ShouldChomp = NoChomp);
|
|
|
|
static RefPtr<StringImpl const> create(char const* cstring, size_t length, ShouldChomp = NoChomp);
|
|
|
|
static RefPtr<StringImpl const> create(ReadonlyBytes, ShouldChomp = NoChomp);
|
|
|
|
static RefPtr<StringImpl const> create_lowercased(char const* cstring, size_t length);
|
|
|
|
static RefPtr<StringImpl const> create_uppercased(char const* cstring, size_t length);
|
|
|
|
|
|
|
|
NonnullRefPtr<StringImpl const> to_lowercase() const;
|
|
|
|
NonnullRefPtr<StringImpl const> to_uppercase() const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-18 07:26:36 +00:00
|
|
|
void operator delete(void* ptr)
|
|
|
|
{
|
2021-07-11 11:23:13 +00:00
|
|
|
kfree_sized(ptr, allocation_size_for_stringimpl(static_cast<StringImpl*>(ptr)->m_length));
|
2019-06-18 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
static StringImpl& the_empty_stringimpl();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
~StringImpl();
|
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t length() const { return m_length; }
|
2020-08-23 10:56:46 +00:00
|
|
|
// Includes NUL-terminator.
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* characters() const { return &m_inline_buffer[0]; }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE ReadonlyBytes bytes() const { return { characters(), length() }; }
|
2021-06-06 07:05:49 +00:00
|
|
|
ALWAYS_INLINE StringView view() const { return { characters(), length() }; }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
char const& operator[](size_t i) const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(i < m_length);
|
2019-06-20 11:21:56 +00:00
|
|
|
return characters()[i];
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(StringImpl const& other) const
|
2020-10-05 15:15:49 +00:00
|
|
|
{
|
|
|
|
if (length() != other.length())
|
|
|
|
return false;
|
2021-11-06 20:12:16 +00:00
|
|
|
return __builtin_memcmp(characters(), other.characters(), length()) == 0;
|
2020-10-05 15:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
unsigned hash() const
|
|
|
|
{
|
2019-06-20 11:21:56 +00:00
|
|
|
if (!m_has_hash)
|
2018-12-21 01:10:45 +00:00
|
|
|
compute_hash();
|
2018-10-10 09:53:07 +00:00
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:05:19 +00:00
|
|
|
unsigned existing_hash() const
|
|
|
|
{
|
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:56:53 +00:00
|
|
|
unsigned case_insensitive_hash() const;
|
|
|
|
|
2020-03-22 09:12:55 +00:00
|
|
|
bool is_fly() const { return m_fly; }
|
2023-01-09 00:23:00 +00:00
|
|
|
void set_fly(Badge<DeprecatedFlyString>, bool fly) const { m_fly = fly; }
|
2020-03-22 09:12:55 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum ConstructTheEmptyStringImplTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
ConstructTheEmptyStringImpl
|
|
|
|
};
|
|
|
|
explicit StringImpl(ConstructTheEmptyStringImplTag)
|
2020-03-22 09:12:55 +00:00
|
|
|
: m_fly(true)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-20 11:21:56 +00:00
|
|
|
m_inline_buffer[0] = '\0';
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum ConstructWithInlineBufferTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
ConstructWithInlineBuffer
|
|
|
|
};
|
2019-12-09 16:45:40 +00:00
|
|
|
StringImpl(ConstructWithInlineBufferTag, size_t length);
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
void compute_hash() const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t m_length { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
mutable unsigned m_hash { 0 };
|
2019-06-20 11:21:56 +00:00
|
|
|
mutable bool m_has_hash { false };
|
2020-03-22 09:12:55 +00:00
|
|
|
mutable bool m_fly { false };
|
2018-12-21 01:10:45 +00:00
|
|
|
char m_inline_buffer[0];
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2021-07-11 11:23:13 +00:00
|
|
|
inline size_t allocation_size_for_stringimpl(size_t length)
|
|
|
|
{
|
|
|
|
return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char);
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:24:46 +00:00
|
|
|
template<>
|
|
|
|
struct Formatter<StringImpl> : Formatter<StringView> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, StringImpl const& value)
|
2020-10-07 11:24:46 +00:00
|
|
|
{
|
2021-11-16 00:15:21 +00:00
|
|
|
return Formatter<StringView>::format(builder, { value.characters(), value.length() });
|
2020-10-07 11:24:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-11-06 23:19:35 +00:00
|
|
|
using AK::Chomp;
|
2020-11-28 14:37:44 +00:00
|
|
|
using AK::NoChomp;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::StringImpl;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|