Coverage Report

Created: 2026-06-01 18:35

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