-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_buffer.cpp
More file actions
41 lines (33 loc) · 886 Bytes
/
text_buffer.cpp
File metadata and controls
41 lines (33 loc) · 886 Bytes
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
#include "text_buffer.h"
TextBuffer::TextBuffer() {
lines.push_back("");
}
void TextBuffer::insertChar(int row, int col, char ch) {
lines[row].insert(col, 1, ch);
}
void TextBuffer::deleteChar(int row, int col) {
if (col == 0) {
mergeLines(row);
return;
}
lines[row].erase(col - 1, 1);
}
void TextBuffer::splitLine(int row, int col) {
std::string tail = lines[row].substr(col);
lines[row] = lines[row].substr(0, col);
lines.insert(lines.begin() + row + 1, tail);
}
void TextBuffer::mergeLines(int row) {
if (row == 0) return;
lines[row - 1] += lines[row];
lines.erase(lines.begin() + row);
}
int TextBuffer::getLineCount() const {
return lines.size();
}
int TextBuffer::getLineLength(int row) const {
return lines[row].size();
}
const std::string& TextBuffer::getLine(int row) const {
return lines[row];
}