From dbaee198e61ca048123056c5c746b6a863a72f30 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 4 Jan 2024 17:31:45 +0900 Subject: [PATCH] hex_util: move common_hex_len() from backend module This function predates the hex_util module. If there were hex_util, I would add it there. --- lib/src/backend.rs | 20 -------------------- lib/src/default_index/composite.rs | 4 ++-- lib/src/hex_util.rs | 20 ++++++++++++++++++++ lib/src/id_prefix.rs | 5 +++-- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/src/backend.rs b/lib/src/backend.rs index f0b2bf8cd..a6e99c4fd 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -424,26 +424,6 @@ impl Tree { } } -/// Calculates common prefix length of two bytes. The length to be returned is -/// a number of hexadecimal digits. -pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize { - iter_half_bytes(bytes_a) - .zip(iter_half_bytes(bytes_b)) - .take_while(|(a, b)| a == b) - .count() -} - -fn iter_half_bytes(bytes: &[u8]) -> impl ExactSizeIterator + '_ { - (0..bytes.len() * 2).map(|i| { - let v = bytes[i / 2]; - if i & 1 == 0 { - v >> 4 - } else { - v & 0xf - } - }) -} - pub fn make_root_commit(root_change_id: ChangeId, empty_tree_id: TreeId) -> Commit { let timestamp = Timestamp { timestamp: MillisSinceEpoch(0), diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index ead3145e9..ff41d4ee3 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -30,7 +30,7 @@ use crate::backend::{ChangeId, CommitId, ObjectId}; use crate::index::{HexPrefix, Index, PrefixResolution}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError}; use crate::store::Store; -use crate::{backend, default_revset_engine}; +use crate::{default_revset_engine, hex_util}; pub(super) trait IndexSegment: Send + Sync { fn num_parent_commits(&self) -> u32; @@ -319,7 +319,7 @@ impl Index for CompositeIndex<'_> { fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize { let (prev_id, next_id) = self.resolve_neighbor_commit_ids(commit_id); itertools::chain(prev_id, next_id) - .map(|id| backend::common_hex_len(commit_id.as_bytes(), id.as_bytes()) + 1) + .map(|id| hex_util::common_hex_len(commit_id.as_bytes(), id.as_bytes()) + 1) .max() .unwrap_or(0) } diff --git a/lib/src/hex_util.rs b/lib/src/hex_util.rs index 1801b09a9..5a1611e4a 100644 --- a/lib/src/hex_util.rs +++ b/lib/src/hex_util.rs @@ -51,6 +51,26 @@ pub fn to_reverse_hex(forward_hex: &str) -> Option { .collect() } +/// Calculates common prefix length of two bytes. The length to be returned is +/// a number of hexadecimal digits. +pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize { + iter_half_bytes(bytes_a) + .zip(iter_half_bytes(bytes_b)) + .take_while(|(a, b)| a == b) + .count() +} + +fn iter_half_bytes(bytes: &[u8]) -> impl ExactSizeIterator + '_ { + (0..bytes.len() * 2).map(|i| { + let v = bytes[i / 2]; + if i & 1 == 0 { + v >> 4 + } else { + v & 0xf + } + }) +} + #[cfg(test)] #[cfg(test)] mod tests { diff --git a/lib/src/id_prefix.rs b/lib/src/id_prefix.rs index 1e5d4366f..8cfcf2b6c 100644 --- a/lib/src/id_prefix.rs +++ b/lib/src/id_prefix.rs @@ -21,7 +21,8 @@ use std::rc::Rc; use itertools::Itertools as _; use once_cell::unsync::OnceCell; -use crate::backend::{self, ChangeId, CommitId, ObjectId}; +use crate::backend::{ChangeId, CommitId, ObjectId}; +use crate::hex_util; use crate::index::{HexPrefix, PrefixResolution}; use crate::repo::Repo; use crate::revset::{DefaultSymbolResolver, RevsetExpression}; @@ -421,7 +422,7 @@ where // Left/right neighbors should have unique short keys. For the current chunk, // we need to look up full-length keys. - let unique_len = |a: &[u8], b: &[u8]| backend::common_hex_len(a, b) + 1; + let unique_len = |a: &[u8], b: &[u8]| hex_util::common_hex_len(a, b) + 1; let neighbor_lens = left .iter() .chain(&right)