-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEditor.hpp
143 lines (120 loc) · 3.14 KB
/
TextEditor.hpp
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
#pragma once
#include <vector>
#include <xutility>
#include <string>
#include <iostream>
#include "MultifunctionString.hpp"
namespace TextEditor {
class StringsDictionary {
using MString = MultifunctionString::MString;
private:
size_t _size;
std::vector<MString*> _strings_pointers;
public:
StringsDictionary() {
_size = 0;
}
~StringsDictionary() {
clear();
}
size_t size() {
return _size;
}
bool empty() {
if (_size == 1 && _strings_pointers[0]->size() == 0) return true;
return size() == 0;
}
void insert(size_t ind, std::string str) {
if (_size == 1 && _strings_pointers[0]->empty() && ind == 0) {
delete _strings_pointers[0];
--_size;
}
MString* new_pointer = new MString(str);
_strings_pointers.insert(_strings_pointers.begin() + ind, new_pointer);
++_size;
}
void insert(size_t ind, std::vector<std::string> strings) {
if (_size == 1 && _strings_pointers[0]->empty() && ind == 0) {
delete _strings_pointers[0];
--_size;
}
std::vector<MString*> strings_pointers(strings.size());
for (int i = 0; i < strings.size(); ++i) {
strings_pointers[i] = new MString(strings[i]);
}
_strings_pointers.insert(_strings_pointers.begin() + ind, strings_pointers.begin(), strings_pointers.end());
_size += strings.size();
}
void replace(const MString& old_substr, const MString& new_substr, size_t L = -1, size_t R = -1) {
if (L == -1)
L = 1;
if (R == -1)
R = _size;
for (size_t i = L; i <= R; ++i) {
_strings_pointers[i - 1]->m_replace(old_substr, new_substr);
}
}
void replace_asterisk(const size_t N, size_t L = -1, size_t R = -1) {
if (L == -1)
L = 1;
if (R == -1)
R = _size;
for (size_t i = L; i <= R; ++i) {
_strings_pointers[i - 1]->replace_asterisk(N);
}
}
void remove_leading_zeros(size_t L = -1, size_t R = -1) {
if (L == -1)
L = 1;
if (R == -1)
R = _size;
for (size_t i = L; i <= R; ++i) {
_strings_pointers[i - 1]->remove_leading_zeros();
}
}
void delete_non_growing_subgroups(size_t L = -1, size_t R = -1) {
if (L == -1)
L = 1;
if (R == -1)
R = _size;
for (size_t i = L; i <= R; ++i) {
_strings_pointers[i - 1]->delete_non_growing_subgroups();
}
}
void delete_between(size_t L = -1, size_t R = -1) {
if (L == -1)
L = 1;
if (R == -1)
R = _size;
for (size_t i = L; i <= R; ++i) {
_strings_pointers[i - 1]->delete_between("{", "}");
}
}
void erase(size_t ind) {
if (this->size() == 1) {
*_strings_pointers[0] = "";
return;
}
delete _strings_pointers[ind - 1];
_strings_pointers.erase(_strings_pointers.begin() + ind - 1);
--_size;
}
void clear() {
for (size_t i = 0; i < _size; ++i) {
delete _strings_pointers[i];
}
_strings_pointers.clear();
_size = 0;
}
MString* operator[](size_t i) {
return _strings_pointers[i - 1];
}
friend std::ostream& operator<< (std::ostream& out, const StringsDictionary& dict) {
MString elements = "";
for (auto& pointer : dict._strings_pointers) {
elements += "\n" + *pointer;
}
return out << "size: " << dict._size << elements << "\n";
}
};
}