mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +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).
|
/// whitespace or commas do not appear in the output).
|
||||||
pub(crate) fn u8_slice_to_hex(slice: &[u8]) -> String {
|
pub(crate) fn u8_slice_to_hex(slice: &[u8]) -> String {
|
||||||
slice.iter().fold(String::new(), |mut acc, x| {
|
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
|
acc
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::utils::dbg_truncate_str;
|
use crate::utils::{dbg_truncate_str, u8_slice_to_hex};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_truncate_str() {
|
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() - 1), "👌🏽");
|
||||||
assert_eq!(dbg_truncate_str(ok_hand, ok_hand.len()), "👌🏽");
|
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