use xxhash_rust::xxh3::xxh3_64;
#[inline]
#[must_use]
pub fn generate_hash(content: &str, file_path: Option<&str>) -> String {
let capacity = file_path.map_or(content.len(), |p| p.len() + 2 + content.len());
let mut hasher_input = String::with_capacity(capacity);
if let Some(path) = file_path {
hasher_input.push_str(path);
hasher_input.push_str("::");
}
hasher_input.push_str(content);
let hash = xxh3_64(hasher_input.as_bytes());
format!("sc_{}", encode_base62(hash))
}
#[inline]
fn encode_base62(mut num: u64) -> String {
const BASE62_CHARS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if num == 0 {
return String::from("0");
}
let mut result = Vec::with_capacity(11);
while num > 0 {
result.push(BASE62_CHARS[(num % 62) as usize]);
num /= 62;
}
result.reverse();
unsafe { String::from_utf8_unchecked(result) }
}