Coverage Report

Created: 2026-08-05 14:35

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