Coverage Report

Created: 2026-11-13 17:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/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 <string>
9
#include <vector>
10
11
static std::string json_escape(const std::string& inS)
12
7.81M
{
13
7.81M
    std::string outS;
14
7.81M
    outS.reserve(inS.size() * 2);
15
16
1.35G
    for (unsigned int i = 0; i < inS.size(); i++) {
17
1.35G
        unsigned char ch = static_cast<unsigned char>(inS[i]);
18
1.35G
        const char *escStr = escapes[ch];
19
20
1.35G
        if (escStr)
21
26.5k
            outS += escStr;
22
1.35G
        else
23
1.35G
            outS += static_cast<char>(ch);
24
1.35G
    }
25
26
7.81M
    return outS;
27
7.81M
}
28
29
// NOLINTNEXTLINE(misc-no-recursion)
30
std::string UniValue::write(unsigned int prettyIndent,
31
                            unsigned int indentLevel) const
32
6.67M
{
33
6.67M
    std::string s;
34
6.67M
    s.reserve(1024);
35
36
6.67M
    unsigned int modIndent = indentLevel;
37
6.67M
    if (modIndent == 0)
38
280k
        modIndent = 1;
39
40
6.67M
    switch (typ) {
41
13.3k
    case VNULL:
42
13.3k
        s += "null";
43
13.3k
        break;
44
797k
    case VOBJ:
45
797k
        writeObject(prettyIndent, modIndent, s);
46
797k
        break;
47
291k
    case VARR:
48
291k
        writeArray(prettyIndent, modIndent, s);
49
291k
        break;
50
2.49M
    case VSTR:
51
2.49M
        s += "\"" + json_escape(val) + "\"";
52
2.49M
        break;
53
2.63M
    case VNUM:
54
2.63M
        s += val;
55
2.63M
        break;
56
446k
    case VBOOL:
57
446k
        s += (val == "1" ? "true" : "false");
58
446k
        break;
59
6.67M
    }
60
61
6.67M
    return s;
62
6.67M
}
63
64
static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
65
21.4k
{
66
21.4k
    s.append(prettyIndent * indentLevel, ' ');
67
21.4k
}
68
69
// NOLINTNEXTLINE(misc-no-recursion)
70
void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
71
291k
{
72
291k
    s += '[';
73
291k
    if (prettyIndent)
74
3.05k
        s += '\n';
75
76
1.36M
    for (unsigned int i = 0; i < values.size(); i++) {
77
1.07M
        if (prettyIndent)
78
8.26k
            indentStr(prettyIndent, indentLevel, s);
79
1.07M
        s += values[i].write(prettyIndent, indentLevel + 1);
80
1.07M
        if (i != (values.size() - 1)) {
81
895k
            s += ',';
82
895k
        }
83
1.07M
        if (prettyIndent)
84
8.26k
            s += '\n';
85
1.07M
    }
86
87
291k
    if (prettyIndent)
88
3.05k
        indentStr(prettyIndent, indentLevel - 1, s);
89
291k
    s += ']';
90
291k
}
91
92
// NOLINTNEXTLINE(misc-no-recursion)
93
void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
94
797k
{
95
797k
    s += '{';
96
797k
    if (prettyIndent)
97
3.27k
        s += '\n';
98
99
6.12M
    for (unsigned int i = 0; i < keys.size(); i++) {
100
5.32M
        if (prettyIndent)
101
6.81k
            indentStr(prettyIndent, indentLevel, s);
102
5.32M
        s += "\"" + json_escape(keys[i]) + "\":";
103
5.32M
        if (prettyIndent)
104
6.81k
            s += ' ';
105
5.32M
        s += values.at(i).write(prettyIndent, indentLevel + 1);
106
5.32M
        if (i != (values.size() - 1))
107
4.53M
            s += ',';
108
5.32M
        if (prettyIndent)
109
6.81k
            s += '\n';
110
5.32M
    }
111
112
797k
    if (prettyIndent)
113
3.27k
        indentStr(prettyIndent, indentLevel - 1, s);
114
797k
    s += '}';
115
797k
}
116