-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmstring.cpp
More file actions
203 lines (172 loc) · 3.85 KB
/
mstring.cpp
File metadata and controls
203 lines (172 loc) · 3.85 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
#include "mstring.h"
#include "include/include.h"
mstring::mstring(const char *str)
{
size_t len = length(str);
this->str = new char[len + 1];
for (int i = 0; i < len; ++i)
{
this->str[i] = str[i];
}
this->str[len] = '\0';
}
mstring::mstring()
{
this->str = nullptr;
}
size_t mstring::length(const char *str)
{
if (str == nullptr)
return 0;
size_t length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
size_t mstring::length()
{
if (str == nullptr)
return 0;
return length(this->str);
}
mstring::~mstring()
{
delete[] this->str;
}
const char *mstring::value() const
{
return this->str;
}
mstring &mstring::operator=(const mstring &other)
{
if (this != &other)
{
if (this->str != nullptr)
{
delete[] str;
}
size_t len = length(other.str);
this->str = new char[len + 1];
for (int i = 0; i < len; ++i)
{
this->str[i] = other.str[i];
}
this->str[len] = '\0';
}
return *this;
}
mstring::mstring(const mstring &other)
{
size_t len = length(other.str);
this->str = new char[len + 1];
for (int i = 0; i < len; i++)
{
this->str[i] = other.str[i];
}
this->str[len] = '\0';
}
mstring mstring::operator+(const mstring &other)
{
mstring newStr;
newStr.str = new char[length(this->str) + length(other.str) + 1];
int i = 0;
for (; i < length(this->str); i++)
{
newStr.str[i] = this->str[i];
}
for (int j = 0; j < length(other.str); i++, j++)
{
newStr.str[i] = other.str[j];
}
newStr.str[length(this->str) + length(other.str)] = '\0';
return newStr;
}
mstring mstring::operator*(const int times)
{
if (times <= 0)
{
return mstring();
}
size_t single_len = length(this->str);
size_t total_len = single_len * times;
mstring newStr;
if (newStr.str)
delete[] newStr.str;
newStr.str = new char[total_len + 1];
for (int i = 0; i < times; ++i)
{
for (size_t j = 0; j < single_len; ++j)
{
newStr.str[i * single_len + j] = this->str[j];
}
}
newStr.str[total_len] = '\0';
return newStr;
}
bool mstring::operator==(const mstring &other)
{
if (length(other.str) != length(this->str))
return false;
for (int i = 0; i < length(this->str); ++i)
{
if (this->str[i] != other.str[i])
{
return false;
}
}
return true;
}
bool mstring::operator!=(const mstring &other)
{
if (length(other.str) != length(this->str))
{
return true;
}
for (int i = 0; i < length(this->str); ++i)
{
if (this->str[i] != other.str[i])
{
return true;
}
}
return false;
}
char &mstring::operator[](const int index)
{
return this->str[index];
}
mstring::mstring(mstring &&other)
{
this->str = other.str;
other.str = nullptr;
}
std::ostream &operator<<(std::ostream &stream, const mstring &obj)
{
stream << obj.value();
return stream;
}
// TODO: Check for safety
std::istream &operator>>(std::istream &stream, mstring &obj)
{
const int BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
stream >> std::setw(BUFFER_SIZE) >> buffer;
if (stream.good())
{
if (obj.str)
{
delete[] obj.str;
obj.str = nullptr;
}
size_t len = obj.length(buffer);
obj.str = new char[len + 1];
for (int i = 0; i < len; ++i)
{
obj.str[i] = buffer[i];
}
obj.str[len] = '\0';
}
return stream;
}