Coverage Report

Created: 2024-10-21 15:10

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