Skip to content

Commit 8c1dbc5

Browse files
committed
Refactor: Removed begin/end_ptr functions.
1 parent 8601784 commit 8c1dbc5

File tree

11 files changed

+32
-61
lines changed

11 files changed

+32
-61
lines changed

src/bench/crypto_hash.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ static void RIPEMD160(benchmark::State& state)
2222
uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
2323
std::vector<uint8_t> in(BUFFER_SIZE,0);
2424
while (state.KeepRunning())
25-
CRIPEMD160().Write(begin_ptr(in), in.size()).Finalize(hash);
25+
CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
2626
}
2727

2828
static void SHA1(benchmark::State& state)
2929
{
3030
uint8_t hash[CSHA1::OUTPUT_SIZE];
3131
std::vector<uint8_t> in(BUFFER_SIZE,0);
3232
while (state.KeepRunning())
33-
CSHA1().Write(begin_ptr(in), in.size()).Finalize(hash);
33+
CSHA1().Write(in.data(), in.size()).Finalize(hash);
3434
}
3535

3636
static void SHA256(benchmark::State& state)
3737
{
3838
uint8_t hash[CSHA256::OUTPUT_SIZE];
3939
std::vector<uint8_t> in(BUFFER_SIZE,0);
4040
while (state.KeepRunning())
41-
CSHA256().Write(begin_ptr(in), in.size()).Finalize(hash);
41+
CSHA256().Write(in.data(), in.size()).Finalize(hash);
4242
}
4343

4444
static void SHA256_32b(benchmark::State& state)
4545
{
4646
std::vector<uint8_t> in(32,0);
4747
while (state.KeepRunning()) {
4848
for (int i = 0; i < 1000000; i++) {
49-
CSHA256().Write(begin_ptr(in), in.size()).Finalize(&in[0]);
49+
CSHA256().Write(in.data(), in.size()).Finalize(&in[0]);
5050
}
5151
}
5252
}
@@ -56,7 +56,7 @@ static void SHA512(benchmark::State& state)
5656
uint8_t hash[CSHA512::OUTPUT_SIZE];
5757
std::vector<uint8_t> in(BUFFER_SIZE,0);
5858
while (state.KeepRunning())
59-
CSHA512().Write(begin_ptr(in), in.size()).Finalize(hash);
59+
CSHA512().Write(in.data(), in.size()).Finalize(hash);
6060
}
6161

6262
static void SipHash_32b(benchmark::State& state)

src/bench/verify_script.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void VerifyScriptBench(benchmark::State& state)
9191
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
9292
stream << txSpend;
9393
int csuccess = bitcoinconsensus_verify_script_with_amount(
94-
begin_ptr(txCredit.vout[0].scriptPubKey),
94+
txCredit.vout[0].scriptPubKey.data(),
9595
txCredit.vout[0].scriptPubKey.size(),
9696
txCredit.vout[0].nValue,
9797
(const unsigned char*)&stream[0], stream.size(), 0, flags, nullptr);

src/netbase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
292292
vSocks5Init.push_back(0x01); // # METHODS
293293
vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED
294294
}
295-
ssize_t ret = send(hSocket, (const char*)begin_ptr(vSocks5Init), vSocks5Init.size(), MSG_NOSIGNAL);
295+
ssize_t ret = send(hSocket, (const char*)vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL);
296296
if (ret != (ssize_t)vSocks5Init.size()) {
297297
CloseSocket(hSocket);
298298
return error("Error sending to proxy");
@@ -317,7 +317,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
317317
vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
318318
vAuth.push_back(auth->password.size());
319319
vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
320-
ret = send(hSocket, (const char*)begin_ptr(vAuth), vAuth.size(), MSG_NOSIGNAL);
320+
ret = send(hSocket, (const char*)vAuth.data(), vAuth.size(), MSG_NOSIGNAL);
321321
if (ret != (ssize_t)vAuth.size()) {
322322
CloseSocket(hSocket);
323323
return error("Error sending authentication to proxy");
@@ -347,7 +347,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
347347
vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
348348
vSocks5.push_back((port >> 8) & 0xFF);
349349
vSocks5.push_back((port >> 0) & 0xFF);
350-
ret = send(hSocket, (const char*)begin_ptr(vSocks5), vSocks5.size(), MSG_NOSIGNAL);
350+
ret = send(hSocket, (const char*)vSocks5.data(), vSocks5.size(), MSG_NOSIGNAL);
351351
if (ret != (ssize_t)vSocks5.size()) {
352352
CloseSocket(hSocket);
353353
return error("Error sending to proxy");

src/qt/guiutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static std::string DummyAddress(const CChainParams &params)
117117
std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
118118
sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata));
119119
for(int i=0; i<256; ++i) { // Try every trailing byte
120-
std::string s = EncodeBase58(begin_ptr(sourcedata), end_ptr(sourcedata));
120+
std::string s = EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size());
121121
if (!CBitcoinAddress(s).IsValid())
122122
return s;
123123
sourcedata[sourcedata.size()-1] += 1;

src/random.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "compat.h" // for Windows API
1212
#include <wincrypt.h>
1313
#endif
14-
#include "serialize.h" // for begin_ptr(vec)
1514
#include "util.h" // for LogPrint()
1615
#include "utilstrencodings.h" // for GetTime()
1716

@@ -72,15 +71,15 @@ static void RandAddSeedPerfmon()
7271
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
7372
while (true) {
7473
nSize = vData.size();
75-
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, begin_ptr(vData), &nSize);
74+
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, vData.data(), &nSize);
7675
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
7776
break;
7877
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
7978
}
8079
RegCloseKey(HKEY_PERFORMANCE_DATA);
8180
if (ret == ERROR_SUCCESS) {
82-
RAND_add(begin_ptr(vData), nSize, nSize / 100.0);
83-
memory_cleanse(begin_ptr(vData), nSize);
81+
RAND_add(vData.data(), nSize, nSize / 100.0);
82+
memory_cleanse(vData.data(), nSize);
8483
LogPrint("rand", "%s: %lu bytes\n", __func__, nSize);
8584
} else {
8685
static bool warned = false; // Warn only once

src/script/interpreter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -856,15 +856,15 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
856856
valtype& vch = stacktop(-1);
857857
valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
858858
if (opcode == OP_RIPEMD160)
859-
CRIPEMD160().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash));
859+
CRIPEMD160().Write(vch.data(), vch.size()).Finalize(vchHash.data());
860860
else if (opcode == OP_SHA1)
861-
CSHA1().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash));
861+
CSHA1().Write(vch.data(), vch.size()).Finalize(vchHash.data());
862862
else if (opcode == OP_SHA256)
863-
CSHA256().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash));
863+
CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
864864
else if (opcode == OP_HASH160)
865-
CHash160().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash));
865+
CHash160().Write(vch.data(), vch.size()).Finalize(vchHash.data());
866866
else if (opcode == OP_HASH256)
867-
CHash256().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash));
867+
CHash256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
868868
popstack(stack);
869869
stack.push_back(vchHash);
870870
}

