Coverage Report

Created: 2026-07-08 14:14

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