Skip to content

Commit 1d4662f

Browse files
committed
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
2 parents 3b84ebb + 60f61f9 commit 1d4662f

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/bech32.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ std::pair<std::string, data> Decode(const std::string& str) {
160160
bool lower = false, upper = false;
161161
for (size_t i = 0; i < str.size(); ++i) {
162162
unsigned char c = str[i];
163-
if (c < 33 || c > 126) return {};
164163
if (c >= 'a' && c <= 'z') lower = true;
165-
if (c >= 'A' && c <= 'Z') upper = true;
164+
else if (c >= 'A' && c <= 'Z') upper = true;
165+
else if (c < 33 || c > 126) return {};
166166
}
167167
if (lower && upper) return {};
168168
size_t pos = str.rfind('1');
@@ -172,7 +172,8 @@ std::pair<std::string, data> Decode(const std::string& str) {
172172
data values(str.size() - 1 - pos);
173173
for (size_t i = 0; i < str.size() - 1 - pos; ++i) {
174174
unsigned char c = str[i + pos + 1];
175-
int8_t rev = (c < 33 || c > 126) ? -1 : CHARSET_REV[c];
175+
int8_t rev = CHARSET_REV[c];
176+
176177
if (rev == -1) {
177178
return {};
178179
}

src/test/bech32_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
5757
"A1G7SGD8",
5858
"10a06t8",
5959
"1qzzfhee",
60+
"a12UEL5L",
61+
"A12uEL5L",
6062
};
6163
for (const std::string& str : CASES) {
6264
auto ret = bech32::Decode(str);

0 commit comments

Comments
 (0)