Skip to content

Commit 487655c

Browse files
committed
Remove unnecessary call to to_ascii_lowercase
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.
1 parent 202b59f commit 487655c

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
}
@@ -463,4 +463,18 @@ mod tests {
463463
let want = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
464464
assert_eq!(address, want);
465465
}
466+
467+
#[test]
468+
#[cfg(feature = "std")]
469+
fn encode_lower_to_writer_including_lowecaseing_hrp() {
470+
let program = witness_program();
471+
let mut buf = Vec::new();
472+
let hrp = Hrp::parse_unchecked("BC");
473+
encode_lower_to_writer_unchecked(&mut buf, &hrp, VERSION_0, &program)
474+
.expect("failed to encode");
475+
476+
let address = std::str::from_utf8(&buf).expect("ascii is valid utf8");
477+
let want = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
478+
assert_eq!(address, want);
479+
}
466480
}

0 commit comments

Comments
 (0)