mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 15:21:24 +01:00
Fix u8_slice_to_hex()
For bytes <0x10, this would omit the leading zero.
This commit is contained in:
parent
76a633cb66
commit
6a44d0af2b
1 changed files with 18 additions and 2 deletions
20
src/utils.rs
20
src/utils.rs
|
|
@ -374,14 +374,14 @@ pub(crate) fn curlify<T>(req: &http::Request<T>) -> Option<String> {
|
|||
/// whitespace or commas do not appear in the output).
|
||||
pub(crate) fn u8_slice_to_hex(slice: &[u8]) -> String {
|
||||
slice.iter().fold(String::new(), |mut acc, x| {
|
||||
write!(acc, "{x:X}").expect("in-memory write should succeed");
|
||||
write!(acc, "{x:02X}").expect("in-memory write should succeed");
|
||||
acc
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::dbg_truncate_str;
|
||||
use crate::utils::{dbg_truncate_str, u8_slice_to_hex};
|
||||
|
||||
#[test]
|
||||
fn test_truncate_str() {
|
||||
|
|
@ -395,4 +395,20 @@ mod tests {
|
|||
assert_eq!(dbg_truncate_str(ok_hand, ok_hand.len() - 1), "👌🏽");
|
||||
assert_eq!(dbg_truncate_str(ok_hand, ok_hand.len()), "👌🏽");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_to_hex() {
|
||||
assert_eq!(u8_slice_to_hex(&[]), "");
|
||||
assert_eq!(u8_slice_to_hex(&[0]), "00");
|
||||
assert_eq!(u8_slice_to_hex(&[0xFF]), "FF");
|
||||
assert_eq!(u8_slice_to_hex(&[1, 2, 3, 4]), "01020304");
|
||||
assert_eq!(
|
||||
u8_slice_to_hex(&[0x42; 100]),
|
||||
"4242424242424242424242424242424242424242\
|
||||
4242424242424242424242424242424242424242\
|
||||
4242424242424242424242424242424242424242\
|
||||
4242424242424242424242424242424242424242\
|
||||
4242424242424242424242424242424242424242"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue