2018-10-10 05:53:07 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ByteBuffer.h"
|
|
|
|
#include "RetainPtr.h"
|
|
|
|
#include "StringImpl.h"
|
|
|
|
#include "Traits.h"
|
|
|
|
#include "Vector.h"
|
2018-10-17 04:55:43 -04:00
|
|
|
#include "kstdio.h"
|
2018-10-10 05:53:07 -04:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class String {
|
|
|
|
public:
|
|
|
|
~String() { }
|
|
|
|
|
|
|
|
String() { }
|
|
|
|
String(const String& other)
|
2019-01-31 11:31:23 -05:00
|
|
|
: m_impl(const_cast<String&>(other).m_impl.copy_ref())
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(String&& other)
|
2018-10-17 04:55:43 -04:00
|
|
|
: m_impl(move(other.m_impl))
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-06 18:19:35 -05:00
|
|
|
String(const char* cstring, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(cstring, shouldChomp))
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-06 18:19:35 -05:00
|
|
|
String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(cstring, length, shouldChomp))
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const StringImpl& impl)
|
|
|
|
: m_impl(const_cast<StringImpl&>(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(RetainPtr<StringImpl>&& impl)
|
2018-10-17 04:55:43 -04:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-25 10:04:08 -05:00
|
|
|
String(Retained<StringImpl>&& impl)
|
|
|
|
: m_impl(move(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-31 11:31:23 -05:00
|
|
|
unsigned to_uint(bool& ok) const;
|
2018-10-31 14:49:22 -04:00
|
|
|
|
2018-12-20 20:10:45 -05:00
|
|
|
String to_lowercase() const
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return String();
|
2018-12-20 20:10:45 -05:00
|
|
|
return m_impl->to_lowercase();
|
2018-10-10 05:53:07 -04:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:10:45 -05:00
|
|
|
String to_uppercase() const
|
2018-10-10 05:53:07 -04:00
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return String();
|
2018-12-20 20:10:45 -05:00
|
|
|
return m_impl->to_uppercase();
|
2018-10-10 05:53:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> split(char separator) const;
|
2018-10-16 05:42:39 -04:00
|
|
|
String substring(size_t start, size_t length) const;
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2018-12-20 20:10:45 -05:00
|
|
|
bool is_null() const { return !m_impl; }
|
|
|
|
bool is_empty() const { return length() == 0; }
|
2019-01-08 23:38:13 -05:00
|
|
|
size_t length() const { return m_impl ? m_impl->length() : 0; }
|
2018-10-10 05:53:07 -04:00
|
|
|
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
2019-01-08 23:38:13 -05:00
|
|
|
char operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
|
2018-10-10 05:53:07 -04:00
|
|
|
|
|
|
|
bool operator==(const String&) const;
|
|
|
|
bool operator!=(const String& other) const { return !(*this == other); }
|
|
|
|
|
2018-12-20 20:10:45 -05:00
|
|
|
String isolated_copy() const;
|
2018-10-26 03:54:29 -04:00
|
|
|
|
2018-10-10 05:53:07 -04: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 08:28:22 -04:00
|
|
|
if (this != &other)
|
2018-10-17 04:55:43 -04:00
|
|
|
m_impl = move(other.m_impl);
|
2018-10-24 08:28:22 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& operator=(const String& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
2019-01-31 11:31:23 -05:00
|
|
|
m_impl = const_cast<String&>(other).m_impl.copy_ref();
|
2018-10-10 05:53:07 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:10:45 -05:00
|
|
|
ByteBuffer to_byte_buffer() const;
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2019-01-30 10:28:51 -05:00
|
|
|
static String format(const char*, ...);
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
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 04:55:43 -04:00
|
|
|
static void dump(const String& s) { kprintf("%s", s.characters()); }
|
2018-10-10 05:53:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::String;
|