Merge #12881: Minor optimizations to bech32::Decode(); add tests.

60f61f9 Tighten up bech32::Decode(); add tests. (murrayn)

Pull request description:

  Just a few minor optimizations to bech32::Decode():

  1) optimize the order and logic of the conditionals
  2) get rid of subsequent '(c < 33 || c > 126)' check which is redundant (already performed above)
  3) add a couple more bech32 tests (mixed-case)

Tree-SHA512: e41af834c8f6b7d34c22c28b724df42c60f72e00df616e70a12efbc4271d15d80627fe1bc36845caf29f615c238499a566298a863cbe119fef457287231053c8
This commit is contained in:
Wladimir J. van der Laan 2018-05-15 11:30:41 +02:00
commit 1d4662f5dc
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
2 changed files with 6 additions and 3 deletions

View file

@ -160,9 +160,9 @@ std::pair<std::string, data> Decode(const std::string& str) {
bool lower = false, upper = false;
for (size_t i = 0; i < str.size(); ++i) {
unsigned char c = str[i];
if (c < 33 || c > 126) return {};
if (c >= 'a' && c <= 'z') lower = true;
if (c >= 'A' && c <= 'Z') upper = true;
else if (c >= 'A' && c <= 'Z') upper = true;
else if (c < 33 || c > 126) return {};
}
if (lower && upper) return {};
size_t pos = str.rfind('1');
@ -172,7 +172,8 @@ std::pair<std::string, data> Decode(const std::string& str) {
data values(str.size() - 1 - pos);
for (size_t i = 0; i < str.size() - 1 - pos; ++i) {
unsigned char c = str[i + pos + 1];
int8_t rev = (c < 33 || c > 126) ? -1 : CHARSET_REV[c];
int8_t rev = CHARSET_REV[c];
if (rev == -1) {
return {};
}

View file

@ -57,6 +57,8 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
"A1G7SGD8",
"10a06t8",
"1qzzfhee",
"a12UEL5L",
"A12uEL5L",
};
for (const std::string& str : CASES) {
auto ret = bech32::Decode(str);