Coverage Report

Created: 2026-09-06 13:43

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.12M
{
13
7.12M
    std::string outS;
14
7.12M
    outS.reserve(inS.size() * 2);
15
16
1.29G
    for (unsigned int i = 0; i < inS.size(); i++) {
17
1.29G
        unsigned char ch = static_cast<unsigned char>(inS[i]);
18
1.29G
        const char *escStr = escapes[ch];
19
20
1.29G
        if (escStr)
21
26.1k
            outS += escStr;
22
1.29G
        else
23
1.29G
            outS += static_cast<char>(ch);
24
1.29G
    }
25
26
7.12M
    return outS;
27
7.12M
}
28
29
// NOLINTNEXTLINE(misc-no-recursion)
30
std::string UniValue::write(unsigned int prettyIndent,
31
                            unsigned int indentLevel) const
32
6.03M
{
33
6.03M
    std::string s;
34
6.03M
    s.reserve(1024);
35
36
6.03M
    unsigned int modIndent = indentLevel;
37
6.03M
    if (modIndent == 0)
38
246k
        modIndent = 1;
39
40
6.03M
    switch (typ) {
41
13.1k
    case VNULL:
42
13.1k
        s += "null";
43
13.1k
        break;
44
728k
    case VOBJ:
45
728k
        writeObject(prettyIndent, modIndent, s);
46
728k
        break;
47
259k
    case VARR:
48
259k
        writeArray(prettyIndent, modIndent, s);
49
259k
        break;
50
2.44M
    case VSTR:
51
2.44M
        s += "\"" + json_escape(val) + "\"";
52
2.44M
        break;
53
2.19M
    case VNUM:
54
2.19M
        s += val;
55
2.19M
        break;
56
393k
    case VBOOL:
57
393k
        s += (val == "1" ? "true" : "false");
58
393k
        break;
59
6.03M
    }
60
61
6.03M
    return s;
62
6.03M
}
63
64
static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
65
20.7k
{
66
20.7k
    s.append(prettyIndent * indentLevel, ' ');
67
20.7k
}
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.93k
        s += "\n";
75
76
1.36M
    for (unsigned int i = 0; i < values.size(); i++) {
77
1.10M
        if (prettyIndent)
78
8.00k
            indentStr(prettyIndent, indentLevel, s);
79
1.10M
        s += values[i].write(prettyIndent, indentLevel + 1);
80
1.10M
        if (i != (values.size() - 1)) {
81
951k
            s += ",";
82
951k
        }
83
1.10M
        if (prettyIndent)
84
8.00k
            s += "\n";
85
1.10M
    }
86
87
259k
    if (prettyIndent)
88
2.93k
        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
728k
{
95
728k
    s += "{";
96
728k
    if (prettyIndent)
97
3.16k
        s += "\n";
98
99
5.41M
    for (unsigned int i = 0; i < keys.size(); i++) {
100
4.68M
        if (prettyIndent)
101
6.67k
            indentStr(prettyIndent, indentLevel, s);
102
4.68M
        s += "\"" + json_escape(keys[i]) + "\":";
103
4.68M
        if (prettyIndent)
104
6.67k
            s += " ";
105
4.68M
        s += values.at(i).write(prettyIndent, indentLevel + 1);
106
4.68M
        if (i != (values.size() - 1))
107
3.95M
            s += ",";
108
4.68M
        if (prettyIndent)
109
6.67k
            s += "\n";
110
4.68M
    }
111
112
728k
    if (prettyIndent)
113
3.16k
        indentStr(prettyIndent, indentLevel - 1, s);
114
728k
    s += "}";
115
728k
}
116