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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#include "string.hh"
#include <cstring>
#include "assert.hh"
#include "unit_tests.hh"
namespace Kakoune
{
namespace
{
// Avoid including all of <algorithm> just for this.
constexpr auto max(auto lhs, auto rhs) { return lhs > rhs ? lhs : rhs;}
constexpr auto min(auto lhs, auto rhs) { return lhs < rhs ? lhs : rhs;}
}
String::Data::Data(const char* data, size_t size, size_t capacity)
{
if (capacity > Short::capacity)
{
kak_assert(capacity <= Long::max_capacity);
u.l.ptr = Alloc{}.allocate(capacity+1);
u.l.size = size;
u.l.capacity = (capacity & Long::max_capacity);
u.l.mode = Long::active_mask;
if (data != nullptr)
memcpy(u.l.ptr, data, size);
u.l.ptr[size] = 0;
}
else
set_short(data, size);
}
String::Data& String::Data::operator=(const Data& other)
{
if (&other == this)
return *this;
const size_t new_size = other.size();
reserve<false>(new_size);
memcpy(data(), other.data(), new_size+1);
set_size(new_size);
return *this;
}
String::Data& String::Data::operator=(Data&& other) noexcept
{
if (&other == this)
return *this;
release();
if (other.is_long())
{
u.l = other.u.l;
other.set_empty();
}
else
u.s = other.u.s;
return *this;
}
template<bool copy>
void String::Data::reserve(size_t new_capacity)
{
auto const current_capacity = capacity();
if (current_capacity != 0 and new_capacity <= current_capacity)
return;
if (!is_long() and new_capacity <= Short::capacity)
return;
kak_assert(new_capacity <= Long::max_capacity);
new_capacity = max(new_capacity, // Do not upgrade new_capacity to be over limit.
min(current_capacity * 2, Long::max_capacity));
char* new_ptr = Alloc{}.allocate(new_capacity+1);
if (copy)
{
memcpy(new_ptr, data(), size()+1);
}
release();
u.l.size = size();
u.l.ptr = new_ptr;
u.l.capacity = (new_capacity & Long::max_capacity);
u.l.mode = Long::active_mask;
}
template void String::Data::reserve<true>(size_t);
template void String::Data::reserve<false>(size_t);
void String::Data::force_size(size_t new_size)
{
reserve<false>(new_size);
set_size(new_size);
data()[new_size] = 0;
}
void String::Data::append(const char* str, size_t len)
{
if (len == 0)
return;
const size_t new_size = size() + len;
reserve(new_size);
memcpy(data() + size(), str, len);
set_size(new_size);
data()[new_size] = 0;
}
void String::Data::clear()
{
release();
set_empty();
}
void String::resize(ByteCount size, char c)
{
const size_t target_size = (size_t)size;
const size_t current_size = m_data.size();
if (target_size < current_size)
m_data.set_size(target_size);
else if (target_size > current_size)
{
m_data.reserve(target_size);
m_data.set_size(target_size);
for (auto i = current_size; i < target_size; ++i)
m_data.data()[i] = c;
}
data()[target_size] = 0;
}
void String::Data::set_size(size_t size)
{
if (is_long())
u.l.size = size;
else
u.s.remaining_size = Short::capacity - size;
}
void String::Data::set_short(const char* data, size_t size)
{
kak_assert(size <= Short::capacity);
u.s.remaining_size = Short::capacity - size;
if (data != nullptr)
memcpy(u.s.string, data, size);
if (size != Short::capacity) // in this case, remaining_size is the null terminator
u.s.string[size] = 0;
}
UnitTest test_data{[]{
using Data = String::Data;
{ // Basic data usage.
Data data;
kak_assert(data.size() == 0);
kak_assert(not data.is_long());
kak_assert(data.capacity() == 23);
// Should be SSO-ed.
data.append("test", 4);
kak_assert(data.size() == 4);
kak_assert(data.capacity() == 23);
kak_assert(not data.is_long());
kak_assert(data.data() == StringView("test"));
}
{
char large_buf[2048];
memset(large_buf, 'x', 2048);
Data data(large_buf, 2048);
kak_assert(data.size() == 2048);
kak_assert(data.capacity() >= 2048);
data.clear();
kak_assert(data.size() == 0);
kak_assert(not data.is_long());
kak_assert(data.capacity() == 23);
}
}};
const String String::ms_empty;
}
|