2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-21 16:45:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/StringImpl.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/StringView.h>
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-02 06:37:01 +00:00
|
|
|
// String is a convenience wrapper around StringImpl, suitable for passing
|
|
|
|
// around as a value type. It's basically the same as passing around a
|
2019-06-21 16:37:47 +00:00
|
|
|
// RefPtr<StringImpl>, with a bit of syntactic sugar.
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// Note that StringImpl is an immutable object that cannot shrink or grow.
|
|
|
|
// Its allocation size is snugly tailored to the specific string it contains.
|
|
|
|
// Copying a String is very efficient, since the internal StringImpl is
|
2019-06-21 16:40:24 +00:00
|
|
|
// retainable and so copying only requires modifying the ref count.
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// There are three main ways to construct a new String:
|
|
|
|
//
|
|
|
|
// s = String("some literal");
|
|
|
|
//
|
|
|
|
// s = String::format("%d little piggies", m_piggies);
|
|
|
|
//
|
|
|
|
// StringBuilder builder;
|
|
|
|
// builder.append("abc");
|
|
|
|
// builder.append("123");
|
|
|
|
// s = builder.to_string();
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
class String {
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
~String() {}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
String() {}
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2019-06-03 16:27:56 +00:00
|
|
|
String(const StringView& view)
|
2019-04-15 12:56:37 +00:00
|
|
|
{
|
2019-06-08 21:55:13 +00:00
|
|
|
if (view.m_impl)
|
|
|
|
m_impl = *view.m_impl;
|
2019-06-03 16:27:56 +00:00
|
|
|
else
|
2019-07-08 13:38:44 +00:00
|
|
|
m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
|
2019-04-15 12:56:37 +00:00
|
|
|
}
|
|
|
|
|
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-06-14 04:43:56 +00:00
|
|
|
String(const char* cstring, int 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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
String(RefPtr<StringImpl>&& impl)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
String(NonnullRefPtr<StringImpl>&& impl)
|
2019-02-25 15:04:08 +00:00
|
|
|
: m_impl(move(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class CaseSensitivity {
|
2019-05-26 18:36:16 +00:00
|
|
|
CaseInsensitive,
|
|
|
|
CaseSensitive,
|
|
|
|
};
|
|
|
|
|
2019-04-25 20:56:09 +00:00
|
|
|
static String repeated(char, int count);
|
2019-06-02 10:26:28 +00:00
|
|
|
bool matches(const StringView& 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
|
|
|
}
|
|
|
|
|
2019-06-04 08:13:07 +00:00
|
|
|
Vector<String> split_limit(char separator, int limit) const;
|
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-06-14 04:43:56 +00:00
|
|
|
int 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-06-14 04:43:56 +00:00
|
|
|
char operator[](int i) const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_impl);
|
|
|
|
return (*m_impl)[i];
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-04 11:53:25 +00:00
|
|
|
bool starts_with(const StringView&) const;
|
2019-06-02 10:26:28 +00:00
|
|
|
bool ends_with(const StringView&) const;
|
2019-03-22 11:43:29 +00:00
|
|
|
|
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;
|
2019-07-04 12:20:48 +00:00
|
|
|
bool operator<(const char*) const;
|
|
|
|
bool operator>=(const String& other) const { return !(*this < other); }
|
|
|
|
bool operator>=(const char* other) const { return !(*this < other); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-08 16:30:40 +00:00
|
|
|
bool operator==(const char* cstring) const
|
|
|
|
{
|
|
|
|
if (is_null())
|
|
|
|
return !cstring;
|
|
|
|
if (!cstring)
|
|
|
|
return false;
|
|
|
|
return !strcmp(characters(), cstring);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const char* cstring) const
|
|
|
|
{
|
|
|
|
return !(*this == cstring);
|
|
|
|
}
|
|
|
|
|
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())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-20 12:13:40 +00:00
|
|
|
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-07-03 12:56:27 +00:00
|
|
|
static String number(unsigned);
|
|
|
|
static String number(int);
|
2019-01-30 15:28:51 +00:00
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
StringView view() const { return { characters(), length() }; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2019-06-02 10:26:28 +00:00
|
|
|
bool match_helper(const StringView& mask) const;
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<StringImpl> m_impl;
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
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<>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct Traits<String> : public GenericTraits<String> {
|
2018-10-10 09:53:07 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-07-04 12:20:48 +00:00
|
|
|
inline bool operator<(const char* characters, const String& string)
|
|
|
|
{
|
|
|
|
if (!characters)
|
|
|
|
return !string.is_null();
|
|
|
|
|
|
|
|
if (string.is_null())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return strcmp(characters, string.characters()) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator>=(const char* characters, const String& string)
|
|
|
|
{
|
|
|
|
return !(characters < string);
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::String;
|