From e02c5b5930c2c9ba3e5423fe12e2ef0155017297 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 30 May 2024 20:31:36 +0200 Subject: Merging upstream version 1.74.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/gix-hash/src/prefix.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'vendor/gix-hash/src/prefix.rs') diff --git a/vendor/gix-hash/src/prefix.rs b/vendor/gix-hash/src/prefix.rs index 166b3c3e7..c8e1b1847 100644 --- a/vendor/gix-hash/src/prefix.rs +++ b/vendor/gix-hash/src/prefix.rs @@ -28,8 +28,8 @@ pub mod from_hex { TooShort { hex_len: usize }, #[error("An id cannot be larger than {} chars in hex, but {hex_len} was requested", crate::Kind::longest().len_in_hex())] TooLong { hex_len: usize }, - #[error("Invalid character {c} at position {index}")] - Invalid { c: char, index: usize }, + #[error("Invalid hex character")] + Invalid, } } @@ -41,8 +41,7 @@ impl Prefix { /// /// For instance, with `hex_len` of 7 the resulting prefix is 3.5 bytes, or 3 bytes and 4 bits /// wide, with all other bytes and bits set to zero. - pub fn new(id: impl AsRef, hex_len: usize) -> Result { - let id = id.as_ref(); + pub fn new(id: &oid, hex_len: usize) -> Result { if hex_len > id.kind().len_in_hex() { Err(Error::TooLong { object_kind: id.kind(), @@ -95,7 +94,6 @@ impl Prefix { /// Create an instance from the given hexadecimal prefix `value`, e.g. `35e77c16` would yield a `Prefix` with `hex_len()` = 8. pub fn from_hex(value: &str) -> Result { - use hex::FromHex; let hex_len = value.len(); if hex_len > crate::Kind::longest().len_in_hex() { @@ -105,16 +103,20 @@ impl Prefix { }; let src = if value.len() % 2 == 0 { - Vec::from_hex(value) + let mut out = Vec::from_iter(std::iter::repeat(0).take(value.len() / 2)); + faster_hex::hex_decode(value.as_bytes(), &mut out).map(move |_| out) } else { + // TODO(perf): do without heap allocation here. let mut buf = [0u8; crate::Kind::longest().len_in_hex()]; buf[..value.len()].copy_from_slice(value.as_bytes()); buf[value.len()] = b'0'; - Vec::from_hex(&buf[..=value.len()]) + let src = &buf[..=value.len()]; + let mut out = Vec::from_iter(std::iter::repeat(0).take(src.len() / 2)); + faster_hex::hex_decode(src, &mut out).map(move |_| out) } .map_err(|e| match e { - hex::FromHexError::InvalidHexCharacter { c, index } => from_hex::Error::Invalid { c, index }, - hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength => panic!("This is already checked"), + faster_hex::Error::InvalidChar => from_hex::Error::Invalid, + faster_hex::Error::InvalidLength(_) => panic!("This is already checked"), })?; let mut bytes = ObjectId::null(crate::Kind::from_hex_len(value.len()).expect("hex-len is already checked")); -- cgit v1.2.3