-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyString.cpp
More file actions
335 lines (271 loc) · 7.09 KB
/
Copy pathMyString.cpp
File metadata and controls
335 lines (271 loc) · 7.09 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#define _CRT_SECURE_NO_WARNINGS
#include "MyString.h"
#include <cstring>
#include <istream>
// Removes old buffer and creates a new one, copying str into it.
void MyString::assign(const char* str)
{
delete[] data_;
if (str) {
size_ = std::strlen(str);
data_ = new char[size_ + 1];
std::strcpy(data_, str);
}
else {
size_ = 0;
data_ = new char[1];
data_[0] = '\0';
}
}
// Helper method for concatenating two strings
char* MyString::concat(const char* s1, size_t len1, const char* s2, size_t len2)
{
char* buf = new char[len1 + len2 + 1];
std::strcpy(buf, s1);
std::strcat(buf, s2);
return buf;
}
// Default constructor
MyString::MyString()
: data_(new char[1])
, size_(0)
{
data_[0] = '\0';
}
// Constructor from string
MyString::MyString(const char* str)
: data_(nullptr)
, size_(0)
{
assign(str);
}
// Copy constructor
// Simply copy the contents
MyString::MyString(const MyString& other)
: data_(new char[other.size_ + 1])
, size_(other.size_)
{
std::strcpy(data_, other.data_);
}
// Destructor
MyString::~MyString()
{
delete[] data_;
}
// Copy assignment
MyString& MyString::operator=(const MyString& other)
{
if (this != &other) {
assign(other.data_);
}
return *this;
}
// Move constructor
MyString::MyString(MyString&& other) noexcept
: data_(other.data_)
, size_(other.size_)
{
other.data_ = nullptr;
other.size_ = 0;
}
// Move assignment
// Free our old buffer, take data from other, nullify other
MyString& MyString::operator=(MyString&& other) noexcept
{
if (this != &other) {
delete[] data_;
data_ = other.data_;
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
}
return *this;
}
const char* MyString::c_str() const
{
return data_;
}
bool MyString::empty() const
{
return size_ == 0;
}
// Returns the length of the string without '\0'
size_t MyString::size() const
{
return size_;
}
// Finds the last occurrence of any character from chars
size_t MyString::find_last_of(const char* chars) const
{
if (!chars || !*chars) return npos;
size_t charsLen = std::strlen(chars);
for (size_t i = size_; i > 0; i--) { // run from end to beginning
for (size_t j = 0; j < charsLen; j++) {
if (data_[i - 1] == chars[j]) {
return i - 1; // found - return index
}
}
}
return npos; // didn't find anything
}
// Same, but looking for one specific character
size_t MyString::find_last_of(char c) const
{
for (size_t i = size_; i > 0; i--) {
if (data_[i - 1] == c) {
return i - 1;
}
}
return npos;
}
// Lexicographic comparison of strings (needed for std::map<MyString, ...>)
bool operator<(const MyString& lhs, const MyString& rhs)
{
return std::strcmp(lhs.data_, rhs.data_) < 0;
}
// ===== substr =====
// Extracts a substring starting at pos with length count.
// If pos is out of bounds - return an empty string.
MyString MyString::substr(size_t pos, size_t count) const
{
if (pos >= size_) return MyString(); // out of bounds
size_t realCount = count;
if (realCount == npos || pos + realCount > size_) {
realCount = size_ - pos; // trim to end of string
}
char* buf = new char[realCount + 1];
for (size_t i = 0; i < realCount; i++) {
buf[i] = data_[pos + i]; // copy character by character
}
buf[realCount] = '\0';
MyString result(buf);
delete[] buf; // delete temporary buffer
return result;
}
// ===== operator== (MyString, MyString) =====
bool operator==(const MyString& lhs, const MyString& rhs)
{
return std::strcmp(lhs.data_, rhs.data_) == 0;
}
// ===== operator== (MyString, const char*) =====
bool operator==(const MyString& lhs, const char* rhs)
{
return std::strcmp(lhs.data_, rhs) == 0;
}
// ===== operator== (const char*, MyString) =====
bool operator==(const char* lhs, const MyString& rhs)
{
return std::strcmp(lhs, rhs.data_) == 0;
}
// Appends a string to the end.
// Uses concat() to join old and new strings in a new buffer.
MyString& MyString::operator+=(const char* rhs)
{
if (!rhs || !*rhs) return *this;
size_t rhsLen = std::strlen(rhs);
char* buf = concat(data_, size_, rhs, rhsLen);
delete[] data_;
data_ = buf;
size_ += rhsLen;
return *this;
}
// Reuses += for const char*, passing data_ from rhs
MyString& MyString::operator+=(const MyString& rhs)
{
return *this += rhs.data_;
}
// Creates a new string = lhs + rhs.
// Allocate buffer via concat(), then change data_ and size_ of result.
MyString operator+(const MyString& lhs, const char* rhs)
{
size_t rhsLen = (rhs ? std::strlen(rhs) : 0);
char* buf = MyString::concat(lhs.data_, lhs.size_, rhs ? rhs : "", rhsLen);
MyString result;
delete[] result.data_; // delete empty buffer created by constructor
result.data_ = buf;
result.size_ = lhs.size_ + rhsLen;
return result;
}
// ===== operator+ (MyString, MyString) =====
MyString operator+(const MyString& lhs, const MyString& rhs)
{
return lhs + rhs.data_;
}
std::ostream& operator<<(std::ostream& os, const MyString& str)
{
os << str.data_;
return os;
}
std::istream& operator>>(std::istream& is, MyString& str)
{
// Clear the string
delete[] str.data_;
str.data_ = new char[1];
str.data_[0] = '\0';
str.size_ = 0;
// Skip whitespace
char c;
while (is.get(c)) {
if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
break;
}
if (!is) return is;
// Read word until whitespace
size_t capacity = 32;
char* buf = new char[capacity];
size_t len = 0;
buf[len++] = c;
while (is.get(c)) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
is.putback(c);
break;
}
if (len + 1 >= capacity) {
capacity *= 2;
char* newBuf = new char[capacity];
for (size_t i = 0; i < len; i++) newBuf[i] = buf[i];
delete[] buf;
buf = newBuf;
}
buf[len++] = c;
}
buf[len] = '\0';
delete[] str.data_;
str.data_ = buf;
str.size_ = len;
// Reset failbit/eofbit, since at least one token was successfully read
is.clear();
return is;
}
std::istream& getline(std::istream& is, MyString& str, char delim)
{
// Clear the string
delete[] str.data_;
str.data_ = new char[1];
str.data_[0] = '\0';
str.size_ = 0;
size_t capacity = 64;
char* buf = new char[capacity];
size_t len = 0;
char c;
while (is.get(c)) {
if (c == delim) break;
if (len + 1 >= capacity) {
capacity *= 2;
char* newBuf = new char[capacity];
for (size_t i = 0; i < len; i++) newBuf[i] = buf[i];
delete[] buf;
buf = newBuf;
}
buf[len++] = c;
}
// Remove trailing \r
while (len > 0 && (buf[len - 1] == '\r' || buf[len - 1] == '\n')) {
len--;
}
buf[len] = '\0';
delete[] str.data_;
str.data_ = buf;
str.size_ = len;
return is;
}