2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "RetainPtr.h"
|
2019-05-28 09:53:16 +00:00
|
|
|
#include "Retainable.h"
|
2018-10-16 10:10:01 +00:00
|
|
|
#include "Types.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-07 09:46:22 +00:00
|
|
|
enum ShouldChomp
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
NoChomp,
|
|
|
|
Chomp
|
|
|
|
};
|
2018-11-06 23:19:35 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
class StringImpl : public Retainable<StringImpl> {
|
|
|
|
public:
|
2019-02-25 21:06:55 +00:00
|
|
|
static Retained<StringImpl> create_uninitialized(ssize_t length, char*& buffer);
|
2018-11-06 23:19:35 +00:00
|
|
|
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
|
2019-02-25 21:06:55 +00:00
|
|
|
static RetainPtr<StringImpl> create(const char* cstring, ssize_t length, ShouldChomp = NoChomp);
|
2019-02-25 15:04:08 +00:00
|
|
|
Retained<StringImpl> to_lowercase() const;
|
|
|
|
Retained<StringImpl> to_uppercase() const;
|
2018-10-10 09:53:07 +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-02-25 21:06:55 +00:00
|
|
|
ssize_t length() const { return m_length; }
|
2018-10-10 09:53:07 +00:00
|
|
|
const char* characters() const { return m_characters; }
|
2019-05-28 09:53:16 +00:00
|
|
|
char operator[](ssize_t i) const
|
|
|
|
{
|
|
|
|
ASSERT(i >= 0 && i < m_length);
|
|
|
|
return m_characters[i];
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
unsigned hash() const
|
|
|
|
{
|
|
|
|
if (!m_hasHash)
|
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 09:46:22 +00:00
|
|
|
enum ConstructTheEmptyStringImplTag
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
ConstructTheEmptyStringImpl
|
|
|
|
};
|
|
|
|
explicit StringImpl(ConstructTheEmptyStringImplTag)
|
|
|
|
: m_characters("")
|
|
|
|
{
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-07 09:46:22 +00:00
|
|
|
enum ConstructWithInlineBufferTag
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
ConstructWithInlineBuffer
|
|
|
|
};
|
2019-02-25 21:06:55 +00:00
|
|
|
StringImpl(ConstructWithInlineBufferTag, ssize_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-02-25 21:06:55 +00:00
|
|
|
ssize_t m_length { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
mutable bool m_hasHash { false };
|
|
|
|
const char* m_characters { nullptr };
|
|
|
|
mutable unsigned m_hash { 0 };
|
2018-12-21 01:10:45 +00:00
|
|
|
char m_inline_buffer[0];
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-11 23:56:33 +00:00
|
|
|
inline dword string_hash(const char* characters, int length)
|
|
|
|
{
|
|
|
|
dword hash = 0;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
hash += (dword)characters[i];
|
|
|
|
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;
|