Skip to content

Commit 68dddcc

Browse files
author
MarcoFalke
committed
Merge #15139: util: Remove [U](BEGIN|END) macros
332b3dd util: Make ToLower and ToUpper take a char (Wladimir J. van der Laan) edb5bb3 util: remove unused [U](BEGIN|END) macros (Wladimir J. van der Laan) 7fa238c Replace use of BEGIN and END macros on uint256 (Wladimir J. van der Laan) Pull request description: Two cleanups in `util/strencodings.h`: - Remove `[U](BEGIN|END)` macros — The only use of these was in the Merkle tree code with `uint256` which has its own `begin` and `end` methods which are better. - Make ToLower and ToUpper take a char — Unfortunately, `std::string` elements are (bare) chars. As these are the most likely type to be passed to these functions, make them use char instead of unsigned char. This avoids some casts. Tree-SHA512: 96c8292e1b588d3d7fde95c2e98ad4e7eb75e7baab40a8e8e8209d4e8e7a1bd3b6846601d20976be34a9daabefc50cbc23f3b04200af17d0dfc857c4ec42aca7
2 parents cebe910 + 332b3dd commit 68dddcc

File tree

6 files changed

+10
-14
lines changed

6 files changed

+10
-14
lines changed

src/merkleblock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::ve
5353
else
5454
right = left;
5555
// combine subhashes
56-
return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
56+
return Hash(left.begin(), left.end(), right.begin(), right.end());
5757
}
5858
}
5959

@@ -109,7 +109,7 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
109109
right = left;
110110
}
111111
// and combine them before returning
112-
return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
112+
return Hash(left.begin(), left.end(), right.begin(), right.end());
113113
}
114114
}
115115

src/test/merkle_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ static uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vecto
1313
uint256 hash = leaf;
1414
for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {
1515
if (nIndex & 1) {
16-
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
16+
hash = Hash(it->begin(), it->end(), hash.begin(), hash.end());
1717
} else {
18-
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
18+
hash = Hash(hash.begin(), hash.end(), it->begin(), it->end());
1919
}
2020
nIndex >>= 1;
2121
}

src/test/util_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ BOOST_AUTO_TEST_CASE(test_ToLower)
12241224
BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
12251225
BOOST_CHECK_EQUAL(ToLower('['), '[');
12261226
BOOST_CHECK_EQUAL(ToLower(0), 0);
1227-
BOOST_CHECK_EQUAL(ToLower(255), 255);
1227+
BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
12281228

12291229
std::string testVector;
12301230
Downcase(testVector);
@@ -1246,7 +1246,7 @@ BOOST_AUTO_TEST_CASE(test_ToUpper)
12461246
BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
12471247
BOOST_CHECK_EQUAL(ToUpper('{'), '{');
12481248
BOOST_CHECK_EQUAL(ToUpper(0), 0);
1249-
BOOST_CHECK_EQUAL(ToUpper(255), 255);
1249+
BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
12501250
}
12511251

12521252
BOOST_AUTO_TEST_CASE(test_Capitalize)

src/uint256.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void base_blob<BITS>::SetHex(const char* psz)
3333
psz++;
3434

3535
// skip 0x
36-
if (psz[0] == '0' && ToLower((unsigned char)psz[1]) == 'x')
36+
if (psz[0] == '0' && ToLower(psz[1]) == 'x')
3737
psz += 2;
3838

3939
// hex string to uint

src/util/strencodings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypa
589589

590590
void Downcase(std::string& str)
591591
{
592-
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){return ToLower(c);});
592+
std::transform(str.begin(), str.end(), str.begin(), [](char c){return ToLower(c);});
593593
}
594594

595595
std::string Capitalize(std::string str)

src/util/strencodings.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
#include <string>
1616
#include <vector>
1717

18-
#define BEGIN(a) ((char*)&(a))
19-
#define END(a) ((char*)&((&(a))[1]))
20-
#define UBEGIN(a) ((unsigned char*)&(a))
21-
#define UEND(a) ((unsigned char*)&((&(a))[1]))
2218
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
2319

2420
/** Used by SanitizeString() */
@@ -212,7 +208,7 @@ NODISCARD bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32
212208
* @return the lowercase equivalent of c; or the argument
213209
* if no conversion is possible.
214210
*/
215-
constexpr unsigned char ToLower(unsigned char c)
211+
constexpr char ToLower(char c)
216212
{
217213
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
218214
}
@@ -233,7 +229,7 @@ void Downcase(std::string& str);
233229
* @return the uppercase equivalent of c; or the argument
234230
* if no conversion is possible.
235231
*/
236-
constexpr unsigned char ToUpper(unsigned char c)
232+
constexpr char ToUpper(char c)
237233
{
238234
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
239235
}

0 commit comments

Comments
 (0)