2018-10-10 09:53:07 +00:00
|
|
|
#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>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
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()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
String(const String& other)
|
2019-01-31 16:31:23 +00:00
|
|
|
: m_impl(const_cast<String&>(other).m_impl.copy_ref())
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-06 23:19:35 +00:00
|
|
|
String(const char* cstring, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(cstring, shouldChomp))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
String(const char* cstring, ssize_t length, ShouldChomp shouldChomp = NoChomp)
|
2018-11-06 23:19:35 +00:00
|
|
|
: m_impl(StringImpl::create(cstring, length, shouldChomp))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const StringImpl& impl)
|
|
|
|
: m_impl(const_cast<StringImpl&>(impl))
|
2019-04-12 12:43:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const StringImpl* impl)
|
|
|
|
: m_impl(const_cast<StringImpl*>(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(RetainPtr<StringImpl>&& impl)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-25 15:04:08 +00:00
|
|
|
String(Retained<StringImpl>&& impl)
|
|
|
|
: m_impl(move(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:16 +00:00
|
|
|
enum class CaseSensitivity {
|
|
|
|
CaseInsensitive,
|
|
|
|
CaseSensitive,
|
|
|
|
};
|
|
|
|
|
2019-04-25 20:56:09 +00:00
|
|
|
static String repeated(char, int count);
|
2019-05-26 18:36:16 +00:00
|
|
|
bool matches(const String& pattern, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
2019-04-25 20:56:09 +00:00
|
|
|
|
2019-04-18 23:05:59 +00:00
|
|
|
int to_int(bool& ok) const;
|
2019-01-31 16:31:23 +00:00
|
|
|
unsigned to_uint(bool& ok) const;
|
2018-10-31 18:49:22 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
String to_lowercase() const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return String();
|
2018-12-21 01:10:45 +00:00
|
|
|
return m_impl->to_lowercase();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
String to_uppercase() const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return String();
|
2018-12-21 01:10:45 +00:00
|
|
|
return m_impl->to_uppercase();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> split(char separator) const;
|
2019-04-16 00:39:16 +00:00
|
|
|
String substring(int start, int length) const;
|
|
|
|
|
|
|
|
Vector<StringView> split_view(char separator) const;
|
|
|
|
StringView substring_view(int start, int length) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_null() const { return !m_impl; }
|
|
|
|
bool is_empty() const { return length() == 0; }
|
2019-02-25 21:06:55 +00:00
|
|
|
ssize_t length() const { return m_impl ? m_impl->length() : 0; }
|
2018-10-10 09:53:07 +00:00
|
|
|
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
2019-02-25 21:06:55 +00:00
|
|
|
char operator[](ssize_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-03-22 11:43:29 +00:00
|
|
|
bool ends_with(const String&) const;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
bool operator==(const String&) const;
|
|
|
|
bool operator!=(const String& other) const { return !(*this == other); }
|
2019-03-09 12:33:52 +00:00
|
|
|
bool operator<(const String&) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
String isolated_copy() const;
|
2018-10-26 07:54:29 +00:00
|
|
|
|
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)
|
2019-01-31 16:31:23 +00:00
|
|
|
m_impl = const_cast<String&>(other).m_impl.copy_ref();
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
ByteBuffer to_byte_buffer() const;
|
2019-04-20 12:13:40 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-01-30 15:28:51 +00:00
|
|
|
static String format(const char*, ...);
|
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
StringView view() const { return { characters(), length() }; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2019-05-26 18:36:16 +00:00
|
|
|
bool match_helper(const String& mask) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
RetainPtr<StringImpl> m_impl;
|
|
|
|
};
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
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;
|