Skip to content

Commit fadf0a7

Browse files
author
MarcoFalke
committed
refactor: Remove Span operator==, Use std::ranges::equal
1 parent 1873e41 commit fadf0a7

20 files changed

+71
-69
lines changed

src/external_signer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
8080
// Check if signer fingerprint matches any input master key fingerprint
8181
auto matches_signer_fingerprint = [&](const PSBTInput& input) {
8282
for (const auto& entry : input.hd_keypaths) {
83-
if (parsed_m_fingerprint == MakeUCharSpan(entry.second.fingerprint)) return true;
83+
if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true;
8484
}
8585
for (const auto& entry : input.m_tap_bip32_paths) {
86-
if (parsed_m_fingerprint == MakeUCharSpan(entry.second.second.fingerprint)) return true;
86+
if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true;
8787
}
8888
return false;
8989
};

src/net.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@
4646

4747
#include <algorithm>
4848
#include <array>
49+
#include <cmath>
4950
#include <cstdint>
5051
#include <functional>
5152
#include <optional>
5253
#include <unordered_map>
5354

54-
#include <math.h>
55-
5655
/** Maximum number of block-relay-only anchor connections */
5756
static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2;
5857
static_assert (MAX_BLOCK_RELAY_ONLY_ANCHORS <= static_cast<size_t>(MAX_BLOCK_RELAY_ONLY_CONNECTIONS), "MAX_BLOCK_RELAY_ONLY_ANCHORS must not exceed MAX_BLOCK_RELAY_ONLY_CONNECTIONS.");
@@ -1152,7 +1151,7 @@ bool V2Transport::ProcessReceivedGarbageBytes() noexcept
11521151
Assume(m_recv_state == RecvState::GARB_GARBTERM);
11531152
Assume(m_recv_buffer.size() <= MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
11541153
if (m_recv_buffer.size() >= BIP324Cipher::GARBAGE_TERMINATOR_LEN) {
1155-
if (MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN) == m_cipher.GetReceiveGarbageTerminator()) {
1154+
if (std::ranges::equal(MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN), m_cipher.GetReceiveGarbageTerminator())) {
11561155
// Garbage terminator received. Store garbage to authenticate it as AAD later.
11571156
m_recv_aad = std::move(m_recv_buffer);
11581157
m_recv_aad.resize(m_recv_aad.size() - BIP324Cipher::GARBAGE_TERMINATOR_LEN);

src/netaddress.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ bool CNetAddr::SetTor(const std::string& addr)
245245
Span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
246246
Span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
247247

248-
if (input_version != torv3::VERSION) {
248+
if (!std::ranges::equal(input_version, torv3::VERSION)) {
249249
return false;
250250
}
251251

252252
uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
253253
torv3::Checksum(input_pubkey, calculated_checksum);
254254

255-
if (input_checksum != calculated_checksum) {
255+
if (!std::ranges::equal(input_checksum, calculated_checksum)) {
256256
return false;
257257
}
258258

src/script/descriptor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <util/strencodings.h>
2222
#include <util/vector.h>
2323

24+
#include <algorithm>
2425
#include <memory>
2526
#include <numeric>
2627
#include <optional>
@@ -1405,11 +1406,11 @@ std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const S
14051406
}
14061407
KeyPath path;
14071408
DeriveType type = DeriveType::NO;
1408-
if (split.back() == Span{"*"}.first(1)) {
1409+
if (std::ranges::equal(split.back(), Span{"*"}.first(1))) {
14091410
split.pop_back();
14101411
type = DeriveType::UNHARDENED;
1411-
} else if (split.back() == Span{"*'"}.first(2) || split.back() == Span{"*h"}.first(2)) {
1412-
apostrophe = split.back() == Span{"*'"}.first(2);
1412+
} else if (std::ranges::equal(split.back(), Span{"*'"}.first(2)) || std::ranges::equal(split.back(), Span{"*h"}.first(2))) {
1413+
apostrophe = std::ranges::equal(split.back(), Span{"*'"}.first(2));
14131414
split.pop_back();
14141415
type = DeriveType::HARDENED;
14151416
}

src/signet.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
#include <signet.h>
66

7-
#include <array>
8-
#include <cstdint>
9-
#include <vector>
10-
117
#include <common/system.h>
128
#include <consensus/merkle.h>
139
#include <consensus/params.h>
@@ -23,6 +19,11 @@
2319
#include <uint256.h>
2420
#include <util/strencodings.h>
2521

22+
#include <algorithm>
23+
#include <array>
24+
#include <cstdint>
25+
#include <vector>
26+
2627
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
2728

2829
static constexpr unsigned int BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY;
@@ -38,7 +39,7 @@ static bool FetchAndClearCommitmentSection(const Span<const uint8_t> header, CSc
3839
std::vector<uint8_t> pushdata;
3940
while (witness_commitment.GetOp(pc, opcode, pushdata)) {
4041
if (pushdata.size() > 0) {
41-
if (!found_header && pushdata.size() > (size_t)header.size() && Span{pushdata}.first(header.size()) == header) {
42+
if (!found_header && pushdata.size() > header.size() && std::ranges::equal(Span{pushdata}.first(header.size()), header)) {
4243
// pushdata only counts if it has the header _and_ some data
4344
result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
4445
pushdata.erase(pushdata.begin() + header.size(), pushdata.end());

src/span.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#ifndef BITCOIN_SPAN_H
66
#define BITCOIN_SPAN_H
77

8-
#include <algorithm>
98
#include <cassert>
109
#include <cstddef>
1110
#include <span>
1211
#include <type_traits>
12+
#include <utility>
1313

1414
#ifdef DEBUG
1515
#define CONSTEXPR_IF_NOT_DEBUG
@@ -213,13 +213,6 @@ class Span
213213
return Span<C>(m_data + m_size - count, count);
214214
}
215215

216-
friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
217-
friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
218-
friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
219-
friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
220-
friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); }
221-
friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); }
222-
223216
template <typename O> friend class Span;
224217
};
225218

src/test/base32_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <util/strencodings.h>
66

77
#include <boost/test/unit_test.hpp>
8+
9+
#include <algorithm>
810
#include <string>
911

1012
using namespace std::literals;
@@ -24,7 +26,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
2426
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
2527
auto dec = DecodeBase32(vstrOut[i]);
2628
BOOST_REQUIRE(dec);
27-
BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
29+
BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);
2830
}
2931

3032
// Decoding strings with embedded NUL characters should fail

src/test/base64_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <util/strencodings.h>
66

77
#include <boost/test/unit_test.hpp>
8+
9+
#include <algorithm>
810
#include <string>
911

1012
using namespace std::literals;
@@ -21,7 +23,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
2123
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
2224
auto dec = DecodeBase64(strEnc);
2325
BOOST_REQUIRE(dec);
24-
BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
26+
BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);
2527
}
2628

