-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSString.cpp
More file actions
600 lines (493 loc) · 16 KB
/
Copy pathDSString.cpp
File metadata and controls
600 lines (493 loc) · 16 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#include "DSString.h"
DSString::DSString() {
len = 0;
data = new char[1];
data[len] = '\0';
}
// constructor that converts a cstring
DSString::DSString(const char* cstr) {
// Determine length of cstring
len = 0;
for (size_t i = 0; cstr[i] != '\0'; i++) {
len++;
}
// Create new char array the correct size
data = new char[len + 1];
// Copy cstring into char array
for (size_t i = 0; i < len; i++) {
data[i] = cstr[i];
}
// Add null terminator
data[len] = '\0';
}
// constructor that converts a std::string
DSString::DSString(const std::string& str) {
len = str.length();
data = new char[len + 1];
// Copy each character from string into char array
for (size_t i = 0; i < len; i++) {
data[i] = str.at(i);
}
// Add null terminator
data[len] = '\0';
}
// copy constructor
DSString::DSString(const DSString& cpyFrm) {
len = cpyFrm.len;
data = new char[len + 1];
// Copy each character
for (size_t i = 0; i < len; i++) {
data[i] = cpyFrm.data[i];
}
// Add null terminator
data[len] = '\0';
}
// Move constructor
DSString::DSString(DSString&& moveFrm) {
len = moveFrm.len;
data = moveFrm.data;
moveFrm.data = nullptr;
moveFrm.len = 0;
}
// destructor
DSString::~DSString() {
// Clean up char array
delete[] data;
}
// assignment operator
DSString& DSString::operator=(const DSString& cpyFrm) {
// Clean up old data
delete data;
len = cpyFrm.len;
data = new char[len + 1];
// Copy each character
for (size_t i = 0; i < len; i++) {
data[i] = cpyFrm.data[i];
}
// Add null terminator
data[len] = '\0';
return *this;
}
// move assignment operator
DSString& DSString::operator=(DSString&& moveFrm) {
if(this == &moveFrm) {
return *this;
}
// Clean up old data
delete[] data;
len = moveFrm.len;
data = moveFrm.data;
moveFrm.data = nullptr;
moveFrm.len = 0;
return *this;
}
/**
* @brief returns the length of the string
*
* @return int containing the length of the string
*/
size_t DSString::length() const { return len; }
// returns a reference to the character at the given index
char& DSString::operator[](size_t index) { return data[index]; }
// returns a reference to the char at the given index and includes bounds checking
char& DSString::at(size_t index) {
if (index >= len) {
throw std::out_of_range("Index out of range");
}
return data[index];
}
/**
* Overloaded operator+ which appends the string in the argument to this string
*/
DSString DSString::operator+(const DSString& rhs) const {
size_t combinedLen = len + rhs.length() + 1;
char* newData = new char[combinedLen];
newData[len] = '\0';
for (size_t i = 0; i < len; i++) {
newData[i] = data[i];
}
for (size_t i = 0; i < rhs.length(); i++) {
newData[len + i] = rhs.data[i];
}
return DSString(newData);
}
bool DSString::operator==(const DSString& rhs) const {
// verify lengths are equal
if (len != rhs.len) {
return false;
}
// Loop through each char and compare
for (size_t i = 0; i < len; i++) {
if (data[i] != rhs.data[i]) {
// if any char is not equal we can return false
return false;
}
}
// All chars are equal
return true;
}
bool DSString::operator<(const DSString& rhs) const {
for (size_t i = 0; i < len && i < rhs.len; i++) {
if (data[i] < rhs.data[i]) {
return true;
} else if (data[i] > rhs.data[i]) {
return false;
}
}
// If we get here, all chars are equal up to the length of the shorter string
// If the strings are equal, return false
if (len == rhs.len) {
return false;
}
// If the strings are not equal, return true if this string is shorter
return len < rhs.len;
}
bool DSString::operator>(const DSString& rhs) const {
for (size_t i = 0; i < len && i < rhs.len; i++) {
if (data[i] > rhs.data[i]) {
return true;
} else if (data[i] < rhs.data[i]) {
return false;
}
}
// If we get here, all chars are equal up to the length of the shorter string
// If the strings are equal, return false
if (len == rhs.len) {
return false;
}
// If the strings are not equal, return true if this string is longer
return len > rhs.len;
}
bool DSString::operator!=(const DSString& rhs) const {
// check if lengths are equal
if (len != rhs.len) {
// if lengths are not equal, strings are not equal
return true;
}
// Loop through each char and compare
for (size_t i = 0; i < len; i++) {
if (data[i] != rhs.data[i]) {
// if any char is not equal we can return true
return true;
}
}
// All chars are equal
return false;
}
bool DSString::operator<=(const DSString& rhs) const {
return this->operator<(rhs) || this->operator==(rhs);
}
bool DSString::operator>=(const DSString& rhs) const {
return this->operator>(rhs) || this->operator==(rhs);
}
/**
* The substring method returns a new string object that contains a
* sequence of characters from this string object.
*
* @param start - the index of where to start
* @param numChars - the number (count) of characters to copy into
* the substring
* @return a DSString object containing the requested substring
**/
DSString DSString::substring(size_t start, size_t numChars) const {
// Validate start and numChars
if (start + numChars > len) {
throw std::out_of_range("Substring out of range");
}
char* newData = new char[numChars + 1];
// Copy chars from 'start' to 'start + numChars'
for (size_t i = 0; i < numChars; i++) {
newData[i] = data[i + start];
}
// Add null terminator
newData[numChars] = '\0';
// Create new DSString object
DSString newstr = DSString(newData);
// Clean up memory
delete[] newData;
// Return the new DSString
return newstr;
}
void DSString::remove(size_t index) {
// verify that index is valid
if (index > len) {
throw std::out_of_range("Index out of range");
}
// Create new char[] 1 shorter than the old (still need room for null terminator)
char* newdata = new char[len];
size_t j = 0;
for (size_t i = 0; i < len; i++) {
if (i != index) {
newdata[j] = data[i];
j++;
}
}
// Clean up old data
delete[] data;
data = newdata;
len = len - 1;
// Add null terminator
data[len] = '\0';
}
/**
* @brief Returns a new string object with all characters in lowercase
*
* @return DSString
*/
DSString DSString::toLower() const {
char* newData = new char[len + 1];
newData[len] = '\0';
for (size_t i = 0; i < len; i++) {
// Check if the character is in the range of uppercase letters
if (data[i] >= 'A' && data[i] <= 'Z') {
// Add 32 to the character to get the lowercase version
newData[i] = data[i] + 32;
} else {
// Otherwise just copy the character
newData[i] = data[i];
}
}
DSString newString = DSString(newData);
delete[] newData;
return newString;
}
/**
* the c_str function returns a pointer a null-terminated c-string holding the
* contents of this object. Since data already has a `\0`
* at the end of the string in DSString so you can just return a pointer to data.
**/
char* DSString::c_str() const { return data; }
/**
* @brief return an std::string version of our DSString
*
* @return std::string
*/
std::string DSString::string() const { return std::string(data); }
/**
* Overloaded stream insertion operator to print the contents of this
* string to the output stream in the first argument.
**/
std::ostream& operator<<(std::ostream& out, const DSString& dsstr) {
out.write(dsstr.data, dsstr.len);
return out;
}
/**
* Overload the stream extraction operator to read a string from the input stream.
*/
std::istream& operator>>(std::istream& in, DSString& dsstr) {
// Clean up old data
delete[] dsstr.data;
dsstr.len = 0;
size_t tmpLen = 5; // Length of the newData char[], scales exponentially to minimize copying
char* newData = new char[tmpLen];
char tmpchar; // Holds each char from the input buffer
// Read in each char from the input buffer until we hit a space or newline
while (in.get(tmpchar) && tmpchar != ' ' && tmpchar != '\n') {
dsstr.len++; // Holds the actual length of the data that's been read in
// Scale if needed
if (dsstr.len > tmpLen) {
// Expand char[]
tmpLen = tmpLen * 2;
char* tmpData = new char[tmpLen];
// Copy data from old/shorter char[] to new/longer char[]
for (size_t i = 0; i < dsstr.len - 1; i++) {
tmpData[i] = newData[i];
}
// delete old char[]
delete[] newData;
newData = nullptr;
// Set newData equal to tmpData
newData = tmpData;
}
newData[dsstr.len - 1] = tmpchar;
}
// Create an appropriatly sized new array
dsstr.data = new char[dsstr.len + 1];
for (size_t i = 0; i < dsstr.len; i++) {
dsstr.data[i] = newData[i];
}
// Add null terminator
dsstr.data[dsstr.len] = '\0';
// Clean up memory
delete[] newData;
return in;
}
std::istream& getline(std::istream& in, DSString& dsstr) {
// Clean up old data
delete[] dsstr.data;
dsstr.len = 0;
size_t tmpLen = 5; // Length of the newData char[], scales exponentially to minimize copying
char* newData = new char[tmpLen];
char tmpchar; // Holds each char from the input buffer
// Read in each char from the input buffer until we hit a newline
while (in.get(tmpchar) && tmpchar != '\n') {
dsstr.len++; // Holds the actual length of the data that's been read in
// Scale if needed
if (dsstr.len > tmpLen) {
// Expand char[]
tmpLen = tmpLen * 2;
char* tmpData = new char[tmpLen];
// Copy data from old/shorter char[] to new/longer char[]
for (size_t i = 0; i < dsstr.len - 1; i++) {
tmpData[i] = newData[i];
}
// delete old char[]
delete[] newData;
newData = nullptr;
// Set newData equal to tmpData
newData = tmpData;
}
newData[dsstr.len - 1] = tmpchar;
}
// Create an appropriatly sized new array
dsstr.data = new char[dsstr.len + 1];
for (size_t i = 0; i < dsstr.len; i++) {
dsstr.data[i] = newData[i];
}
// Add null terminator
dsstr.data[dsstr.len] = '\0';
// Clean up memory
delete[] newData;
return in;
}
std::istream& getline(std::istream& in, DSString& dsstr, char delim) {
// Clean up old data
delete[] dsstr.data;
dsstr.len = 0;
size_t tmpLen = 5; // Length of the newData char[], scales exponentially to minimize copying
char* newData = new char[tmpLen];
char tmpchar; // Holds each char from the input buffer
// Read in each char from the input buffer until we hit a newline
while (in.get(tmpchar) && tmpchar != delim) {
dsstr.len++; // Holds the actual length of the data that's been read in
// Scale if needed
if (dsstr.len > tmpLen) {
// Expand char[]
tmpLen = tmpLen * 2;
char* tmpData = new char[tmpLen];
// Copy data from old/shorter char[] to new/longer char[]
for (size_t i = 0; i < dsstr.len - 1; i++) {
tmpData[i] = newData[i];
}
// delete old char[]
delete[] newData;
newData = nullptr;
// Set newData equal to tmpData
newData = tmpData;
}
newData[dsstr.len - 1] = tmpchar;
}
// Create an appropriatly sized new array
dsstr.data = new char[dsstr.len + 1];
for (size_t i = 0; i < dsstr.len; i++) {
dsstr.data[i] = newData[i];
}
// Add null terminator
dsstr.data[dsstr.len] = '\0';
// Clean up memory
delete[] newData;
return in;
}
/**
* @brief Finds the first occurrence of the given substring
*
* @param substr The string to search for
* @param start The index to start searching from
* @return size_t The index of the first occurrence of the substring or npos if not found
*/
size_t DSString::find(const DSString& substr, size_t start) {
// validate start index
if (start >= len) {
throw std::out_of_range("Start index out of range");
}
// validate substring length
if (substr.len + start > len) {
throw std::out_of_range("Substring length out of range");
}
// Loop through each character in this string
for (size_t i = start; i < len; i++) {
// Check if the current character matches the first character of the substring
if (data[i] == substr.data[0]) {
// Loop through each character in the substring
for (size_t j = 0; j < substr.len; j++) {
// Check if the current character in the substring matches the current character in this
// string
if (data[i + j] != substr.data[j]) {
// If the characters don't match, break out of the loop
break;
} else if (j == substr.len - 1) {
// If we get here, all characters in the substring matched
// Return the index of the first character in the substring
return i;
}
}
}
}
// Substring not found, return npos
return npos;
}
/**
* @brief Finds the first occurrence of the given substring
*
* @param substr The cstring to search for (must be null terminated)
* @param start The index to start searching from
* @returns The index of the first character in the substring, or npos if the substring is not found
*/
size_t DSString::find(const char* substr, size_t start) {
// Validate start index
if (start >= len) {
throw std::out_of_range("Start index out of range");
}
// Determine length of substring
size_t substrLen = 0;
while (substr[substrLen] != '\0') {
substrLen++;
}
// Validate substring length
if (substrLen + start > len) {
return npos;
}
// Loop through each character in this string
for (size_t i = start; i < len; i++) {
// Check if the current character matches the first character of the substring
if (data[i] == substr[0]) {
// Loop through each character in the substring
for (size_t j = 0; j < substrLen; j++) {
// Check if the current character in the substring matches the current character in this
// string
if (data[i + j] != substr[j]) {
// If the characters don't match, break out of the loop
break;
} else if (j == substrLen - 1) {
// If we get here, all characters in the substring matched
// Return the index of the first character in the substring
return i;
}
}
}
}
// Substring not found, return npos
return npos;
}
/**
* @brief Finds the first occurrence of the given substring
*
* @param c The character to search for
* @param start The index to start searching from
*/
size_t DSString::find(char c, size_t start) {
// Validate start index
if (start >= len) {
throw std::out_of_range("Start index out of range");
}
// Loop through each character in this string
for (size_t i = start; i < len; i++) {
// Check if the current character matches the given character
if (data[i] == c) {
// Return the index of the character
return i;
}
}
// Character not found, return npos
return npos;
}