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 : 7338210 : static std::string json_escape(const std::string& inS)
12 : : {
13 [ - + ]: 7338210 : std::string outS;
14 [ - + + - ]: 7338210 : outS.reserve(inS.size() * 2);
15 : :
16 [ - + + + ]: 1638781093 : for (unsigned int i = 0; i < inS.size(); i++) {
17 [ + + ]: 1631442883 : unsigned char ch = static_cast<unsigned char>(inS[i]);
18 : 1631442883 : const char *escStr = escapes[ch];
19 : :
20 [ + + ]: 1631442883 : if (escStr)
21 [ + - ]: 26723 : outS += escStr;
22 : : else
23 [ + - ]: 3262859043 : outS += static_cast<char>(ch);
24 : : }
25 : :
26 : 7338210 : return outS;
27 : 0 : }
28 : :
29 : : // NOLINTNEXTLINE(misc-no-recursion)
30 : 6208173 : std::string UniValue::write(unsigned int prettyIndent,
31 : : unsigned int indentLevel) const
32 : : {
33 [ + - ]: 6208173 : std::string s;
34 [ + - ]: 6208173 : s.reserve(1024);
35 : :
36 : 6208173 : unsigned int modIndent = indentLevel;
37 [ + + ]: 6208173 : if (modIndent == 0)
38 : 280925 : modIndent = 1;
39 : :
40 [ + + + + : 6208173 : switch (typ) {
+ + - ]
41 : 28921 : case VNULL:
42 [ + - ]: 28921 : s += "null";
43 : : break;
44 : 771764 : case VOBJ:
45 [ + - ]: 771764 : writeObject(prettyIndent, modIndent, s);
46 : : break;
47 : 270190 : case VARR:
48 [ + - ]: 270190 : writeArray(prettyIndent, modIndent, s);
49 : : break;
50 : 2471494 : case VSTR:
51 [ + - + - : 7414482 : s += "\"" + json_escape(val) + "\"";
- + ]
52 : 2471494 : break;
53 : 2269091 : case VNUM:
54 [ - + ]: 2269091 : s += val;
55 : : break;
56 : 396713 : case VBOOL:
57 [ + + ]: 6604886 : s += (val == "1" ? "true" : "false");
58 : : break;
59 : : }
60 : :
61 : 6208173 : return s;
62 : 0 : }
63 : :
64 : 20738 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
65 : : {
66 : 20738 : s.append(prettyIndent * indentLevel, ' ');
67 : 20738 : }
68 : :
69 : : // NOLINTNEXTLINE(misc-no-recursion)
70 : 270190 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
71 : : {
72 : 270190 : s += "[";
73 [ + + ]: 270190 : if (prettyIndent)
74 : 2932 : s += "\n";
75 : :
76 [ - + + + ]: 1328545 : for (unsigned int i = 0; i < values.size(); i++) {
77 [ + + ]: 1058355 : if (prettyIndent)
78 : 7946 : indentStr(prettyIndent, indentLevel, s);
79 [ - + ]: 2116710 : s += values[i].write(prettyIndent, indentLevel + 1);
80 [ - + + + ]: 1058355 : if (i != (values.size() - 1)) {
81 : 894211 : s += ",";
82 : : }
83 [ + + ]: 1058355 : if (prettyIndent)
84 : 7946 : s += "\n";
85 : : }
86 : :
87 [ + + ]: 270190 : if (prettyIndent)
88 : 2932 : indentStr(prettyIndent, indentLevel - 1, s);
89 : 270190 : s += "]";
90 : 270190 : }
91 : :
92 : : // NOLINTNEXTLINE(misc-no-recursion)
93 : 771764 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
94 : : {
95 : 771764 : s += "{";
96 [ + + ]: 771764 : if (prettyIndent)
97 : 3182 : s += "\n";
98 : :
99 [ - + + + ]: 5638480 : for (unsigned int i = 0; i < keys.size(); i++) {
100 [ + + ]: 4866716 : if (prettyIndent)
101 : 6678 : indentStr(prettyIndent, indentLevel, s);
102 [ + - - + ]: 14600148 : s += "\"" + json_escape(keys[i]) + "\":";
103 [ + + ]: 4866716 : if (prettyIndent)
104 : 6678 : s += " ";
105 [ - + ]: 9733432 : s += values.at(i).write(prettyIndent, indentLevel + 1);
106 [ - + + + ]: 4866716 : if (i != (values.size() - 1))
107 : 4102960 : s += ",";
108 [ + + ]: 4866716 : if (prettyIndent)
109 : 6678 : s += "\n";
110 : : }
111 : :
112 [ + + ]: 771764 : if (prettyIndent)
113 : 3182 : indentStr(prettyIndent, indentLevel - 1, s);
114 : 771764 : s += "}";
115 : 771764 : }
116 : :
|