Skip to content

Commit be40a82

Browse files
committed
f - no allocations
1 parent 4f8f481 commit be40a82

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

lightning/src/routing/gossip.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -935,20 +935,24 @@ impl fmt::Display for NodeAlias {
935935
let control_symbol = core::char::REPLACEMENT_CHARACTER;
936936
let first_null = self.0.iter().position(|b| *b == 0).unwrap_or(self.0.len());
937937
let bytes = self.0.split_at(first_null).0;
938-
let alias = match core::str::from_utf8(bytes) {
938+
match core::str::from_utf8(bytes) {
939939
Ok(alias) => {
940-
alias.chars()
941-
.map(|c| if c.is_control() { control_symbol } else { c })
942-
.collect::<String>()
940+
for c in alias.chars() {
941+
let mut bytes = [0u8; 4];
942+
let c = if !c.is_control() { c } else { control_symbol };
943+
f.write_str(c.encode_utf8(&mut bytes))?;
944+
}
943945
},
944946
Err(_) => {
945-
bytes.iter().map(|b| *b as char)
947+
for c in bytes.iter().map(|b| *b as char) {
946948
// Display printable ASCII characters
947-
.map(|c| if c >= '\x20' && c <= '\x7e' { c } else { control_symbol })
948-
.collect::<String>()
949+
let mut bytes = [0u8; 4];
950+
let c = if c >= '\x20' && c <= '\x7e' { c } else { control_symbol };
951+
f.write_str(c.encode_utf8(&mut bytes))?;
952+
}
949953
},
950954
};
951-
f.write_str(&alias)
955+
Ok(())
952956
}
953957
}
954958

0 commit comments

Comments
 (0)