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