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 <string>
9 : : #include <vector>
10 : :
11 : 6898533 : static std::string json_escape(const std::string& inS)
12 : : {
13 [ - + ]: 6898533 : std::string outS;
14 [ - + + - ]: 6898533 : outS.reserve(inS.size() * 2);
15 : :
16 [ - + + + ]: 1027744187 : for (unsigned int i = 0; i < inS.size(); i++) {
17 [ + + ]: 1020845654 : unsigned char ch = static_cast<unsigned char>(inS[i]);
18 : 1020845654 : const char *escStr = escapes[ch];
19 : :
20 [ + + ]: 1020845654 : if (escStr)
21 [ + - ]: 24445 : outS += escStr;
22 : : else
23 [ + - ]: 2041666863 : outS += static_cast<char>(ch);
24 : : }
25 : :
26 : 6898533 : return outS;
27 : 0 : }
28 : :
29 : : // NOLINTNEXTLINE(misc-no-recursion)
30 : 5862085 : std::string UniValue::write(unsigned int prettyIndent,
31 : : unsigned int indentLevel) const
32 : : {
33 [ + - ]: 5862085 : std::string s;
34 [ + - ]: 5862085 : s.reserve(1024);
35 : :
36 : 5862085 : unsigned int modIndent = indentLevel;
37 [ + + ]: 5862085 : if (modIndent == 0)
38 : 266660 : modIndent = 1;
39 : :
40 [ + + + + : 5862085 : switch (typ) {
+ + - ]
41 : 29400 : case VNULL:
42 [ + - ]: 29400 : s += "null";
43 : : break;
44 : 699138 : case VOBJ:
45 [ + - ]: 699138 : writeObject(prettyIndent, modIndent, s);
46 : : break;
47 : 239716 : case VARR:
48 [ + - ]: 239716 : writeArray(prettyIndent, modIndent, s);
49 : : break;
50 : 2347731 : case VSTR:
51 [ + - + - : 7043193 : s += "\"" + json_escape(val) + "\"";
- + ]
52 : 2347731 : break;
53 : 2092026 : case VNUM:
54 [ - + ]: 2092026 : s += val;
55 : : break;
56 : 454074 : case VBOOL:
57 [ + + ]: 6316159 : s += (val == "1" ? "true" : "false");
58 : : break;
59 : : }
60 : :
61 : 5862085 : return s;
62 : 0 : }
63 : :
64 : 20416 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
65 : : {
66 : 20416 : s.append(prettyIndent * indentLevel, ' ');
67 : 20416 : }
68 : :
69 : : // NOLINTNEXTLINE(misc-no-recursion)
70 : 239716 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
71 : : {
72 : 239716 : s += "[";
73 [ + + ]: 239716 : if (prettyIndent)
74 : 2898 : s += "\n";
75 : :
76 [ - + + + ]: 1282240 : for (unsigned int i = 0; i < values.size(); i++) {
77 [ + + ]: 1042524 : if (prettyIndent)
78 : 7951 : indentStr(prettyIndent, indentLevel, s);
79 [ - + ]: 2085048 : s += values[i].write(prettyIndent, indentLevel + 1);
80 [ - + + + ]: 1042524 : if (i != (values.size() - 1)) {
81 : 850479 : s += ",";
82 : : }
83 [ + + ]: 1042524 : if (prettyIndent)
84 : 7951 : s += "\n";
85 : : }
86 : :
87 [ + + ]: 239716 : if (prettyIndent)
88 : 2898 : indentStr(prettyIndent, indentLevel - 1, s);
89 : 239716 : s += "]";
90 : 239716 : }
91 : :
92 : : // NOLINTNEXTLINE(misc-no-recursion)
93 : 699138 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
94 : : {
95 : 699138 : s += "{";
96 [ + + ]: 699138 : if (prettyIndent)
97 : 3078 : s += "\n";
98 : :
99 [ - + + + ]: 5249940 : for (unsigned int i = 0; i < keys.size(); i++) {
100 [ + + ]: 4550802 : if (prettyIndent)
101 : 6489 : indentStr(prettyIndent, indentLevel, s);
102 [ + - - + ]: 13652406 : s += "\"" + json_escape(keys[i]) + "\":";
103 [ + + ]: 4550802 : if (prettyIndent)
104 : 6489 : s += " ";
105 [ - + ]: 9101604 : s += values.at(i).write(prettyIndent, indentLevel + 1);
106 [ - + + + ]: 4550802 : if (i != (values.size() - 1))
107 : 3859596 : s += ",";
108 [ + + ]: 4550802 : if (prettyIndent)
109 : 6489 : s += "\n";
110 : : }
111 : :
112 [ + + ]: 699138 : if (prettyIndent)
113 : 3078 : indentStr(prettyIndent, indentLevel - 1, s);
114 : 699138 : s += "}";
115 : 699138 : }
116 : :
|