Coverage Report

Created: 2026-07-14 18:13

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