-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
299 lines (237 loc) · 5.45 KB
/
String.cpp
File metadata and controls
299 lines (237 loc) · 5.45 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
// ===========================================================================
// String.cpp
// ===========================================================================
#include <stdexcept>
#include "String.h"
// c'tors and d'tor
String::String()
{
// empty string
m_len = 0;
m_ptr = (char*) 0;
}
String::String(const String& s)
{
// allocate buffer
m_len = s.m_len;
m_ptr = new char[m_len];
// copy contents of buffer
for (int i = 0; i < m_len; i++) {
m_ptr[i] = s.m_ptr[i];
}
}
String::String(const char* s)
{
// length of string
m_len = 0;
while (s[m_len] != '\0')
m_len++;
// allocate buffer
m_ptr = new char[m_len];
// copy string
for (int i = 0; i < m_len; i++)
m_ptr[i] = s[i];
}
String::~String()
{
delete[] m_ptr;
}
// getter
int String::length() const
{
return m_len;
}
char& String::operator[] (int index)
{
// check parameter
if (index < 0 || index >= m_len)
throw std::out_of_range("Wrong index");
return m_ptr[index];
}
// public methods
bool String::insert(const String & s, int ofs)
{
if (ofs > m_len)
return false;
// allocate new buffer
char* tmp = new char[m_len + s.m_len];
for (int i = 0; i < ofs; i++) { // copy first part
tmp[i] = m_ptr[i];
}
for (int i = 0; i < s.m_len; i++) { // copy string to insert
tmp[ofs + i] = s.m_ptr[i];
}
for (int i = ofs; i < m_len; i++) { // copy second part
tmp[s.m_len + i] = m_ptr[i];
}
delete[] m_ptr; // release old buffer
m_ptr = tmp; // switch buffer
m_len += s.m_len; // adjust buffer length
return true;
}
void String::append(const String& s)
{
insert(s, m_len);
}
bool String::remove(int ofs, int num)
{
if (ofs + num > m_len)
return false;
// allocate new buffer
char* tmp = new char[m_len - num];
// copy remaining parts
for (int i = 0; i < ofs; i++) { // first part
tmp[i] = m_ptr[i];
}
for (int i = ofs + num; i < m_len; i++) { // second part
tmp[i - num] = m_ptr[i];
}
delete[] m_ptr; // release old buffer
m_ptr = tmp; // switch buffer
m_len -= num; // adjust buffer length
return true;
}
String String::subString(int ofs, int num) const
{
String empty;
if (ofs < 0) {
return empty;
}
if (ofs > m_len) {
return empty;
}
if (ofs + num > m_len) {
return empty;
}
// allocate temporary buffer
char* tmp = new char[num + 1];
// copy partial string
for (int i = 0; i < num; i++) {
tmp[i] = m_ptr[ofs + i];
}
tmp[num] = '\0'; // terminate buffer
// create result object
String s(tmp);
delete[] tmp; // release temporary buffer
return s;
}
int String::find(const String & s) const
{
for (int i = 0; i < m_len - s.m_len + 1; i++)
{
int k = 0;
while (k < s.m_len)
{
if (m_ptr[i + k] != s.m_ptr[k])
break;
k++;
}
if (k == s.m_len)
return i;
}
return -1;
}
void String::toUpper()
{
for (int i = 0; i < m_len; i++) {
if (m_ptr[i] >= 'a' && m_ptr[i] <= 'z') {
m_ptr[i] -= ('a' - 'A');
}
}
}
void String::toLower()
{
for (int i = 0; i < m_len; i++) {
if (m_ptr[i] >= 'A' && m_ptr[i] <= 'Z') {
m_ptr[i] += ('a' - 'A');
}
}
}
String String::left(int num) const
{
return subString(0, num);
}
String String::right(int num) const
{
return subString(length() - num, num);
}
// assignment operator
String & String::operator= (const String & s)
{
if (this != &s)
{
// delete old string (left side)
delete[] m_ptr;
// allocate new buffer
m_len = s.m_len;
m_ptr = new char[m_len];
// deep copy
for (int i = 0; i < m_len; i++) {
m_ptr[i] = s.m_ptr[i];
}
}
return *this;
}
// string concatenation
String operator+ (const String & s1, const String & s2)
{
String s(s1);
s.append(s2);
return s;
}
const String & operator+= (String & s1, const String & s2)
{
s1.append(s2);
return s1;
}
// comparison operators
bool operator== (const String & s1, const String & s2)
{
if (s1.m_len != s2.m_len) {
return false;
}
for (int i = 0; i < s1.m_len; i++) {
if (s1.m_ptr[i] != s2.m_ptr[i]) {
return false;
}
}
return true;
}
bool operator!= (const String & s1, const String & s2)
{
return !(s1 == s2);
}
// output operator
std::ostream & operator<< (std::ostream & os, const String & s)
{
os << '"';
for (int i = 0; i < s.m_len; i++) {
os << s.m_ptr[i];
}
os << "\"[" << s.m_len << ']';
return os;
}
// input operator
std::istream & operator>> (std::istream & is, String & s)
{
char line[256];
is.getline(line, 256);
// calculate length of string
int len = 0;
while (line[len] != '\0') {
len++;
}
// release old buffer, if any
delete[] s.m_ptr;
// allocate new buffer and copy string
s.m_ptr = new char[len];
for (int i = 0; i < len; i++) {
s.m_ptr[i] = line[i];
}
// adjust buffer length
s.m_len = len;
return is;
}
// ===========================================================================
// End-of-File
// ===========================================================================