2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Retainable.h"
|
|
|
|
#include "RetainPtr.h"
|
2018-10-16 10:10:01 +00:00
|
|
|
#include "Types.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class StringImpl : public Retainable<StringImpl> {
|
|
|
|
public:
|
2018-10-16 10:10:01 +00:00
|
|
|
static RetainPtr<StringImpl> createUninitialized(size_t length, char*& buffer);
|
2018-10-10 09:53:07 +00:00
|
|
|
static RetainPtr<StringImpl> create(const char* cstring);
|
|
|
|
static RetainPtr<StringImpl> create(const char* cstring, size_t length);
|
|
|
|
RetainPtr<StringImpl> toLowercase() const;
|
|
|
|
RetainPtr<StringImpl> toUppercase() const;
|
|
|
|
|
|
|
|
static StringImpl& theEmptyStringImpl();
|
2018-10-22 11:10:08 +00:00
|
|
|
static void initializeGlobals();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
~StringImpl();
|
|
|
|
|
2018-10-16 10:10:01 +00:00
|
|
|
size_t length() const { return m_length; }
|
2018-10-10 09:53:07 +00:00
|
|
|
const char* characters() const { return m_characters; }
|
2018-10-16 10:10:01 +00:00
|
|
|
char operator[](size_t i) const { ASSERT(i < m_length); return m_characters[i]; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
unsigned hash() const
|
|
|
|
{
|
|
|
|
if (!m_hasHash)
|
|
|
|
computeHash();
|
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum ConstructTheEmptyStringImplTag { ConstructTheEmptyStringImpl };
|
|
|
|
explicit StringImpl(ConstructTheEmptyStringImplTag) : m_characters("") { }
|
|
|
|
|
|
|
|
enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer };
|
2018-10-16 10:10:01 +00:00
|
|
|
explicit StringImpl(ConstructWithInlineBufferTag, size_t length) : m_length(length), m_characters(m_inlineBuffer) { }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
void computeHash() const;
|
|
|
|
|
2018-10-16 10:10:01 +00:00
|
|
|
size_t m_length { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
bool m_ownsBuffer { true };
|
|
|
|
mutable bool m_hasHash { false };
|
|
|
|
const char* m_characters { nullptr };
|
|
|
|
mutable unsigned m_hash { 0 };
|
|
|
|
char m_inlineBuffer[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::StringImpl;
|