1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#pragma once
#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() { }
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)
: 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)
: m_impl(move(impl))
{
}
String(Retained<StringImpl>&& impl)
: m_impl(move(impl))
{
}
unsigned to_uint(bool& ok) const;
String to_lowercase() const
{
if (!m_impl)
return String();
return m_impl->to_lowercase();
}
String to_uppercase() const
{
if (!m_impl)
return String();
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;
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;
String isolated_copy() const;
static String empty();
StringImpl* impl() { return m_impl.ptr(); }
const StringImpl* impl() const { return m_impl.ptr(); }
String& operator=(String&& other)
{
if (this != &other)
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;
}
ByteBuffer to_byte_buffer() const;
static String from_byte_buffer(const ByteBuffer&, ShouldChomp = NoChomp);
static String format(const char*, ...);
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; }
static void dump(const String& s) { kprintf("%s", s.characters()); }
};
}
using AK::String;
|