forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex14_18_String.cpp
202 lines (173 loc) · 4.48 KB
/
ex14_18_String.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "ex14_18_String.h"
#include <algorithm>
//===========================================================================
//
// operator - friend
//
//===========================================================================
std::ostream& operator<<(std::ostream &os, const String &lhs)
{
os << lhs.c_str();
return os;
}
std::istream& operator>>(std::istream &is, String &rhs)
{
for (char c; (c = is.get()) != '\n';) {
rhs.push_back(c);
}
return is;
}
bool operator==(const String &lhs, const String &rhs)
{
return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
bool operator!=(const String &lhs, const String &rhs)
{
return !(lhs == rhs);
}
bool operator<(const String &lhs, const String &rhs)
{
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
bool operator>(const String &lhs, const String &rhs)
{
return rhs < lhs;
}
bool operator<=(const String &lhs, const String &rhs)
{
return !(rhs < lhs);
}
bool operator>=(const String &lhs, const String &rhs)
{
return !(lhs < rhs);
}
//===========================================================================
//
// Constructors
//
//===========================================================================
String::String(const char *s)
{
char *sl = const_cast<char*>(s);
while (*sl)
++sl;
range_initializer(s, ++sl);
}
//===========================================================================
//
// Big 5
//
//===========================================================================
String::String(const String& rhs)
{
range_initializer(rhs.elements, rhs.first_free);
}
String& String::operator = (const String &rhs)
{
auto newstr = alloc_n_copy(rhs.elements, rhs.first_free);
free();
elements = newstr.first;
first_free = cap = newstr.second;
last_elem = first_free - 1;
return *this;
}
String::String(String &&s) NOEXCEPT : elements(s.elements), last_elem(s.last_elem), first_free(s.first_free), cap(s.cap)
{
s.elements = s.last_elem = s.first_free = s.cap = nullptr;
}
String& String::operator = (String &&rhs) NOEXCEPT
{
if (this != &rhs) {
free();
elements = rhs.elements;
last_elem = rhs.last_elem;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.elements = rhs.last_elem = rhs.first_free = rhs.cap = nullptr;
}
return *this;
}
String::~String()
{
free();
}
//===========================================================================
//
// members
//
//===========================================================================
void String::push_back(const char c)
{
chk_n_alloc();
*last_elem = c;
last_elem = first_free;
alloc.construct(first_free++, '\0');
}
void String::reallocate()
{
// \0 | -
// ^ ^
// elements first_free
// last_elem cap
auto newcapacity = size() ? 2 * (size() + 1) : 2;
alloc_n_move(newcapacity);
}
void String::alloc_n_move(size_t new_cap)
{
auto newdata = alloc.allocate(new_cap);
auto dest = newdata;
auto elem = elements;
for (size_t i = 0; i != size() + 1; ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
last_elem = dest - 1;
first_free = dest;
cap = elements + new_cap;
}
void String::free()
{
if (elements) {
std::for_each(elements, first_free, [this](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, cap - elements);
}
}
std::pair<char*, char*>
String::alloc_n_copy(const char *b, const char *e)
{
auto str = alloc.allocate(e - b);
return{ str, std::uninitialized_copy(b, e, str) };
}
void String::range_initializer(const char *first, const char *last)
{
auto newstr = alloc_n_copy(first, last);
elements = newstr.first;
first_free = cap = newstr.second;
last_elem = first_free - 1;
}
void String::reserve(size_t new_cap)
{
if (new_cap <= capacity()) return;
alloc_n_move(new_cap);
}
void String::resize(size_t count, char c)
{
if (count > size()) {
if (count > capacity()) reserve(count * 2);
for (size_t i = size(); i != count; ++i) {
*last_elem++ = c;
alloc.construct(first_free++, '\0');
}
}
else if (count < size()) {
while (last_elem != elements + count) {
--last_elem;
alloc.destroy(--first_free);
}
*last_elem = '\0';
}
}
void String::resize(size_t count)
{
resize(count, ' ');
}