2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ByteBuffer.h"
|
|
|
|
#include "RetainPtr.h"
|
|
|
|
#include "StringImpl.h"
|
|
|
|
#include "Traits.h"
|
|
|
|
#include "Vector.h"
|
2018-10-17 08:55:43 +00:00
|
|
|
#include "kstdio.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class String {
|
|
|
|
public:
|
|
|
|
~String() { }
|
|
|
|
|
|
|
|
String() { }
|
|
|
|
String(const String& other)
|
|
|
|
: m_impl(const_cast<String&>(other).m_impl.copyRef())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(String&& other)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(other.m_impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const char* cstring)
|
|
|
|
: m_impl(StringImpl::create(cstring))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const char* cstring, size_t length)
|
|
|
|
: m_impl(StringImpl::create(cstring, length))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const StringImpl& impl)
|
|
|
|
: m_impl(const_cast<StringImpl&>(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(RetainPtr<StringImpl>&& impl)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:49:22 +00:00
|
|
|
unsigned toUInt(bool& ok) const;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
String toLowercase() const
|
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return String();
|
|
|
|
return m_impl->toLowercase();
|
|
|
|
}
|
|
|
|
|
|
|
|
String toUppercase() const
|
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return String();
|
|
|
|
return m_impl->toUppercase();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> split(char separator) const;
|
2018-10-16 09:42:39 +00:00
|
|
|
String substring(size_t start, size_t length) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
bool isNull() const { return !m_impl; }
|
2018-10-10 09:53:07 +00:00
|
|
|
bool isEmpty() const { return length() == 0; }
|
|
|
|
unsigned length() const { return m_impl ? m_impl->length() : 0; }
|
|
|
|
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
|
|
|
char operator[](unsigned i) const { ASSERT(m_impl); return (*m_impl)[i]; }
|
|
|
|
|
|
|
|
bool operator==(const String&) const;
|
|
|
|
bool operator!=(const String& other) const { return !(*this == other); }
|
|
|
|
|
2018-10-26 07:54:29 +00:00
|
|
|
String isolatedCopy() const;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
static String empty();
|
|
|
|
|
|
|
|
StringImpl* impl() { return m_impl.ptr(); }
|
|
|
|
const StringImpl* impl() const { return m_impl.ptr(); }
|
|
|
|
|
|
|
|
String& operator=(String&& other)
|
|
|
|
{
|
2018-10-24 12:28:22 +00:00
|
|
|
if (this != &other)
|
2018-10-17 08:55:43 +00:00
|
|
|
m_impl = move(other.m_impl);
|
2018-10-24 12:28:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& operator=(const String& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
m_impl = const_cast<String&>(other).m_impl.copyRef();
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer toByteBuffer() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
RetainPtr<StringImpl> m_impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<String> {
|
|
|
|
static unsigned hash(const String& s) { return s.impl() ? s.impl()->hash() : 0; }
|
2018-10-17 08:55:43 +00:00
|
|
|
static void dump(const String& s) { kprintf("%s", s.characters()); }
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::String;
|