-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyString.h
More file actions
180 lines (145 loc) · 4.01 KB
/
Copy pathMyString.h
File metadata and controls
180 lines (145 loc) · 4.01 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
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>
template<typename CharT>
class MyBasicString {
private:
CharT* __data;
size_t __length;
size_t __size;
const size_t __buffer = 10;
public:
// Constructor with no arguments
MyBasicString() : __data(new CharT[1]) {
__data[0] = '\0';
__length = 0;
__size = 0;
}
// Constructor with a character array
MyBasicString(const char* source) {
if (source == nullptr) {
__data = new CharT[1];
__data[0] = '\0';
} else {
__length = std::strlen(source);
__size = __length + __buffer;
__data = new CharT[__size];
strcpy(__data, source);
}
}
// Copy constructor
MyBasicString(const MyBasicString& source) {
__data = new CharT[source.size()];
strcpy(__data, source.c_str());
__length = source.length();
__size = source.size();
}
// Move constructor
MyBasicString(MyBasicString&& source) {
if(__length > 0) {
delete [] __data;
}
__data = source.c_str();
__length = source.length();
__size = source.size();
source.release();
}
// Destructor
~MyBasicString() {
delete[] __data;
__data = nullptr;
__length = 0;
__size = 0;
}
/* helper functions */
void clear () {
if (__data != nullptr) {
CharT *temp = __data;
while (*__data != '\0') {
*__data = '\0';
__data++;
}
__data = temp;
__length = strlen(__data);
}
}
void release() {
__data = nullptr;
__length = 0;
__size = 0;
}
// Get size
size_t size() const {
return __size;
}
// Get length
size_t length() const {
return __length;
}
// Get naked __data pointer
CharT* c_str() const {
return __data;
}
// Print
void print() const {
std::cout << __data << std::endl;
}
/* string manipulations */
// Concatenate
MyBasicString operator+(const MyBasicString& source) const {
size_t tempSize = __length + source.length() + __buffer;
CharT* temp = new CharT[tempSize];
if (__data != nullptr && (*__data != '\0')) {
std::strcpy(temp, __data);
}
std::strcat(temp, source.c_str());
MyBasicString result(temp);
delete[] temp;
return result;
}
// append
MyBasicString append(const MyBasicString& source) {
if (__size <= __length + strlen(source.c_str())) {
size_t newSize = __length + source.length() + __buffer;
CharT* temp = new CharT[newSize];
std::strcpy(temp, __data);
std::strcat(temp, source.c_str());
delete[] __data;
__data = temp;
__length = std::strlen(__data);
__size = newSize;
}
else {
//copy new string after the __length position
std::strcpy(__data, source.c_str());
__length += __length + source.length();
}
return this;
}
// prepend
MyBasicString prepend(const MyBasicString& source) {
size_t newSize = __length + source.length() + __buffer;
CharT* temp = new CharT[newSize];
std::strcat(temp, source.c_str());
std::strcpy(temp, __data);
delete[] __data;
__data = temp;
__length = std::strlen(__data);
__size = newSize;
return this;
}
// Compare
bool operator==(const MyBasicString& source) const {
return std::strcmp(__data, source.c_str()) == 0;
}
// Access character by index
CharT& operator[] (int index) const {
if (index < __length)
return __data[index];
else
throw std::out_of_range("Index out of bounds");
}
};
typedef MyBasicString<char> MyString;
#endif // MYSTRING_H