use the char trick to avoid allocation
This commit is contained in:
parent
104836a042
commit
85ae82febb
|
@ -388,25 +388,36 @@ impl fmt::Display for Address {
|
||||||
version: ver,
|
version: ver,
|
||||||
program: ref prog,
|
program: ref prog,
|
||||||
} => {
|
} => {
|
||||||
if fmt.alternate() {
|
let hrp = match self.network {
|
||||||
//TODO format without allocation when alternate uppercase is in bech32
|
Network::Bitcoin => "bc",
|
||||||
let lower = self.to_string();
|
Network::Testnet | Network::Signet => "tb",
|
||||||
write!(fmt, "{}", lower.to_ascii_uppercase())
|
Network::Regtest => "bcrt",
|
||||||
} else {
|
};
|
||||||
let hrp = match self.network {
|
let is_alternate = fmt.alternate();
|
||||||
Network::Bitcoin => "bc",
|
let mut opt_up_writer = OptionallyUpperWriter(fmt, is_alternate);
|
||||||
Network::Testnet | Network::Signet => "tb",
|
let mut bech32_writer = bech32::Bech32Writer::new(hrp, &mut opt_up_writer)?;
|
||||||
Network::Regtest => "bcrt",
|
bech32::WriteBase32::write_u5(&mut bech32_writer, ver)?;
|
||||||
};
|
bech32::ToBase32::write_base32(&prog, &mut bech32_writer)
|
||||||
let mut bech32_writer = bech32::Bech32Writer::new(hrp, fmt)?;
|
|
||||||
bech32::WriteBase32::write_u5(&mut bech32_writer, ver)?;
|
|
||||||
bech32::ToBase32::write_base32(&prog, &mut bech32_writer)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct OptionallyUpperWriter<W: fmt::Write>(W, bool);
|
||||||
|
|
||||||
|
impl<W: fmt::Write> fmt::Write for OptionallyUpperWriter<W> {
|
||||||
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
|
if self.1 {
|
||||||
|
for c in s.chars() {
|
||||||
|
self.0.write_char(c.to_ascii_uppercase())?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.0.write_str(s)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Extract the bech32 prefix.
|
/// Extract the bech32 prefix.
|
||||||
/// Returns the same slice when no prefix is found.
|
/// Returns the same slice when no prefix is found.
|
||||||
fn find_bech32_prefix(bech32: &str) -> &str {
|
fn find_bech32_prefix(bech32: &str) -> &str {
|
||||||
|
|
Loading…
Reference in New Issue