-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmstring.cpp
195 lines (173 loc) · 2.86 KB
/
mstring.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
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include "mstring.h"
#include <iostream>
void String::Append(char ch)
{
if (current == (length - 1))
{
length *= 2;
char * new_str = new char[length];
for (int i = 0; i < current; i++)
{
new_str[i] = contents[i];
}
delete [] contents;
contents = new_str;
}
contents[current] = ch;
current++;
contents[current] = '\0';
}
String::String(String & str)
{
int i = 0;
contents = new char[SIZE];
current = 0;
length = SIZE;
while (str[i] != -1)
{
Append(str[i]);
i++;
}
}
String::String(const char * p_contents)
{
int i = 0;
contents = new char[SIZE];
current = 0;
length = SIZE;
while (p_contents[i] != '\0')
{
Append(p_contents[i]);
i++;
}
}
String::~String()
{
delete[] contents;
}
const String & String::operator=(String & str)
{
int i = 0;
current = 0;
while (str[i] != -1)
{
Append(str[i]);
i++;
}
return *this;
}
const String & String::operator=(const char * p_contents)
{
int i = 0;
current = 0;
while (p_contents[i] != '\0')
{
Append(p_contents[i]);
i++;
}
return *this;
}
const String & String::operator+=(const char * p_contents)
{
int i = 0;
while (p_contents[i] != '\0')
{
Append(p_contents[i]);
i++;
}
return *this;
}
const String & String::operator+=(char ch)
{
Append(ch);
return *this;
}
const String & String::operator+=(String & str)
{
Append(str);
return *this;
}
char String::operator[](int i)
{
return At(i);
}
bool String::operator==(String & str)
{
int i = 0;
if (current != str.current)
return false;
for (int i = 0; i < current; i++)
{
if (contents[i] != str.contents[i])
return false;
}
return true;
}
bool String::operator==(const char * p_contents)
{
int i = 0;
while (contents[i] != '\0')
{
if (p_contents[i] != contents[i])
return false;
i++;
}
if (p_contents[i] != contents[i])
return false;
return true;
}
int String::GetLength()
{
return current;
}
int String::FindSymbol(char ch)
{
int i = 0;
for (int i = 0; i < current; i++)
{
if (contents[i] == ch)
return i;
}
return -1;
}
char String::At(int i)
{
if ((i < current) && (i >= 0))
return contents[i];
else
return -1;
}
void String::Print()
{
std::cout << contents;
//printf("%s", contents);
}
void String::Append(String& str)
{
int i = 0;
while (str[i] != -1)
{
Append(str[i]);
i++;
}
}
void String::Append(String && str)
{
int i = 0;
while (str[i] != -1)
{
Append(str[i]);
i++;
}
}
char String::PopBack()
{
if (current == 0)
return '\0';
current--;
char t = contents[current];
contents[current] = '\0';
return t;
}