-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.cc
67 lines (60 loc) · 1.78 KB
/
test.cc
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
#include <gtest/gtest.h>
#include <test/test.h>
#include <sstream>
namespace turbo
{
TextState TextState::decode(std::string_view input)
{
size_t caret = input.find(chCaret);
if (caret == std::string_view::npos)
throw std::runtime_error((std::ostringstream()
<< "Input text does not have a caret: '" << input << "'"
).str());
size_t anchor = input.find(chAnchor);
if (anchor == std::string_view::npos)
anchor = caret;
else if (anchor < caret)
caret -= 1;
else
anchor -= 1;
auto &&text = std::string(input);
text.erase(
std::remove_if(text.begin(), text.end(), [] (auto &c) { return c == chCaret || c == chAnchor; }),
text.end()
);
return {
std::move(text),
Sci::Position(caret),
Sci::Position(anchor),
};
}
std::string TextState::encode(TextState self)
{
self.text.insert(self.caret, 1, chCaret);
if (self.anchor != -1 && self.anchor != self.caret)
self.text.insert(
self.anchor + (self.caret < self.anchor),
1,
chAnchor
);
return std::move(self.text);
}
TScintilla &createScintilla(TextState state)
{
auto &scintilla = createScintilla();
call(scintilla, SCI_SETTEXT, 0U, (sptr_t) state.text.c_str());
call(scintilla, SCI_SETSEL, state.anchor, state.caret);
return scintilla;
}
TextState getTextState(TScintilla &scintilla)
{
Sci::Position length = call(scintilla, SCI_GETLENGTH, 0U, 0U);
std::string text(length, char());
call(scintilla, SCI_GETTEXT, length + 1, (sptr_t) text.data());
return {
std::move(text),
(Sci::Position) call(scintilla, SCI_GETCURRENTPOS, 0U, 0U),
(Sci::Position) call(scintilla, SCI_GETANCHOR, 0U, 0U),
};
}
} // namespace turbo