Branch data Line data Source code
1 : : // Copyright 2014 BitPay Inc.
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or https://opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <univalue.h>
6 : : #include <univalue_escapes.h>
7 : :
8 : : #include <memory>
9 : : #include <string>
10 : : #include <vector>
11 : :
12 : 6503231 : static std::string json_escape(const std::string& inS)
13 : : {
14 [ - + ]: 6503231 : std::string outS;
15 [ - + + - ]: 6503231 : outS.reserve(inS.size() * 2);
16 : :
17 [ - + + + ]: 1049396881 : for (unsigned int i = 0; i < inS.size(); i++) {
18 [ + + ]: 1042893650 : unsigned char ch = static_cast<unsigned char>(inS[i]);
19 : 1042893650 : const char *escStr = escapes[ch];
20 : :
21 [ + + ]: 1042893650 : if (escStr)
22 [ + - ]: 23870 : outS += escStr;
23 : : else
24 [ + - ]: 2085763430 : outS += static_cast<char>(ch);
25 : : }
26 : :
27 : 6503231 : return outS;
28 : 0 : }
29 : :
30 : : // NOLINTNEXTLINE(misc-no-recursion)
31 : 5420623 : std::string UniValue::write(unsigned int prettyIndent,
32 : : unsigned int indentLevel) const
33 : : {
34 [ + - ]: 5420623 : std::string s;
35 [ + - ]: 5420623 : s.reserve(1024);
36 : :
37 : 5420623 : unsigned int modIndent = indentLevel;
38 [ + + ]: 5420623 : if (modIndent == 0)
39 : 251223 : modIndent = 1;
40 : :
41 [ + + + + : 5420623 : switch (typ) {
+ + - ]
42 : 27773 : case VNULL:
43 [ + - ]: 27773 : s += "null";
44 : : break;
45 : 658665 : case VOBJ:
46 [ + - ]: 658665 : writeObject(prettyIndent, modIndent, s);
47 : : break;
48 : 196099 : case VARR:
49 [ + - ]: 196099 : writeArray(prettyIndent, modIndent, s);
50 : : break;
51 : 2121094 : case VSTR:
52 [ + - + - : 6363282 : s += "\"" + json_escape(val) + "\"";
- + ]
53 : 2121094 : break;
54 : 1968864 : case VNUM:
55 [ - + ]: 1968864 : s += val;
56 : : break;
57 : 448128 : case VBOOL:
58 [ + + ]: 5868751 : s += (val == "1" ? "true" : "false");
59 : : break;
60 : : }
61 : :
62 : 5420623 : return s;
63 : 0 : }
64 : :
65 : 20108 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
66 : : {
67 : 20108 : s.append(prettyIndent * indentLevel, ' ');
68 : 20108 : }
69 : :
70 : : // NOLINTNEXTLINE(misc-no-recursion)
71 : 196099 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
72 : : {
73 : 196099 : s += "[";
74 [ + + ]: 196099 : if (prettyIndent)
75 : 2853 : s += "\n";
76 : :
77 [ - + + + ]: 981316 : for (unsigned int i = 0; i < values.size(); i++) {
78 [ + + ]: 785217 : if (prettyIndent)
79 : 7845 : indentStr(prettyIndent, indentLevel, s);
80 [ - + ]: 1570434 : s += values[i].write(prettyIndent, indentLevel + 1);
81 [ - + + + ]: 785217 : if (i != (values.size() - 1)) {
82 : 636407 : s += ",";
83 : : }
84 [ + + ]: 785217 : if (prettyIndent)
85 : 7845 : s += "\n";
86 : : }
87 : :
88 [ + + ]: 196099 : if (prettyIndent)
89 : 2853 : indentStr(prettyIndent, indentLevel - 1, s);
90 : 196099 : s += "]";
91 : 196099 : }
92 : :
93 : : // NOLINTNEXTLINE(misc-no-recursion)
94 : 658665 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
95 : : {
96 : 658665 : s += "{";
97 [ + + ]: 658665 : if (prettyIndent)
98 : 2995 : s += "\n";
99 : :
100 [ - + + + ]: 5040802 : for (unsigned int i = 0; i < keys.size(); i++) {
101 [ + + ]: 4382137 : if (prettyIndent)
102 : 6415 : indentStr(prettyIndent, indentLevel, s);
103 [ + - - + ]: 13146411 : s += "\"" + json_escape(keys[i]) + "\":";
104 [ + + ]: 4382137 : if (prettyIndent)
105 : 6415 : s += " ";
106 [ - + ]: 8764274 : s += values.at(i).write(prettyIndent, indentLevel + 1);
107 [ - + + + ]: 4382137 : if (i != (values.size() - 1))
108 : 3730743 : s += ",";
109 [ + + ]: 4382137 : if (prettyIndent)
110 : 6415 : s += "\n";
111 : : }
112 : :
113 [ + + ]: 658665 : if (prettyIndent)
114 : 2995 : indentStr(prettyIndent, indentLevel - 1, s);
115 : 658665 : s += "}";
116 : 658665 : }
117 : :
|