-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.cpp
More file actions
301 lines (251 loc) · 6.36 KB
/
string.cpp
File metadata and controls
301 lines (251 loc) · 6.36 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <cstring>
#include <cctype>
#include <utility>
#include <ostream>
#include <stdexcept>
#include "string.h"
#define NDEBUG
#include "assert.h"
/**
* Constructors
*/
//Constructor with a chararray as a parameter.
String::String(const char* s) : sz(cstrlen(s)), capacity(npowoftwo(sz)) {
str = new (std::nothrow) char[capacity]();
cstrncpy(str, s, capacity);
check();
}
//Constructor with reference to another String as a parameter.
String::String(const String& s) : sz(s.sz), capacity(s.capacity) {
str = new (std::nothrow) char[capacity]();
cstrncpy(str, s.str, capacity);
check();
}
//Initializer list constructor
String::String(std::initializer_list<char> li) : sz(li.size()), capacity(npowoftwo(li.size())) {
str = new (std::nothrow) char[capacity]();
uint i = 0;
for (auto it = li.begin(); it != li.end(); ++it)
str[i++] = *it;
check();
}
//Move constructor.
String::String(String&& other) : sz(other.sz), capacity(other.capacity), str(other.str) {
other.sz = 0;
other.capacity = 2;
other.str = new (std::nothrow) char[2]();
}
//Destructor. Deletes the memory allocated for str.
String::~String() {
delete [] str;
}
/**
* Operators
*/
//
String& String::operator=(const String& other) {
swap(other);
return (*this);
}
//Move assignment operator
String& String::operator=(String&& other) {
if (this == &other)
return (*this);
delete [] str;
sz = other.sz;
capacity = other.capacity;
str = other.str;
other.sz = 0;
other.capacity = 2;
other.str = new (std::nothrow) char[2]();
check();
}
//Accesses the given index in str. Invalid index will throw an error.
char& String::operator[](int i) {
if (!(i >= 0 && i <= sz))
throw std::logic_error("String operator [] called with an out of bounds integer.");
return str[i];
}
//Implements << operation for output of String. Outputs str to out.
std::ostream& operator<<(std::ostream& out, const String s) {
out << s.str;
return out;
}
//Implements >> operation for reading input to a String. Reads characters to string until eof or
//white-space. Using with extremely large inputs will have unknown results.
std::istream& operator>>(std::istream& is, String& s) {
s.clear();
char c;
while(true) {
is.get(c);
if (std::isspace(c, is.getloc()) || is.eof())
break;
s.push_back(c);
}
s.check();
return is;
}
//Swaps the contents of two Strings.
String& String::swap(String other) {
std::swap(sz, other.sz);
std::swap(capacity, other.capacity);
std::swap(str, other.str);
check();
return (*this);
}
/**
* Iterator
*/
typedef String::StringIterator str_it;
//Moves the iterator one step forward.
str_it& str_it::operator++() {
index++;
return *this;
}
//Post-increment that moves the iterator one-step forward.
str_it str_it::operator++(int) {
str_it tmp(*this);
operator++();
return tmp;
}
//Returns the character the iterator is pointing at.
char str_it::operator*() {
return str[index];
}
//Checks whether two iterators are the same. Should only be used on iterators of same instance
//of a String.
bool str_it::operator==(str_it other) {
return index == other.index;
}
//Returns the start-point of the String's iterator.
str_it String::begin() const {
str_it begin(0, str);
return begin;
}
//Returns the ending point of the String's iterator.
const str_it String::end() const {
str_it end(sz, str);
return end;
}
/**
* Basic operations
*/
//Returns the contents of the String as a newly allocated c-string. Responsibility for deletion
//lies with the caller.
char* String::to_C_string() const {
char* cstr = new (std::nothrow) char[sz + 1];
cstrncpy(cstr, str, capacity);
cstr[sz] = 0;
return cstr;
}
//Pushes a String to the back of this string. Also for char and c-string.
void String::push_back(String s) {
ensure_capacity(s.sz);
cstrncpy(str + sz, s.str, s.sz);
sz += s.sz;
check();
}
//Removes the last character from the String.
void String::pop_back() {
if(sz == 0)
throw std::logic_error("Tried to pop an empty string.");
str[sz] = 0;
sz--;
check();
}
//Inserts a string at position indicated by index. Also works for c-strings and a char due to
//conversions defined in constructors.
void String::insert(int index, String s) {
if (!(index <= sz && index >= 0))
throw std::logic_error("Inserted out of bounds");
char* prev = str;
sz += s.sz;
capacity = npowoftwo(sz);
str = new (std::nothrow) char[capacity]();
int k = 0;
for (int i = 0; i < sz; ++i, k++) {
if (i == index)
for (int j = 0; j < s.sz; ++j, ++i)
str[i] = s[j];
str[i] = prev[k];
}
check();
delete [] prev;
}
void String::clear() {
for (int i = 0; i < capacity; i++)
str[i] = 0;
sz = 0;
}
//Erases n characters from index onwards. If n characters between index and sz don't exist,
//an error is thrown.
void String::erase(int index, int n) {
if (!(index >= 0 && index < sz && n > 0 && n <= sz - index))
throw std::logic_error("Called erase with invalid index or number of characters.");
int i;
for (i = index; i < sz - n; i++)
str[i] = str[i + n];
for (; i < sz; i++)
str[i] = 0;
sz -= n;
check();
}
/**
* Memory operations
*/
//When called, ensures that the class has enough memory space for n characters.
void String::ensure_capacity(int n) {
if (sz + n >= capacity) {
capacity = npowoftwo(sz + n);
allocate_space();
}
check();
}
//Allocates a new memory block for str and copies the contents of the old one to the new
//destination. The old str is deleted.
void String::allocate_space() {
char* str_new = new (std::nothrow) char[capacity]();
cstrncpy(str_new, str, capacity);
delete [] str;
str = str_new;
}
/**
* Check
*/
inline void String::check() {
assert(sz >= 0 && "The size can't be negative.");
assert(capacity >= 2 && "The String doesn't have enough capacity for one char.");
assert(sz < capacity && "Size of the string must be smaller than the capacity");
assert(cstrlen(str) == sz && "The sz variable is incorrect.");
assert(str[sz] == 0 && "The String is not a valid c-string.");
}
/**
* Helpful functions
*/
//Calculates the length of a (valid) c-string
int cstrlen(const char* s) {
uint i = 0;
while (s[i]) {
i++;
}
return i;
}
//Copies up to n characters from one c-string to another.
char* cstrncpy(char* dst, const char* src, int n) {
uint i = 0;
while (i < n) {
if (src[i] == 0)
break;
dst[i] = src[i];
i++;
}
return dst;
}
//Finds the next power of two from the given number.
int npowoftwo(int n) {
int r = 2;
while (n >= r) {
r *= 2;
}
return r;
}