Coverage Report

Created: 2026-06-17 15:31

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