-
Notifications
You must be signed in to change notification settings - Fork 3
/
Serialize.cpp
183 lines (165 loc) · 4.72 KB
/
Serialize.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <sstream>
#include <iomanip>
#include "Serialize.h"
#include "Overload.h"
STable arrayToSTable(std::vector<SValue> &&array) {
STable result;
for (size_t i{}; i < array.size(); ++i) {
if (!std::holds_alternative<std::monostate>(array[i]))
result[static_cast<double>(i + 1)] = std::move(array[i]);
}
return result;
}
std::vector<SValue> sTableToArray(STable&& table) {
std::vector<SValue> result;
for (auto &entry : table) {
auto key(std::get<double>(entry.first) - 1);
auto iKey(static_cast<size_t>(key));
if (iKey != key)
throw std::runtime_error("non-integer key");
if (result.size() <= iKey)
result.resize(iKey + 1);
result[iKey] = std::move(entry.second);
}
return result;
}
namespace {
struct Serializer {
std::string operator()(std::monostate) const {
return "!";
}
std::string operator()(double x) const {
std::ostringstream os;
os << "#" << std::setprecision(std::numeric_limits<double>::max_digits10) << x << "@";
return os.str();
}
std::string operator()(const std::string &x) const {
std::string result{"@"};
for (char i : x)
if (i == '@')
result += "@.";
else
result.push_back(i);
result += "@~";
return result;
}
std::string operator()(bool x) const {
return x ? "+" : "-";
}
std::string operator()(const STable &x) const {
std::string result{"="};
for (auto &i : x) {
result += std::visit(*this, static_cast<const SKeyBase&>(i.first));
result += serialize(i.second);
}
result.push_back('!');
return result;
}
};
}
std::string serialize(const SValue &x) {
return std::visit(Serializer{}, static_cast<const SValueBase&>(x));
}
std::string serialize(const STable &x) {
return Serializer{}(x);
}
std::unique_ptr<Deserializer::State> Deserializer::State::yield() {
auto s(std::move(env.s));
env.s = std::move(p);
return s;
}
void Deserializer::Number::shift(const char *data, size_t size) {
while (size) {
char now{*data};
++data; --size;
if ('@' == now) {
auto s(yield());
return env.s->reduce(std::stod(buffer), data, size);
} else {
buffer.push_back(now);
}
}
}
void Deserializer::String::shift(const char *data, size_t size) {
while (size) {
char now{*data};
++data; --size;
if (escape) {
if ('.' == now) {
buffer.push_back('@');
escape = false;
} else if ('~' == now) {
auto s(yield());
return env.s->reduce(std::move(buffer), data, size);
} else {
throw std::runtime_error("unknown escape");
}
} else if ('@' == now) {
escape = true;
} else {
buffer.push_back(now);
}
}
}
void Deserializer::Root::shift(const char *data, size_t size) {
if (!size) return;
char now{*data};
++data; --size;
auto s(yield());
switch (now) {
case '!':
return env.s->reduce(std::monostate{}, data, size);
case '#':
env.enter<Number>();
return env.s->shift(data, size);
case '@':
env.enter<String>();
return env.s->shift(data, size);
case '+':
return env.s->reduce(true, data, size);
case '-':
return env.s->reduce(false, data, size);
case '=':
env.enter<Table>();
return env.s->shift(data, size);
default:
throw std::runtime_error("invalid tag");
}
}
namespace {
template<typename T, typename ...Ts> struct Contains {};
template<typename T> struct Contains<T> : std::false_type {};
template<typename T, typename U, typename ...Ts> struct Contains<T, U, Ts...> :
std::bool_constant<std::is_same_v<T, U> || Contains<T, Ts...>::value> {};
template<typename T, typename U> struct VariantContains {};
template<typename T, typename ...Ts> struct VariantContains<T, std::variant<Ts...>> : Contains<T, Ts...> {};
}
void Deserializer::Table::reduce(SValue x, const char *data, size_t size) {
if (key.has_value()) {
result.emplace(std::move(*key), std::move(x));
key.reset();
} else if (std::holds_alternative<std::monostate>(x)) {
auto s(yield());
return env.s->reduce(std::move(result), data, size);
} else {
std::visit([this](auto &x) {
if constexpr (VariantContains<std::remove_reference_t<decltype(x)>, SKeyBase>::value)
key.emplace(std::move(x));
else
throw std::runtime_error("invalid table key");
}, static_cast<SValueBase&>(x));
}
env.enter<Root>();
env.s->shift(data, size);
}
void Deserializer::Table::init() {
env.enter<Root>();
}
void Deserializer::Start::reduce(SValue x, const char *data, size_t size) {
env.cb(std::move(x));
env.enter<Root>();
env.s->shift(data, size);
}
void Deserializer::Start::init() {
env.enter<Root>();
}