ladybird/AK/StringImpl.h

55 lines
1.5 KiB
C
Raw Normal View History

#pragma once
#include "Retainable.h"
#include "RetainPtr.h"
2018-10-16 10:10:01 +00:00
#include "Types.h"
namespace AK {
enum ShouldChomp { NoChomp, Chomp };
class StringImpl : public Retainable<StringImpl> {
public:
2018-12-21 01:10:45 +00:00
static RetainPtr<StringImpl> create_uninitialized(size_t length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RetainPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
2018-12-21 01:10:45 +00:00
RetainPtr<StringImpl> to_lowercase() const;
RetainPtr<StringImpl> to_uppercase() const;
2018-12-21 01:10:45 +00:00
static StringImpl& the_empty_stringimpl();
static void initialize_globals();
~StringImpl();
2018-10-16 10:10:01 +00:00
size_t length() const { return m_length; }
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]; }
unsigned hash() const
{
if (!m_hasHash)
2018-12-21 01:10:45 +00:00
compute_hash();
return m_hash;
}
private:
enum ConstructTheEmptyStringImplTag { ConstructTheEmptyStringImpl };
explicit StringImpl(ConstructTheEmptyStringImplTag) : m_characters("") { }
enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer };
2018-12-21 01:10:45 +00:00
explicit StringImpl(ConstructWithInlineBufferTag, size_t length) : m_length(length), m_characters(m_inline_buffer) { }
2018-12-21 01:10:45 +00:00
void compute_hash() const;
2018-10-16 10:10:01 +00:00
size_t m_length { 0 };
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];
};
}
using AK::StringImpl;
using AK::Chomp;