Skip to content

Commit 413e8b5

Browse files
committed
Merge #149: Remove unnecessary call to to_ascii_lowercase
b6233c8 Remove unnecessary call to to_ascii_lowercase (Tobin C. Harding) Pull request description: We already iterate over lowercase characters when encoding a bech32 string, no need to call `to_ascii_lowercase`. Add a unit test as documentation of this behavior as well as to verify the change is correct. ACKs for top commit: apoelstra: ACK b6233c8 Tree-SHA512: f2a7ef807645b00ae4d87cdeb138624b52b322fc51d9dd0c86076e7ce2c4408aee1333dda1fdf78ee4b59c57334b31f39c4ab0c2e1f2863dd4e2d80aa681c30e
2 parents 13b3f47 + b6233c8 commit 413e8b5

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/segwit.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ pub fn encode_lower_to_writer_unchecked<W: std::io::Write>(
221221
match witness_version {
222222
VERSION_0 => {
223223
for c in iter.with_checksum::<Bech32>(hrp).with_witness_version(VERSION_0).chars() {
224-
w.write_all(&[c.to_ascii_lowercase() as u8])?;
224+
w.write_all(&[c as u8])?;
225225
}
226226
}
227227
version => {
228228
for c in iter.with_checksum::<Bech32m>(hrp).with_witness_version(version).chars() {
229-
w.write_all(&[c.to_ascii_lowercase() as u8])?;
229+
w.write_all(&[c as u8])?;
230230
}
231231
}
232232
}
@@ -416,4 +416,18 @@ mod tests {
416416
let want = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
417417
assert_eq!(address, want);
418418
}
419+
420+
#[test]
421+
#[cfg(feature = "std")]
422+
fn encode_lower_to_writer_including_lowecaseing_hrp() {
423+
let program = witness_program();
424+
let mut buf = Vec::new();
425+
let hrp = Hrp::parse_unchecked("BC");
426+
encode_lower_to_writer_unchecked(&mut buf, &hrp, VERSION_0, &program)
427+
.expect("failed to encode");
428+
429+
let address = std::str::from_utf8(&buf).expect("ascii is valid utf8");
430+
let want = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
431+
assert_eq!(address, want);
432+
}
419433
}

0 commit comments

Comments
 (0)