Merge #15139: util: Remove [U](BEGIN|END) macros

332b3dd7c1 util: Make ToLower and ToUpper take a char (Wladimir J. van der Laan)
edb5bb3500 util: remove unused [U](BEGIN|END) macros (Wladimir J. van der Laan)
7fa238c701 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
This commit is contained in:
MarcoFalke 2019-01-10 12:09:20 -05:00 committed by Munkybooty
parent 60d7fd708c
commit b39ec37988
5 changed files with 9 additions and 13 deletions

View File

@ -83,7 +83,7 @@ uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::ve
else
right = left;
// combine subhashes
return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
return Hash(left.begin(), left.end(), right.begin(), right.end());
}
}
@ -139,7 +139,7 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
right = left;
}
// and combine them before returning
return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
return Hash(left.begin(), left.end(), right.begin(), right.end());
}
}

View File

@ -13,9 +13,9 @@ static uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vecto
uint256 hash = leaf;
for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {
if (nIndex & 1) {
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
hash = Hash(it->begin(), it->end(), hash.begin(), hash.end());
} else {
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
hash = Hash(hash.begin(), hash.end(), it->begin(), it->end());
}
nIndex >>= 1;
}

View File

@ -1297,7 +1297,7 @@ BOOST_AUTO_TEST_CASE(test_ToLower)
BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
BOOST_CHECK_EQUAL(ToLower('['), '[');
BOOST_CHECK_EQUAL(ToLower(0), 0);
BOOST_CHECK_EQUAL(ToLower(255), 255);
BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
std::string testVector;
Downcase(testVector);
@ -1319,7 +1319,7 @@ BOOST_AUTO_TEST_CASE(test_ToUpper)
BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
BOOST_CHECK_EQUAL(ToUpper('{'), '{');
BOOST_CHECK_EQUAL(ToUpper(0), 0);
BOOST_CHECK_EQUAL(ToUpper(255), 255);
BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
}
BOOST_AUTO_TEST_CASE(test_Capitalize)

View File

@ -620,7 +620,7 @@ std::string HexStr(const Span<const uint8_t> s)
void Downcase(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){return ToLower(c);});
std::transform(str.begin(), str.end(), str.begin(), [](char c){return ToLower(c);});
}
std::string Capitalize(std::string str)

View File

@ -16,10 +16,6 @@
#include <string>
#include <vector>
#define BEGIN(a) ((char*)&(a))
#define END(a) ((char*)&((&(a))[1]))
#define UBEGIN(a) ((unsigned char*)&(a))
#define UEND(a) ((unsigned char*)&((&(a))[1]))
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
/** Used by SanitizeString() */
@ -196,7 +192,7 @@ bool ConvertBits(const O& outfn, I it, I end) {
* @return the lowercase equivalent of c; or the argument
* if no conversion is possible.
*/
constexpr unsigned char ToLower(unsigned char c)
constexpr char ToLower(char c)
{
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
}
@ -217,7 +213,7 @@ void Downcase(std::string& str);
* @return the uppercase equivalent of c; or the argument
* if no conversion is possible.
*/
constexpr unsigned char ToUpper(unsigned char c)
constexpr char ToUpper(char c)
{
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
}