Skip to content

Commit

Permalink
lib: Add convenience function to decode hex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 20, 2024
1 parent 09290e7 commit 1045bf4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,9 +1506,7 @@ fn tree_value_from_json(json: &serde_json::Value) -> TreeValue {

fn bytes_vec_from_json(value: &serde_json::Value) -> Vec<u8> {
let s = value.as_str().unwrap();
let mut val = vec![0; s.len() / 2];
faster_hex::hex_decode(s.as_bytes(), &mut val).unwrap();
val
crate::hex_util::decode_hex_string(s).unwrap()
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions lib/src/hex_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ pub fn to_reverse_hex(forward_hex: &str) -> Option<String> {
.collect()
}

pub fn decode_hex_string(hex: &str) -> Option<Vec<u8>> {
let mut dst = vec![0; hex.len() / 2];
faster_hex::hex_decode(hex.as_bytes(), &mut dst)
.ok()
.map(|()| dst)
}

/// Calculates common prefix length of two byte sequences. The length
/// to be returned is a number of hexadecimal digits.
pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize {
Expand Down
9 changes: 5 additions & 4 deletions lib/src/object_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ macro_rules! impl_id_type {
pub(crate) use id_type;
pub(crate) use impl_id_type;

use crate::hex_util::decode_hex_string;

/// An identifier prefix (typically from a type implementing the [`ObjectId`]
/// trait) with facilities for converting between bytes and a hex string.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -120,11 +122,10 @@ impl HexPrefix {
/// hex to bytes.
pub fn new(prefix: &str) -> Option<HexPrefix> {
let has_odd_byte = prefix.len() & 1 != 0;
let mut min_prefix_bytes = vec![0; prefix.len() / 2 + has_odd_byte as usize];
if has_odd_byte {
faster_hex::hex_decode(&format!("{prefix}0").as_bytes(), &mut min_prefix_bytes).ok()?;
let min_prefix_bytes = if has_odd_byte {
decode_hex_string(&format!("{prefix}0"))?
} else {
faster_hex::hex_decode(prefix.as_bytes(), &mut min_prefix_bytes).ok()?;
decode_hex_string(prefix)?
};
Some(HexPrefix {
min_prefix_bytes,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/simple_op_heads_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::fs;
use std::path::Path;
use std::path::PathBuf;

use crate::hex_util::decode_hex_string;
use crate::lock::FileLock;
use crate::object_id::ObjectId;
use crate::op_heads_store::OpHeadsStore;
Expand Down Expand Up @@ -97,8 +98,7 @@ impl OpHeadsStore for SimpleOpHeadsStore {
let op_head_file_name = op_head_entry.unwrap().file_name();
let op_head_file_name = op_head_file_name.to_str().unwrap();

let mut op_head = vec![0; op_head_file_name.len() / 2];
if faster_hex::hex_decode(op_head_file_name.as_bytes(), &mut op_head).is_ok() {
if let Some(op_head) = decode_hex_string(op_head_file_name) {
op_heads.push(OperationId::new(op_head));
}
}
Expand Down

0 comments on commit 1045bf4

Please sign in to comment.