Coverage Report

Created: 2026-10-19 15:22

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