-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.h
65 lines (60 loc) · 1.81 KB
/
Utils.h
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
#pragma once
template <typename Map>
bool ContainerEqual (Map const &lhs, Map const &rhs) {
return lhs.size() == rhs.size()
&& std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
std::string Quoted(const std::string& str) {
return "\"" + str + "\"";
}
typedef std::pair<std::string, std::string> AttributeType;
/**
* @brief combine key value pairs into a dict in json format
* @param[in] items key value paris
* @param[in] num_indents number of indents for each item, -1 for no
* indent and no newline
* @return a string representing dict in json format
*/
std::string JsonDict(const std::vector<AttributeType>& items,
const int num_indents = 0) {
std::string indents;
std::string s;
if (num_indents < 0) {
indents = "";
s = "{";
} else {
indents = "\n" + std::string(num_indents, ' ');
s = std::string(num_indents, ' ') + "{";
}
for (auto& item : items) {
s += indents + item.first + ":" + item.second + ",";
}
s.pop_back(); // remove the last comma
s += indents + "}";
return s;
}
/**
* @brief combine items into a list in json format
* @param[in] items vector of items
* @param[in] num_indents number of indents for each item, -1 for no
* indent and no newline
* @return a string representing list in json format
*/
std::string JsonList(const std::vector<std::string>& items,
const int num_indents = 0) {
std::string indents;
std::string s;
if (num_indents < 0) {
indents = "";
s = "[";
} else {
indents = "\n" + std::string(num_indents, ' ');
s = std::string(num_indents, ' ') + "[";
}
for (auto& item : items) {
s += indents + item + ",";
}
s.pop_back(); // remove the last comma
s += indents + "]";
return s;
}