• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 14507858421

17 Apr 2025 04:11AM UTC coverage: 82.822% (+0.007%) from 82.815%
14507858421

push

github

web-flow
Merge pull request #1640 from evoskuil/master

Fix inverted NOEXCEPT define (regression), style, comments, whitespace.

7 of 8 new or added lines in 1 file covered. (87.5%)

15 existing lines in 6 files now uncovered.

10183 of 12295 relevant lines covered (82.82%)

3821900.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.01
/src/stream/binary.cpp
1
/**
2
 * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/system/stream/binary.hpp>
20

21
#include <iostream>
22
#include <string>
23
#include <utility>
24
#include <bitcoin/system/data/data_chunk.hpp>
25
#include <bitcoin/system/data/data_slice.hpp>
26
#include <bitcoin/system/math/math.hpp>
27
#include <bitcoin/system/stream/stream.hpp>
28

29
namespace libbitcoin {
30
namespace system {
31

32
constexpr uint8_t pad = 0x00;
33

34
constexpr bool is_binary(char character) NOEXCEPT
223✔
35
{
36
    return character == '0' || character == '1';
223✔
37
}
38

39
bool binary::is_base2(const std::string& text) NOEXCEPT
75✔
40
{
41
    return std::all_of(text.begin(), text.end(), is_binary);
75✔
42
}
43

44
// constructors
45
// ----------------------------------------------------------------------------
46

47
binary::binary() NOEXCEPT
26✔
48
  : bits_(0), bytes_()
26✔
49
{
50
}
26✔
51

52
binary::binary(const std::string& bits) NOEXCEPT
61✔
53
  : binary(from_string(bits))
61✔
54
{
55
}
61✔
56

57
binary::binary(size_t bits, const data_slice& data) NOEXCEPT
27✔
58
  : binary(from_data(bits, data.to_chunk()))
27✔
59
{
60
}
27✔
61

62
// private
63
binary::binary(data_chunk&& bytes, size_t bits) NOEXCEPT
87✔
64
  : bits_(bits), bytes_(bytes)
×
65
{
UNCOV
66
}
×
67

68
// factories
69
// ----------------------------------------------------------------------------
70

71
binary binary::from_data(size_t bits, data_chunk&& data) NOEXCEPT
27✔
72
{
73
    data.resize(ceilinged_divide(bits, byte_bits), pad);
27✔
74

75
    // absolute cannot overflow as magnitude is limited to sub1(byte_bits).
76
    if (!data.empty())
27✔
77
        mask_right_into(data.back(),
16✔
78
            absolute(ceilinged_modulo(bits, byte_bits)));
79

80
    return { std::move(data), bits };
27✔
81
}
82

83
binary binary::from_string(const std::string bits) NOEXCEPT
61✔
84
{
85
    if (!binary::is_base2(bits))
61✔
86
        return {};
1✔
87

88
    const auto length = bits.length();
60✔
89
    data_chunk data(ceilinged_divide(length, byte_bits), pad);
60✔
90
    write::bits::copy writer(data);
60✔
91

92
    for (const auto bit: bits)
243✔
93
        writer.write_bit(bit == '1');
183✔
94

95
    writer.flush();
60✔
96
    return { std::move(data), length };
60✔
97
}
60✔
98

99
// methods
100
// ----------------------------------------------------------------------------
101

102
std::string binary::encoded() const NOEXCEPT
90✔
103
{
104
    std::string text(to_bits(bytes_.size()), pad);
90✔
105
    write::bytes::copy writer(text);
90✔
106
    read::bits::copy reader(bytes_);
90✔
107

108
    while (!reader.is_exhausted())
962✔
109
        writer.write_byte(reader.read_bit() ? '1' : '0');
1,486✔
110

111
    // trim up to 7 characters of padding.
112
    text.resize(bits_);
90✔
113
    text.shrink_to_fit();
90✔
114
    return text;
180✔
115
}
90✔
116

117
const data_chunk& binary::data() const NOEXCEPT
18✔
118
{
119
    return bytes_;
18✔
120
}
121

122
size_t binary::bytes() const NOEXCEPT
7✔
123
{
124
    return bytes_.size();
7✔
125
}
126

127
size_t binary::bits() const NOEXCEPT
41✔
128
{
129
    return bits_;
41✔
130
}
131

132
// operators
133
// ----------------------------------------------------------------------------
134

135
binary::operator const data_chunk&() const NOEXCEPT
6✔
136
{
137
    return bytes_;
6✔
138
}
139

140
bool binary::operator[](size_t index) const NOEXCEPT
23✔
141
{
142
    const auto byte = index / byte_bits;
23✔
143
    return (byte < bits_) && get_left(bytes_[byte], index % byte_bits);
23✔
144
}
145

146
bool binary::operator<(const binary& other) const NOEXCEPT
16✔
147
{
148
    return encoded() < other.encoded();
16✔
149
}
150

151
bool operator==(const binary& left, const binary& right) NOEXCEPT
20✔
152
{
153
    return left.encoded() == right.encoded();
20✔
154
}
155

156
bool operator!=(const binary& left, const binary& right) NOEXCEPT
6✔
157
{
158
    return !(left == right);
6✔
159
}
160

161
std::istream& operator>>(std::istream& in, binary& to) NOEXCEPT
1✔
162
{
163
    std::string text;
1✔
164
    in >> text;
1✔
165
    to = binary(text);
1✔
166
    return in;
1✔
167
}
168

169
std::ostream& operator<<(std::ostream& out, const binary& of) NOEXCEPT
1✔
170
{
171
    out << of.encoded();
1✔
172
    return out;
1✔
173
}
174

175
} // namespace system
176
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc