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 : 6386687 : static std::string json_escape(const std::string& inS)
13 : : {
14 [ + - ]: 6386687 : std::string outS;
15 [ + - ]: 6386687 : outS.reserve(inS.size() * 2);
16 : :
17 [ + + ]: 833527493 : for (unsigned int i = 0; i < inS.size(); i++) {
18 [ + + ]: 827140806 : unsigned char ch = static_cast<unsigned char>(inS[i]);
19 : 827140806 : const char *escStr = escapes[ch];
20 : :
21 [ + + ]: 827140806 : if (escStr)
22 [ + - ]: 26310 : outS += escStr;
23 : : else
24 [ + - ]: 1654255302 : outS += static_cast<char>(ch);
25 : : }
26 : :
27 : 6386687 : return outS;
28 : 0 : }
29 : :
30 : : // NOLINTNEXTLINE(misc-no-recursion)
31 : 5326273 : std::string UniValue::write(unsigned int prettyIndent,
32 : : unsigned int indentLevel) const
33 : : {
34 [ + - ]: 5326273 : std::string s;
35 [ + - ]: 5326273 : s.reserve(1024);
36 : :
37 : 5326273 : unsigned int modIndent = indentLevel;
38 [ + + ]: 5326273 : if (modIndent == 0)
39 : 243166 : modIndent = 1;
40 : :
41 [ + + + + : 5326273 : switch (typ) {
+ + - ]
42 : 27538 : case VNULL:
43 [ + - ]: 27538 : s += "null";
44 : : break;
45 : 650902 : case VOBJ:
46 [ + - ]: 650902 : writeObject(prettyIndent, modIndent, s);
47 : : break;
48 : 191927 : case VARR:
49 [ + - ]: 191927 : writeArray(prettyIndent, modIndent, s);
50 : : break;
51 : 2113768 : case VSTR:
52 [ + - + - : 6341304 : s += "\"" + json_escape(val) + "\"";
+ - ]
53 : 2113768 : break;
54 : 1909906 : case VNUM:
55 [ + - ]: 1909906 : s += val;
56 : : break;
57 : 432232 : case VBOOL:
58 [ + + ]: 5758505 : s += (val == "1" ? "true" : "false");
59 : : break;
60 : : }
61 : :
62 : 5326273 : return s;
63 : 0 : }
64 : :
65 : 19433 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
66 : : {
67 : 19433 : s.append(prettyIndent * indentLevel, ' ');
68 : 19433 : }
69 : :
70 : : // NOLINTNEXTLINE(misc-no-recursion)
71 : 191927 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
72 : : {
73 : 191927 : s += "[";
74 [ + + ]: 191927 : if (prettyIndent)
75 : 2762 : s += "\n";
76 : :
77 [ + + ]: 1000167 : for (unsigned int i = 0; i < values.size(); i++) {
78 [ + + ]: 808240 : if (prettyIndent)
79 : 7708 : indentStr(prettyIndent, indentLevel, s);
80 [ + - ]: 1616480 : s += values[i].write(prettyIndent, indentLevel + 1);
81 [ + + ]: 808240 : if (i != (values.size() - 1)) {
82 : 656646 : s += ",";
83 : : }
84 [ + + ]: 808240 : if (prettyIndent)
85 : 7708 : s += "\n";
86 : : }
87 : :
88 [ + + ]: 191927 : if (prettyIndent)
89 : 2762 : indentStr(prettyIndent, indentLevel - 1, s);
90 : 191927 : s += "]";
91 : 191927 : }
92 : :
93 : : // NOLINTNEXTLINE(misc-no-recursion)
94 : 650902 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
95 : : {
96 : 650902 : s += "{";
97 [ + + ]: 650902 : if (prettyIndent)
98 : 2856 : s += "\n";
99 : :
100 [ + + ]: 4923821 : for (unsigned int i = 0; i < keys.size(); i++) {
101 [ + + ]: 4272919 : if (prettyIndent)
102 : 6107 : indentStr(prettyIndent, indentLevel, s);
103 [ + - + - ]: 12818757 : s += "\"" + json_escape(keys[i]) + "\":";
104 [ + + ]: 4272919 : if (prettyIndent)
105 : 6107 : s += " ";
106 [ + - ]: 8545838 : s += values.at(i).write(prettyIndent, indentLevel + 1);
107 [ + + ]: 4272919 : if (i != (values.size() - 1))
108 : 3629174 : s += ",";
109 [ + + ]: 4272919 : if (prettyIndent)
110 : 6107 : s += "\n";
111 : : }
112 : :
113 [ + + ]: 650902 : if (prettyIndent)
114 : 2856 : indentStr(prettyIndent, indentLevel - 1, s);
115 : 650902 : s += "}";
116 : 650902 : }
117 : :
|