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 : 2926107 : static std::string json_escape(const std::string& inS)
13 : : {
14 [ - + ]: 2926107 : std::string outS;
15 [ - + + - ]: 2926107 : outS.reserve(inS.size() * 2);
16 : :
17 [ - + + + ]: 53515478 : for (unsigned int i = 0; i < inS.size(); i++) {
18 [ + + ]: 50589371 : unsigned char ch = static_cast<unsigned char>(inS[i]);
19 : 50589371 : const char *escStr = escapes[ch];
20 : :
21 [ + + ]: 50589371 : if (escStr)
22 [ + - ]: 113997 : outS += escStr;
23 : : else
24 [ + - ]: 101064745 : outS += static_cast<char>(ch);
25 : : }
26 : :
27 : 2926107 : return outS;
28 : 0 : }
29 : :
30 : : // NOLINTNEXTLINE(misc-no-recursion)
31 : 3116727 : std::string UniValue::write(unsigned int prettyIndent,
32 : : unsigned int indentLevel) const
33 : : {
34 [ + - ]: 3116727 : std::string s;
35 [ + - ]: 3116727 : s.reserve(1024);
36 : :
37 : 3116727 : unsigned int modIndent = indentLevel;
38 [ + + ]: 3116727 : if (modIndent == 0)
39 : 7011 : modIndent = 1;
40 : :
41 [ + + + + : 3116727 : switch (typ) {
+ + - ]
42 : 6184 : case VNULL:
43 [ + - ]: 6184 : s += "null";
44 : : break;
45 : 577720 : case VOBJ:
46 [ + - ]: 577720 : writeObject(prettyIndent, modIndent, s);
47 : : break;
48 : 89142 : case VARR:
49 [ + - ]: 89142 : writeArray(prettyIndent, modIndent, s);
50 : : break;
51 : 586038 : case VSTR:
52 [ + - + - : 1758114 : s += "\"" + json_escape(val) + "\"";
- + ]
53 : 586038 : break;
54 : 1856133 : case VNUM:
55 [ - + ]: 1856133 : s += val;
56 : : break;
57 : 1510 : case VBOOL:
58 [ + + ]: 3118237 : s += (val == "1" ? "true" : "false");
59 : : break;
60 : : }
61 : :
62 : 3116727 : return s;
63 : 0 : }
64 : :
65 : 3402310 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
66 : : {
67 : 3402310 : s.append(prettyIndent * indentLevel, ' ');
68 : 3402310 : }
69 : :
70 : : // NOLINTNEXTLINE(misc-no-recursion)
71 : 89142 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
72 : : {
73 : 89142 : s += "[";
74 [ + + ]: 89142 : if (prettyIndent)
75 : 13225 : s += "\n";
76 : :
77 [ - + + + ]: 845564 : for (unsigned int i = 0; i < values.size(); i++) {
78 [ + + ]: 756422 : if (prettyIndent)
79 : 558220 : indentStr(prettyIndent, indentLevel, s);
80 [ - + ]: 1512844 : s += values[i].write(prettyIndent, indentLevel + 1);
81 [ - + + + ]: 756422 : if (i != (values.size() - 1)) {
82 : 738569 : s += ",";
83 : : }
84 [ + + ]: 756422 : if (prettyIndent)
85 : 558220 : s += "\n";
86 : : }
87 : :
88 [ + + ]: 89142 : if (prettyIndent)
89 : 13225 : indentStr(prettyIndent, indentLevel - 1, s);
90 : 89142 : s += "]";
91 : 89142 : }
92 : :
93 : : // NOLINTNEXTLINE(misc-no-recursion)
94 : 577720 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
95 : : {
96 : 577720 : s += "{";
97 [ + + ]: 577720 : if (prettyIndent)
98 : 571476 : s += "\n";
99 : :
100 [ - + + + ]: 2917789 : for (unsigned int i = 0; i < keys.size(); i++) {
101 [ + + ]: 2340069 : if (prettyIndent)
102 : 2259389 : indentStr(prettyIndent, indentLevel, s);
103 [ + - - + ]: 7020207 : s += "\"" + json_escape(keys[i]) + "\":";
104 [ + + ]: 2340069 : if (prettyIndent)
105 : 2259389 : s += " ";
106 [ - + ]: 4680138 : s += values.at(i).write(prettyIndent, indentLevel + 1);
107 [ - + + + ]: 2340069 : if (i != (values.size() - 1))
108 : 1767601 : s += ",";
109 [ + + ]: 2340069 : if (prettyIndent)
110 : 2259389 : s += "\n";
111 : : }
112 : :
113 [ + + ]: 577720 : if (prettyIndent)
114 : 571476 : indentStr(prettyIndent, indentLevel - 1, s);
115 : 577720 : s += "}";
116 : 577720 : }
117 : :
|