-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.h
More file actions
324 lines (257 loc) · 10 KB
/
string.h
File metadata and controls
324 lines (257 loc) · 10 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// Created by Admin on 06.04.2021.
//
#ifndef STRING_STRING_H
#include <iostream>
#include <cstring>
#include <algorithm>
class String {
private:
//ячейки памяти, которые доступны
size_t size_In_Storage_ = 0;
// реальный размер строки
size_t real_Size_Of_Line_ = 0;
//строка
char *str = nullptr;
void ExpandStorage(size_t newSize) {// Этот метод должен быть приватный, так как пользователю снаружи не следует знать о нём
size_t copySizeStorage = size_In_Storage_;
bool mark = 0;
while (copySizeStorage + newSize > size_In_Storage_) {
(size_In_Storage_ += 1) *= 2;
mark = 1;// А есть случаи, когда это не произойдёт?
}
if (mark) {
char *copy = str;
str = new char[++size_In_Storage_];
if (copy != nullptr) {
memcpy(str, copy, real_Size_Of_Line_);
delete[] copy;
}
}
}
void DecreaseStorage() {// аналогично
if (real_Size_Of_Line_ > 0) {
bool mark = 0;
while (real_Size_Of_Line_ < size_In_Storage_ / 4) {
(size_In_Storage_ /= 2);
mark = 1;
}
if (mark) {
char *copy = str;
str = new char[++size_In_Storage_];
if (copy != nullptr) {
memcpy(str, copy, real_Size_Of_Line_);
delete[] copy;
}
}
}
}
[[nodiscard]] size_t size() const {
return size_In_Storage_;
};
public:
//Конструктор по умолчанию
String() = default;
//Конструктор от двух параметров: число n и символ c - создает строку из n копий символа c.
explicit String(size_t sz, char c = '\0') : real_Size_Of_Line_(sz),
str(new char[sz]) {
size_In_Storage_ = sz;
memset(str, c, real_Size_Of_Line_);
};
//Конструктор от C-style строки (const char*).
String(const char *s) : String(strlen(s), '\0') {
memcpy(str, s, real_Size_Of_Line_);
};
String(const char s) : String(1, '\0') {
str[0] = s;
};
//копирующий конструктор
String(const String &s) : String(s.real_Size_Of_Line_, '\0') {
memcpy(str, s.str, real_Size_Of_Line_);
DecreaseStorage();
};
//оператор присваивания
String &operator=(const String &other) {
String copy = other;
swap(copy);
DecreaseStorage();
return *this;
};
void swap(String &s) {
std::swap(real_Size_Of_Line_, s.real_Size_Of_Line_);
std::swap(size_In_Storage_, s.size_In_Storage_);
std::swap(str, s.str);
};
bool operator!=(const String &other);
friend bool operator==(const String &My, const String &other);
friend String operator+(const String &first,
const String &second);
//Квадратные скобки, корректно работающие как для константных, так и для неконстантных строк.
const char &operator[](size_t index) const {
return str[index];
};
char &operator[](size_t index) {
return str[index];
};
//Метод length(), возвращающий размер строки.
[[nodiscard]] size_t length() const {
return real_Size_Of_Line_;
};
//Методы push_back(char) и pop_back(). Учетное время работы должно быть O(1).
void push_back(char c) {
if (real_Size_Of_Line_ + 1 >= size_In_Storage_) {
size_t newSize = real_Size_Of_Line_ + 2 - size_In_Storage_;
real_Size_Of_Line_++;
this->ExpandStorage(newSize);
str[real_Size_Of_Line_ - 1] = c;
} else {
real_Size_Of_Line_++;
str[real_Size_Of_Line_ - 1] = c;
}
};
void pop_back() {
if (real_Size_Of_Line_ != 0) {
real_Size_Of_Line_--;
}
DecreaseStorage();
};
//Методы front() и back(), возвращающие ссылку на начальный и на последний символы строки соответственно.
char &front() {
return str[0];
};
const char &front() const {
return str[0];
};
char &back() {
return str[real_Size_Of_Line_ - 1];
};
const char &back() const {
return str[real_Size_Of_Line_ - 1];
};
//Оператор +=, позволяющий добавить к строке символ или другую строку. Добавление символа должно работать за амортизированное O(1).
String &operator+=(const String &other) {
if (real_Size_Of_Line_ + other.real_Size_Of_Line_ > size_In_Storage_) {
size_t newSize = real_Size_Of_Line_ + other.real_Size_Of_Line_ -
size_In_Storage_;
ExpandStorage(newSize);
}
memcpy(str + real_Size_Of_Line_, other.str, (other.real_Size_Of_Line_));
real_Size_Of_Line_ += other.real_Size_Of_Line_;
return (*this);
};
String &operator+=(const char &other) {
this->push_back(other);
return *this;
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Методы find(substring) и rfind(substring), позволяющие найти самое левое и самое правое вхождение подстроки substring в данную строку.
//Возвращают индекс найденного вхождения. Если вхождений не найдено, нужно вернуть length() (фиктивный индекс).
size_t find(const String &other) const {
if (str != nullptr) {
size_t glob_index = 0, local_index;
while (glob_index < real_Size_Of_Line_) {
local_index = 0;
while (glob_index < real_Size_Of_Line_ &&
str[glob_index] == other[local_index]) {
glob_index++;
local_index++;
}
if (local_index == other.real_Size_Of_Line_) {
return glob_index - local_index;
}
glob_index++;
}
}
return real_Size_Of_Line_;
};
size_t rfind(const String &other) const {
if (str != nullptr) {
size_t glob_index = real_Size_Of_Line_, local_index;
for (size_t i = glob_index; i > 0; i--) {
local_index = 0;
size_t start = i - 1;
while (start < real_Size_Of_Line_ &&
str[start] == other[local_index]) {
start++;
local_index++;
}
if (local_index == other.real_Size_Of_Line_) {
return start - local_index;
}
}
}
return real_Size_Of_Line_;
};
//+Метод substr(start, count), возвращающий подстроку начиная с индекса start и состоящую из count символов.
String substr(size_t start, size_t count) const {
String copy;
size_t newSize = count + 1;
copy.ExpandStorage(newSize);
copy.real_Size_Of_Line_ = newSize;
memcpy(copy.str, str + start, count);
copy.str[count] = '\0';
return copy;
};
//Метод empty(), проверяющий, пуста ли строка.++
bool empty() const {
return real_Size_Of_Line_ == 0;
}
//Метод clear(), позволяющий очистить строку.+
void clear() {
real_Size_Of_Line_ = 0;
size_In_Storage_ = 0;
delete[]this->str;
str = nullptr;
}
//Операторы вывода в поток « и ввода из потока ».++
friend std::ostream &operator<<(std::ostream &out, const String &s);
friend std::istream &operator>>(std::istream &in, String &s);
//Деструктор++
~String() {
clear();
};
};
//Оператор ==, позволяющий проверять строки на совпадение.++
bool operator==(const String &My, const String &other) {
long long h = std::strcmp(My.str, other.str);
if (h == 0) {
return 1;
}
return 0;
};
bool String::operator!=(const String &other) {
long long h = std::strcmp(this->str, other.str);
if (h == 0) {
return 0;
}
return 1;
};
//Оператор +, позволяющий складывать строки с символами, символы со строками и строки со строками.
//+
String operator+(const String &first,
const String &second) {
String copy = first;
copy += second;
return copy;
};
//++
std::ostream &operator<<(std::ostream &out, const String &s) {
for (size_t i = 0; i < s.length(); ++i) {
out << s[i];
}
return out;
}
std::istream &operator>>(std::istream &in, String &s) {
s.clear();
char c;
while (in.get(c)) {
if (!isspace(c)) {
s.push_back(c);
} else {
break;
}
}
return in;
}
#define STRING_STRING_H
#endif //STRING_STRING_H