-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceTable.cpp
More file actions
63 lines (56 loc) · 1.38 KB
/
PieceTable.cpp
File metadata and controls
63 lines (56 loc) · 1.38 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
#include "PieceTable.hpp"
PieceTable::PieceTable()
{
original = "";
pieces.push_back({true, 0, 0});
}
PieceTable::PieceTable(const std::string &text)
{
original = text;
pieces.push_back({true, 0, static_cast<int>(text.size())});
}
void PieceTable::insert(const std::string &text)
{
// Save the current state for undo operation.
undoStack.push(pieces);
// Clear the redo stack as a new operation is being performed.
while (!redoStack.empty())
redoStack.pop();
// Simple insert at end of the string, without handling more complex cases
add += text;
pieces.push_back({false, static_cast<int>(add.size()) - static_cast<int>(text.size()), static_cast<int>(text.size())});
}
std::string PieceTable::getText()
{
std::string result;
for (auto &piece : pieces)
{
if (piece.isOriginal)
result += original.substr(piece.start, piece.length);
else
result += add.substr(piece.start, piece.length);
}
return result;
}
void PieceTable::undo()
{
if (!undoStack.empty())
{
// Save the current state for redo operation.
redoStack.push(pieces);
// Revert to the previous state.
pieces = undoStack.top();
undoStack.pop();
}
}
void PieceTable::redo()
{
if (!redoStack.empty())
{
// Save the current state for undo operation.
undoStack.push(pieces);
// Revert to the next state.
pieces = redoStack.top();
redoStack.pop();
}
}