Skip to content

Commit f0fce06

Browse files
committed
util: make EncodeBase58 consume Spans
1 parent bd00d3b commit f0fce06

File tree

5 files changed

+13
-23
lines changed

5 files changed

+13
-23
lines changed

src/base58.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,21 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_
8484
return true;
8585
}
8686

87-
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
87+
std::string EncodeBase58(Span<const unsigned char> input)
8888
{
8989
// Skip & count leading zeroes.
9090
int zeroes = 0;
9191
int length = 0;
92-
while (pbegin != pend && *pbegin == 0) {
93-
pbegin++;
92+
while (input.size() > 0 && input[0] == 0) {
93+
input = input.subspan(1);
9494
zeroes++;
9595
}
9696
// Allocate enough space in big-endian base58 representation.
97-
int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.
97+
int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up.
9898
std::vector<unsigned char> b58(size);
9999
// Process the bytes.
100-
while (pbegin != pend) {
101-
int carry = *pbegin;
100+
while (input.size() > 0) {
101+
int carry = input[0];
102102
int i = 0;
103103
// Apply "b58 = b58 * 256 + ch".
104104
for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
@@ -109,7 +109,7 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
109109

110110
assert(carry == 0);
111111
length = i;
112-
pbegin++;
112+
input = input.subspan(1);
113113
}
114114
// Skip leading zeroes in base58 result.
115115
std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
@@ -124,11 +124,6 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
124124
return str;
125125
}
126126

127-
std::string EncodeBase58(const std::vector<unsigned char>& vch)
128-
{
129-
return EncodeBase58(vch.data(), vch.data() + vch.size());
130-
}
131-
132127
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len)
133128
{
134129
if (!ValidAsCString(str)) {

src/base58.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515
#define BITCOIN_BASE58_H
1616

1717
#include <attributes.h>
18+
#include <span.h>
1819

1920
#include <string>
2021
#include <vector>
2122

2223
/**
23-
* Encode a byte sequence as a base58-encoded string.
24-
* pbegin and pend cannot be nullptr, unless both are.
24+
* Encode a byte span as a base58-encoded string
2525
*/
26-
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
27-
28-
/**
29-
* Encode a byte vector as a base58-encoded string
30-
*/
31-
std::string EncodeBase58(const std::vector<unsigned char>& vch);
26+
std::string EncodeBase58(Span<const unsigned char> input);
3227

3328
/**
3429
* Decode a base58-encoded string (psz) into a byte vector (vchRet).

src/bench/base58.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void Base58Encode(benchmark::Bench& bench)
2020
}
2121
};
2222
bench.batch(buff.size()).unit("byte").run([&] {
23-
EncodeBase58(buff.data(), buff.data() + buff.size());
23+
EncodeBase58(buff);
2424
});
2525
}
2626

src/qt/guiutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static std::string DummyAddress(const CChainParams &params)
9494
std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
9595
sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata));
9696
for(int i=0; i<256; ++i) { // Try every trailing byte
97-
std::string s = EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size());
97+
std::string s = EncodeBase58(sourcedata);
9898
if (!IsValidDestinationString(s)) {
9999
return s;
100100
}

src/test/base58_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
3333
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
3434
std::string base58string = test[1].get_str();
3535
BOOST_CHECK_MESSAGE(
36-
EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
36+
EncodeBase58(sourcedata) == base58string,
3737
strTest);
3838
}
3939
}

0 commit comments

Comments
 (0)