Coverage Report

Created: 2026-10-03 10:33

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