2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-21 16:45:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2019-06-21 16:58:45 +00:00
|
|
|
#include <AK/RefCounted.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
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
class StringImpl : public RefCounted<StringImpl> {
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2019-06-21 16:37:47 +00:00
|
|
|
static NonnullRefPtr<StringImpl> create_uninitialized(int length, char*& buffer);
|
|
|
|
static RefPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
|
|
|
|
static RefPtr<StringImpl> create(const char* cstring, int length, ShouldChomp = NoChomp);
|
|
|
|
NonnullRefPtr<StringImpl> to_lowercase() const;
|
|
|
|
NonnullRefPtr<StringImpl> to_uppercase() const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-18 07:26:36 +00:00
|
|
|
void operator delete(void* ptr)
|
|
|
|
{
|
|
|
|
kfree(ptr);
|
|
|
|
}
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
static StringImpl& the_empty_stringimpl();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
~StringImpl();
|
|
|
|
|
2019-06-20 11:21:56 +00:00
|
|
|
int length() const { return m_length; }
|
|
|
|
const char* characters() const { return &m_inline_buffer[0]; }
|
|
|
|
char operator[](int i) const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(i >= 0 && 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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum ConstructTheEmptyStringImplTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
ConstructTheEmptyStringImpl
|
|
|
|
};
|
|
|
|
explicit StringImpl(ConstructTheEmptyStringImplTag)
|
|
|
|
{
|
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-06-20 11:21:56 +00:00
|
|
|
StringImpl(ConstructWithInlineBufferTag, int 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-06-20 11:21:56 +00:00
|
|
|
int 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 };
|
2018-12-21 01:10:45 +00:00
|
|
|
char m_inline_buffer[0];
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
inline constexpr u32 string_hash(const char* characters, int length)
|
2019-03-11 23:56:33 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 hash = 0;
|
2019-03-11 23:56:33 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2019-07-03 19:17:35 +00:00
|
|
|
hash += (u32)characters[i];
|
2019-03-11 23:56:33 +00:00
|
|
|
hash += (hash << 10);
|
|
|
|
hash ^= (hash >> 6);
|
|
|
|
}
|
|
|
|
hash += hash << 3;
|
|
|
|
hash ^= hash >> 11;
|
|
|
|
hash += hash << 15;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 23:19:35 +00:00
|
|
|
using AK::Chomp;
|
2019-03-11 23:56:33 +00:00
|
|
|
using AK::string_hash;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::StringImpl;
|