From a684076f16822f8b488e7ffd8bd9600c9c166caa Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 16 Sep 2024 21:20:30 +0900 Subject: [PATCH] hex_util: simplify common_hex_len() a bit to compare input bytes once I think it's slightly easier to follow if we calculate a diff of input bits first. I don't know which one is faster, but I assume compiler can optimize to similar instructions. --- lib/src/hex_util.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/src/hex_util.rs b/lib/src/hex_util.rs index 3ed514d23..48517b38f 100644 --- a/lib/src/hex_util.rs +++ b/lib/src/hex_util.rs @@ -56,16 +56,10 @@ pub fn to_reverse_hex(forward_hex: &str) -> Option { pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize { std::iter::zip(bytes_a, bytes_b) .enumerate() - .find_map(|(i, (a, b))| { - if a != b { - if a >> 4 != b >> 4 { - Some(i * 2) - } else { - Some(i * 2 + 1) - } - } else { - None - } + .find_map(|(i, (a, b))| match a ^ b { + 0 => None, + d if d & 0xf0 == 0 => Some(i * 2 + 1), + _ => Some(i * 2), }) .unwrap_or_else(|| bytes_a.len().min(bytes_b.len()) * 2) }