-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystr.cpp
229 lines (181 loc) · 3.16 KB
/
mystr.cpp
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
#include <cstdlib>
#include <utility>
#include "mystr.h"
namespace my
{
str::str()
: m_length(0)
, m_capacity(n_initial_capacity)
, m_chars(static_cast<char*>(malloc(sizeof(char) * m_capacity)))
{
m_chars[0] = '\0';
}
str::str(const char* chars)
: str()
{
append(chars);
}
str::str(const char ch)
: str()
{
append(ch);
}
str::str(const str& other)
: str(other.cstr())
{
}
str::str(str&& other)
: m_length(other.m_length)
, m_capacity(other.m_capacity)
, m_chars(other.m_chars)
{
other.m_chars = nullptr;
}
str::~str()
{
free(m_chars);
}
void str::append(char ch)
{
if (m_length + 1 >= m_capacity)
{
m_capacity *= 2;
m_chars = static_cast<char*>(realloc(m_chars, m_capacity * sizeof(char)));
}
m_chars[m_length++] = ch;
m_chars[m_length] = '\0';
}
void str::append(const char* chars)
{
while (*chars != '\0')
{
append(*chars);
++chars;
}
}
size_t str::len() const
{
return m_length;
}
const char* str::cstr() const
{
return m_chars;
}
str& str::operator+=(char ch)
{
append(ch);
return *this;
}
/* :
str& str::operator+=(const char* chars)
{
append(chars);
return *this;
}
*/
str& str::operator+=(const str& other)
{
append(other.m_chars);
return *this;
}
str& str::operator*=(size_t times)
{
if (times < 1)
{
m_chars[0] = '\0';
m_length = 0;
}
else
{
const size_t original_length = m_length;
for (size_t t = 1; t < times; ++t)
{
for (size_t i = 0; i < original_length; ++i)
{
// ¤]¥i¥H: *this += m_chars[i];
append(m_chars[i]);
}
}
}
return *this;
}
/*:
str str::operator+(char ch) const
{
str new_str = m_chars;
new_str.append(ch);
return new_str;
}
*/
/*
str str::operator+(const char* string) const
{
str new_str = m_chars;
new_str.append(string);
return new_str;
}
*/
str str::operator+(const str& other) const
{
str new_str = m_chars;
new_str.append(other.cstr());
return new_str;
}
str str::operator*(size_t times) const
{
// my answer:
// str new_str;
//
// if (times >= 1)
// {
// for (size_t t = 0; t < times; ++t)
// new_str.append(m_chars);
// }
//
// return new_str;
// better answer:
str result(m_chars);
result *= times;
return result;
}
str& str::operator=(const str& other)
{
// m_chars[0] = '\0';
*m_chars = '\0';
m_length = 0;
append(other.m_chars);
return *this;
}
str& str::operator=(str&& other)
{
m_length = other.m_length;
m_capacity = other.m_capacity;
// swap (include <utility>)
// std::swap(m_chars, other.m_chars);
char* temp = m_chars;
m_chars = other.m_chars;
other.m_chars = temp;
return *this;
}
str operator+(const char str1[], const str& str2)
{
str result = str1;
return result + str2;
}
str operator*(size_t times, const str& string)
{
str result(string);
// return result *= times; // return str&
result *= times;
// optimization (copy elision)
return result;
}
std::ostream& operator<<(std::ostream& os, const str& mystr)
{
return os << mystr.cstr();
}
char str::operator[](size_t index) const
{
return m_chars[index];
}
}