src/serialize.h

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,6 @@ inline T* NCONST_PTR(const T* val)
5959
return const_cast<T*>(val);
6060
}
6161

62-
/**
63-
* Important: Do not use the following functions in new code, but use v.data()
64-
* and v.data() + v.size() respectively directly. They were once introduced to
65-
* have a compatible, safe way to get the begin and end pointer of a vector.
66-
* However with C++11 the language has built-in functionality for this and it's
67-
* more readable to just use that.
68-
*/
69-
template <typename V>
70-
inline typename V::value_type* begin_ptr(V& v)
71-
{
72-
return v.data();
73-
}
74-
template <typename V>
75-
inline const typename V::value_type* begin_ptr(const V& v)
76-
{
77-
return v.data();
78-
}
79-
template <typename V>
80-
inline typename V::value_type* end_ptr(V& v)
81-
{
82-
return v.data() + v.size();
83-
}
84-
template <typename V>
85-
inline const typename V::value_type* end_ptr(const V& v)
86-
{
87-
return v.data() + v.size();
88-
}
89-
9062
/*
9163
* Lowest-level serialization and conversion.
9264
* @note Sizes of these types are verified in the tests
@@ -390,14 +362,14 @@ class CFlatData
390362
template <class T, class TAl>
391363
explicit CFlatData(std::vector<T,TAl> &v)
392364
{
393-
pbegin = (char*)begin_ptr(v);
394-
pend = (char*)end_ptr(v);
365+
pbegin = (char*)v.data();
366+
pend = (char*)(v.data() + v.size());
395367
}
396368
template <unsigned int N, typename T, typename S, typename D>
397369
explicit CFlatData(prevector<N, T, S, D> &v)
398370
{
399-
pbegin = (char*)begin_ptr(v);
400-
pend = (char*)end_ptr(v);
371+
pbegin = (char*)v.data();
372+
pend = (char*)(v.data() + v.size());
401373
}
402374
char* begin() { return pbegin; }
403375
const char* begin() const { return pbegin; }

src/test/base58_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
3939
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
4040
std::string base58string = test[1].get_str();
4141
BOOST_CHECK_MESSAGE(
42-
EncodeBase58(begin_ptr(sourcedata), end_ptr(sourcedata)) == base58string,
42+
EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
4343
strTest);
4444
}
4545
}

src/test/script_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
176176
int libconsensus_flags = flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL;
177177
if (libconsensus_flags == flags) {
178178
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
179-
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
179+
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
180180
} else {
181-
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
182-
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(begin_ptr(scriptPubKey), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect,message);
181+
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
182+
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect,message);
183183
}
184184
}
185185
#endif

src/torcontrol.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,10 @@ static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::v
501501
{
502502
CHMAC_SHA256 computeHash((const uint8_t*)key.data(), key.size());
503503
std::vector<uint8_t> computedHash(CHMAC_SHA256::OUTPUT_SIZE, 0);
504-
computeHash.Write(begin_ptr(cookie), cookie.size());
505-
computeHash.Write(begin_ptr(clientNonce), clientNonce.size());
506-
computeHash.Write(begin_ptr(serverNonce), serverNonce.size());
507-
computeHash.Finalize(begin_ptr(computedHash));
504+
computeHash.Write(cookie.data(), cookie.size());
505+
computeHash.Write(clientNonce.data(), clientNonce.size());
506+
computeHash.Write(serverNonce.data(), serverNonce.size());
507+
computeHash.Finalize(computedHash.data());
508508
return computedHash;
509509
}
510510

0 commit comments

Comments
 (0)