Coverage Report

Created: 2025-03-18 19:34

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