forked from mirrors/jj
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.
This commit is contained in:
parent
329e128099
commit
dbaee198e6
4 changed files with 25 additions and 24 deletions
|
@ -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<Item = u8> + '_ {
|
||||
(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),
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -51,6 +51,26 @@ pub fn to_reverse_hex(forward_hex: &str) -> Option<String> {
|
|||
.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<Item = u8> + '_ {
|
||||
(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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue