-
Notifications
You must be signed in to change notification settings - Fork 574
/
Copy pathContextUpdater.cpp
127 lines (100 loc) · 3.14 KB
/
ContextUpdater.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
116
117
118
119
120
121
122
123
124
125
126
127
#include "stdafx.h"
#include <StringAlgorithm.hpp>
#include <WeaselUtility.h>
#include "Deserializer.h"
#include "ContextUpdater.h"
using namespace weasel;
// ContextUpdater
Deserializer::Ptr ContextUpdater::Create(ResponseParser* pTarget) {
return Deserializer::Ptr(new ContextUpdater(pTarget));
}
ContextUpdater::ContextUpdater(ResponseParser* pTarget)
: Deserializer(pTarget) {}
ContextUpdater::~ContextUpdater() {}
void ContextUpdater::Store(Deserializer::KeyType const& k,
std::wstring const& value) {
if (!m_pTarget->p_context || k.size() < 2)
return;
if (k[1] == L"preedit") {
_StoreText(m_pTarget->p_context->preedit, k, value);
return;
}
if (k[1] == L"aux") {
_StoreText(m_pTarget->p_context->aux, k, value);
return;
}
if (k[1] == L"cand") {
_StoreCand(k, value);
return;
}
}
void ContextUpdater::_StoreText(Text& target,
Deserializer::KeyType k,
std::wstring const& value) {
if (k.size() == 2) {
target.clear();
target.str = unescape_string(value);
return;
}
if (k.size() == 3) {
// ctx.preedit.cursor
if (k[2] == L"cursor") {
std::vector<std::wstring> vec;
split(vec, value, L",");
if (vec.size() < 2)
return;
weasel::TextAttribute attr;
attr.type = HIGHLIGHTED;
attr.range.start = _wtoi(vec[0].c_str());
attr.range.end = _wtoi(vec[1].c_str());
attr.range.cursor = _wtoi(vec[2].c_str());
target.attributes.push_back(attr);
return;
}
}
}
void ContextUpdater::_StoreCand(Deserializer::KeyType k,
std::wstring const& value) {
CandidateInfo& cinfo = m_pTarget->p_context->cinfo;
std::wstringstream ss(value);
boost::archive::text_wiarchive ia(ss);
TryDeserialize(ia, cinfo);
for (auto& cand : cinfo.candies)
cand.str = unescape_string(cand.str);
for (auto& lalel : cinfo.labels)
lalel.str = unescape_string(lalel.str);
for (auto& comment : cinfo.comments)
comment.str = unescape_string(comment.str);
}
// StatusUpdater
Deserializer::Ptr StatusUpdater::Create(ResponseParser* pTarget) {
return Deserializer::Ptr(new StatusUpdater(pTarget));
}
StatusUpdater::StatusUpdater(ResponseParser* pTarget) : Deserializer(pTarget) {}
StatusUpdater::~StatusUpdater() {}
void StatusUpdater::Store(Deserializer::KeyType const& k,
std::wstring const& value) {
if (!m_pTarget->p_status || k.size() < 2)
return;
bool bool_value = (!value.empty() && value != L"0");
if (k[1] == L"schema_id") {
m_pTarget->p_status->schema_id = value;
return;
}
if (k[1] == L"ascii_mode") {
m_pTarget->p_status->ascii_mode = bool_value;
return;
}
if (k[1] == L"composing") {
m_pTarget->p_status->composing = bool_value;
return;
}
if (k[1] == L"disabled") {
m_pTarget->p_status->disabled = bool_value;
return;
}
if (k[1] == L"full_shape") {
m_pTarget->p_status->full_shape = bool_value;
return;
}
}