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