2729
{

src/test/bip324_tests.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <test/util/setup_common.h>
1212
#include <util/strencodings.h>
1313

14+
#include <algorithm>
1415
#include <array>
1516
#include <cstddef>
1617
#include <cstdint>
@@ -62,9 +63,9 @@ void TestBIP324PacketVector(
6263
BOOST_CHECK(cipher);
6364

6465
// Compare session variables.
65-
BOOST_CHECK(Span{out_session_id} == cipher.GetSessionID());
66-
BOOST_CHECK(Span{mid_send_garbage} == cipher.GetSendGarbageTerminator());
67-
BOOST_CHECK(Span{mid_recv_garbage} == cipher.GetReceiveGarbageTerminator());
66+
BOOST_CHECK(std::ranges::equal(out_session_id, cipher.GetSessionID()));
67+
BOOST_CHECK(std::ranges::equal(mid_send_garbage, cipher.GetSendGarbageTerminator()));
68+
BOOST_CHECK(std::ranges::equal(mid_recv_garbage, cipher.GetReceiveGarbageTerminator()));
6869

6970
// Vector of encrypted empty messages, encrypted in order to seek to the right position.
7071
std::vector<std::vector<std::byte>> dummies(in_idx);
@@ -89,7 +90,7 @@ void TestBIP324PacketVector(
8990
BOOST_CHECK(out_ciphertext == ciphertext);
9091
} else {
9192
BOOST_CHECK(ciphertext.size() >= out_ciphertext_endswith.size());
92-
BOOST_CHECK(Span{out_ciphertext_endswith} == Span{ciphertext}.last(out_ciphertext_endswith.size()));
93+
BOOST_CHECK(std::ranges::equal(out_ciphertext_endswith, Span{ciphertext}.last(out_ciphertext_endswith.size())));
9394
}
9495

9596
for (unsigned error = 0; error <= 12; ++error) {
@@ -109,9 +110,9 @@ void TestBIP324PacketVector(
109110
BOOST_CHECK(dec_cipher);
110111

111112
// Compare session variables.
112-
BOOST_CHECK((Span{out_session_id} == dec_cipher.GetSessionID()) == (error != 1));
113-
BOOST_CHECK((Span{mid_send_garbage} == dec_cipher.GetSendGarbageTerminator()) == (error != 1));
114-
BOOST_CHECK((Span{mid_recv_garbage} == dec_cipher.GetReceiveGarbageTerminator()) == (error != 1));
113+
BOOST_CHECK(std::ranges::equal(out_session_id, dec_cipher.GetSessionID()) == (error != 1));
114+
BOOST_CHECK(std::ranges::equal(mid_send_garbage, dec_cipher.GetSendGarbageTerminator()) == (error != 1));
115+
BOOST_CHECK(std::ranges::equal(mid_recv_garbage, dec_cipher.GetReceiveGarbageTerminator()) == (error != 1));
115116

116117
// Seek to the numbered packet.
117118
if (in_idx == 0 && error == 12) continue;

src/test/crypto_tests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <test/util/setup_common.h>
2222
#include <util/strencodings.h>
2323

24+
#include <algorithm>
2425
#include <vector>
2526

2627
#include <boost/test/unit_test.hpp>
@@ -833,9 +834,9 @@ BOOST_AUTO_TEST_CASE(chacha20_midblock)
833834
c20.Keystream(b2);
834835
c20.Keystream(b3);
835836

836-
BOOST_CHECK(Span{block}.first(5) == Span{b1});
837-
BOOST_CHECK(Span{block}.subspan(5, 7) == Span{b2});
838-
BOOST_CHECK(Span{block}.last(52) == Span{b3});
837+
BOOST_CHECK(std::ranges::equal(Span{block}.first(5), b1));
838+
BOOST_CHECK(std::ranges::equal(Span{block}.subspan(5, 7), b2));
839+
BOOST_CHECK(std::ranges::equal(Span{block}.last(52), b3));
839840
}
840841

841842
BOOST_AUTO_TEST_CASE(poly1305_testvector)

0 commit comments

Comments
 (0)