Coverage Report

Created: 2026-09-21 19:49

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