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
|
#pragma once
#include "unpackPqTextArray.h"
#include <charconv>
#include <iterator>
namespace Slicer {
template<typename T> class UnpackPqTextArrayInto : public UnpackPqTextArray {
public:
UnpackPqTextArrayInto(std::istream & s, std::vector<T> & l) : UnpackPqTextArray(s), list(l) { }
void
consume(const std::string & s) override
{
if constexpr (std::is_arithmetic_v<T>) {
if (std::from_chars(s.c_str(), s.c_str() + s.length(), list.emplace_back()).ec != std::error_code {}) {
throw std::domain_error {"Invalid arithmetic input"};
}
}
else {
list.emplace_back(s);
}
}
private:
std::vector<T> & list;
};
template<typename T>
void
packPqVar(std::ostream & s, const T & l)
{
if constexpr (std::is_arithmetic_v<T>) {
s << l;
}
else {
s << '\"';
std::transform(l.begin(), l.end(), std::ostream_iterator<std::string_view>(s), [](const char & c) {
if (c == '"') {
return std::string_view(R"(\")");
}
return std::string_view(&c, 1);
});
s << '\"';
}
}
template<typename T>
std::vector<T>
unpackPqArray(const std::string & s)
{
std::vector<T> rtn;
std::stringstream ss(s);
UnpackPqTextArrayInto<T> u(ss, rtn);
u.yylex();
return rtn;
}
template<typename T>
std::string
packPqArray(const T & l)
{
std::stringstream ss;
ss << "{";
if (!l.empty()) {
auto i = l.cbegin();
packPqVar(ss, *i);
i++;
while (i != l.cend()) {
ss << ",";
packPqVar(ss, *i++);
}
}
ss << "}";
return std::move(ss).str();
}
}
|