ladybird/AK/AKString.h

164 lines
3.9 KiB
C
Raw Normal View History

#pragma once
2019-04-15 12:56:37 +00:00
#include <AK/ByteBuffer.h>
#include <AK/RetainPtr.h>
#include <AK/StringImpl.h>
#include <AK/Traits.h>
#include <AK/Vector.h>
#include <AK/StringView.h>
#include <AK/kstdio.h>
namespace AK {
class String {
public:
~String() { }
String() { }
2019-04-15 12:56:37 +00:00
String(StringView view)
: m_impl(StringImpl::create(view.characters(), view.length()))
{
}
String(const String& other)
: m_impl(const_cast<String&>(other).m_impl.copy_ref())
{
}
String(String&& other)
2018-10-17 08:55:43 +00:00
: m_impl(move(other.m_impl))
{
}
String(const char* cstring, ShouldChomp shouldChomp = NoChomp)
: m_impl(StringImpl::create(cstring, shouldChomp))
{
}
String(const char* cstring, ssize_t length, ShouldChomp shouldChomp = NoChomp)
: m_impl(StringImpl::create(cstring, length, shouldChomp))
{
}
String(const StringImpl& impl)
: m_impl(const_cast<StringImpl&>(impl))
{
}
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))
{
}
String(Retained<StringImpl>&& impl)
: m_impl(move(impl))
{
}
static String repeated(char, int count);
int to_int(bool& ok) const;
unsigned to_uint(bool& ok) const;
2018-12-21 01:10:45 +00:00
String to_lowercase() const
{
if (!m_impl)
return String();
2018-12-21 01:10:45 +00:00
return m_impl->to_lowercase();
}
2018-12-21 01:10:45 +00:00
String to_uppercase() const
{
if (!m_impl)
return String();
2018-12-21 01:10:45 +00:00
return m_impl->to_uppercase();
}
Vector<String> split(char separator) const;
String substring(int start, int length) const;
Vector<StringView> split_view(char separator) const;
StringView substring_view(int start, int length) const;
2018-12-21 01:10:45 +00:00
bool is_null() const { return !m_impl; }
bool is_empty() const { return length() == 0; }
ssize_t length() const { return m_impl ? m_impl->length() : 0; }
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
char operator[](ssize_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
bool ends_with(const String&) const;
bool operator==(const String&) const;
bool operator!=(const String& other) const { return !(*this == other); }
bool operator<(const String&) const;
2018-12-21 01:10:45 +00:00
String isolated_copy() const;
2018-10-26 07:54:29 +00:00
static String empty();
StringImpl* impl() { return m_impl.ptr(); }
const StringImpl* impl() const { return m_impl.ptr(); }
String& operator=(String&& other)
{
if (this != &other)
2018-10-17 08:55:43 +00:00
m_impl = move(other.m_impl);
return *this;
}
String& operator=(const String& other)
{
if (this != &other)
m_impl = const_cast<String&>(other).m_impl.copy_ref();
return *this;
}
2018-12-21 01:10:45 +00:00
ByteBuffer to_byte_buffer() const;
template<typename BufferType>
static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp)
{
if (buffer.is_null())
return { };
if (buffer.is_empty())
return empty();
return String((const char*)buffer.data(), buffer.size(), should_chomp);
}
static String format(const char*, ...);
2019-04-15 12:56:37 +00:00
StringView view() const { return { characters(), length() }; }
private:
RetainPtr<StringImpl> m_impl;
};
inline bool StringView::operator==(const String& string) const
{
if (string.is_null())
return !m_characters;
if (!m_characters)
return false;
if (m_length != string.length())
return false;
if (m_characters == string.characters())
return true;
return !memcmp(m_characters, string.characters(), m_length);
}
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()); }
};
}
using AK::String;