/bitcoin/src/univalue/lib/univalue_write.cpp
Line | Count | Source |
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 | | static std::string json_escape(const std::string& inS) |
13 | 10.5M | { |
14 | 10.5M | std::string outS; |
15 | 10.5M | outS.reserve(inS.size() * 2); |
16 | | |
17 | 72.4M | for (unsigned int i = 0; i < inS.size(); i++) { Branch (17:30): [True: 61.8M, False: 10.5M]
|
18 | 61.8M | unsigned char ch = static_cast<unsigned char>(inS[i]); |
19 | 61.8M | const char *escStr = escapes[ch]; |
20 | | |
21 | 61.8M | if (escStr) Branch (21:13): [True: 122k, False: 61.7M]
|
22 | 122k | outS += escStr; |
23 | 61.7M | else |
24 | 61.7M | outS += static_cast<char>(ch); |
25 | 61.8M | } |
26 | | |
27 | 10.5M | return outS; |
28 | 10.5M | } |
29 | | |
30 | | // NOLINTNEXTLINE(misc-no-recursion) |
31 | | std::string UniValue::write(unsigned int prettyIndent, |
32 | | unsigned int indentLevel) const |
33 | 10.4M | { |
34 | 10.4M | std::string s; |
35 | 10.4M | s.reserve(1024); |
36 | | |
37 | 10.4M | unsigned int modIndent = indentLevel; |
38 | 10.4M | if (modIndent == 0) Branch (38:9): [True: 2.81M, False: 7.67M]
|
39 | 2.81M | modIndent = 1; |
40 | | |
41 | 10.4M | switch (typ) { Branch (41:13): [True: 0, False: 10.4M]
|
42 | 2.26M | case VNULL: Branch (42:5): [True: 2.26M, False: 8.22M]
|
43 | 2.26M | s += "null"; |
44 | 2.26M | break; |
45 | 2.53M | case VOBJ: Branch (45:5): [True: 2.53M, False: 7.95M]
|
46 | 2.53M | writeObject(prettyIndent, modIndent, s); |
47 | 2.53M | break; |
48 | 33.2k | case VARR: Branch (48:5): [True: 33.2k, False: 10.4M]
|
49 | 33.2k | writeArray(prettyIndent, modIndent, s); |
50 | 33.2k | break; |
51 | 2.95M | case VSTR: Branch (51:5): [True: 2.95M, False: 7.53M]
|
52 | 2.95M | s += "\"" + json_escape(val) + "\""; |
53 | 2.95M | break; |
54 | 2.44M | case VNUM: Branch (54:5): [True: 2.44M, False: 8.04M]
|
55 | 2.44M | s += val; |
56 | 2.44M | break; |
57 | 266k | case VBOOL: Branch (57:5): [True: 266k, False: 10.2M]
|
58 | 266k | s += (val == "1" ? "true" : "false"); Branch (58:15): [True: 255k, False: 11.0k]
|
59 | 266k | break; |
60 | 10.4M | } |
61 | | |
62 | 10.4M | return s; |
63 | 10.4M | } |
64 | | |
65 | | static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) |
66 | 66.5k | { |
67 | 66.5k | s.append(prettyIndent * indentLevel, ' '); |
68 | 66.5k | } |
69 | | |
70 | | // NOLINTNEXTLINE(misc-no-recursion) |
71 | | void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const |
72 | 33.2k | { |
73 | 33.2k | s += "["; |
74 | 33.2k | if (prettyIndent) Branch (74:9): [True: 11.0k, False: 22.1k]
|
75 | 11.0k | s += "\n"; |
76 | | |
77 | 55.4k | for (unsigned int i = 0; i < values.size(); i++) { Branch (77:30): [True: 22.1k, False: 33.2k]
|
78 | 22.1k | if (prettyIndent) Branch (78:13): [True: 0, False: 22.1k]
|
79 | 0 | indentStr(prettyIndent, indentLevel, s); |
80 | 22.1k | s += values[i].write(prettyIndent, indentLevel + 1); |
81 | 22.1k | if (i != (values.size() - 1)) { Branch (81:13): [True: 0, False: 22.1k]
|
82 | 0 | s += ","; |
83 | 0 | } |
84 | 22.1k | if (prettyIndent) Branch (84:13): [True: 0, False: 22.1k]
|
85 | 0 | s += "\n"; |
86 | 22.1k | } |
87 | | |
88 | 33.2k | if (prettyIndent) Branch (88:9): [True: 11.0k, False: 22.1k]
|
89 | 11.0k | indentStr(prettyIndent, indentLevel - 1, s); |
90 | 33.2k | s += "]"; |
91 | 33.2k | } |
92 | | |
93 | | // NOLINTNEXTLINE(misc-no-recursion) |
94 | | void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const |
95 | 2.53M | { |
96 | 2.53M | s += "{"; |
97 | 2.53M | if (prettyIndent) Branch (97:9): [True: 22.1k, False: 2.50M]
|
98 | 22.1k | s += "\n"; |
99 | | |
100 | 10.1M | for (unsigned int i = 0; i < keys.size(); i++) { Branch (100:30): [True: 7.62M, False: 2.53M]
|
101 | 7.62M | if (prettyIndent) Branch (101:13): [True: 33.2k, False: 7.59M]
|
102 | 33.2k | indentStr(prettyIndent, indentLevel, s); |
103 | 7.62M | s += "\"" + json_escape(keys[i]) + "\":"; |
104 | 7.62M | if (prettyIndent) Branch (104:13): [True: 33.2k, False: 7.59M]
|
105 | 33.2k | s += " "; |
106 | 7.62M | s += values.at(i).write(prettyIndent, indentLevel + 1); |
107 | 7.62M | if (i != (values.size() - 1)) Branch (107:13): [True: 5.09M, False: 2.53M]
|
108 | 5.09M | s += ","; |
109 | 7.62M | if (prettyIndent) Branch (109:13): [True: 33.2k, False: 7.59M]
|
110 | 33.2k | s += "\n"; |
111 | 7.62M | } |
112 | | |
113 | 2.53M | if (prettyIndent) Branch (113:9): [True: 22.1k, False: 2.50M]
|
114 | 22.1k | indentStr(prettyIndent, indentLevel - 1, s); |
115 | 2.53M | s += "}"; |
116 | 2.53M | } |
117 | | |