-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
115 lines (92 loc) · 1.99 KB
/
test.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
#include <vector>
#include <string>
class CodePointer {
public:
int line = 0;
int character = 0;
std::vector<std::string> *lines;
std::string CurrentLine() {
return (*lines)[line];
}
bool InvalidPlace() {
if(line > lines->size() - 1)
true;
if(character > CurrentLine().size())
return true;
if(line < 0 || character < 0)
return true;
return false;
}
bool Forward(int places) {
int oldLine = line;
int oldCharacter = character;
for(int i = 0; i < places; i++) {
character++;
if(character > CurrentLine().size()) {
character = 0;
line++;
}
}
if(InvalidPlace()) {
line = oldLine;
character = oldCharacter;
return false;
}
return true;
}
bool Backwards(int places) {
int oldLine = line;
int oldCharacter = character;
for(int i = 0; i < places; i++) {
character--;
if(character < 0) {
line--;
if(line < 0) {
line = oldLine;
character = oldCharacter;
return false;
}
character = CurrentLine().size();
}
}
if(InvalidPlace()) {
line = oldLine;
character = oldCharacter;
return false;
}
return true;
}
void Insert(char newCharacter) {
(*lines)[line].insert((*lines)[line].begin() + character, newCharacter);
Forward(1);
}
void JoinLines() {
(*lines)[line] += (*lines)[line + 1];
(*lines).erase(lines->begin() + line + 1);
}
void Delete() {
int oldLine = line;
Backwards(1);
if(oldLine != line) {
JoinLines();
} else {
(*lines)[line].erase((*lines)[line].begin() + character);
}
}
};
#include <iostream>
int main() {
CodePointer ptr;
ptr.lines = new std::vector<std::string> {"abc cdef", "hijk lm", "nopqr"};
{
ptr.line = 2;
ptr.character = 0;
ptr.Delete();
}
system("clear");
std::vector<std::string> linesCopy = (*ptr.lines);
linesCopy[ptr.line].insert(linesCopy[ptr.line].begin() + ptr.character, '|');
for(std::string s : linesCopy)
std::cout << s << std::endl;
return 0;
}