jj_lib/
hex_util.rs

1// Copyright 2023 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(missing_docs)]
16
17const REVERSE_HEX_CHARS: &[u8; 16] = b"zyxwvutsrqponmlk";
18
19fn to_forward_hex_digit(b: u8) -> Option<u8> {
20    let value = match b {
21        b'k'..=b'z' => b'z' - b,
22        b'K'..=b'Z' => b'Z' - b,
23        _ => return None,
24    };
25    if value < 10 {
26        Some(b'0' + value)
27    } else {
28        Some(b'a' + value - 10)
29    }
30}
31
32pub fn to_forward_hex(reverse_hex: impl AsRef<[u8]>) -> Option<String> {
33    reverse_hex
34        .as_ref()
35        .iter()
36        .map(|b| to_forward_hex_digit(*b).map(char::from))
37        .collect()
38}
39
40/// Encodes `data` as hex string using `z-k` "digits".
41pub fn encode_reverse_hex(data: &[u8]) -> String {
42    let chars = REVERSE_HEX_CHARS;
43    let encoded = data
44        .iter()
45        .flat_map(|b| [chars[usize::from(b >> 4)], chars[usize::from(b & 0xf)]])
46        .collect();
47    String::from_utf8(encoded).unwrap()
48}
49
50/// Calculates common prefix length of two byte sequences. The length
51/// to be returned is a number of hexadecimal digits.
52pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize {
53    std::iter::zip(bytes_a, bytes_b)
54        .enumerate()
55        .find_map(|(i, (a, b))| match a ^ b {
56            0 => None,
57            d if d & 0xf0 == 0 => Some(i * 2 + 1),
58            _ => Some(i * 2),
59        })
60        .unwrap_or_else(|| bytes_a.len().min(bytes_b.len()) * 2)
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_reverse_hex() {
69        // Empty string
70        assert_eq!(encode_reverse_hex(b""), "".to_string());
71        assert_eq!(to_forward_hex(""), Some("".to_string()));
72
73        // Single digit
74        assert_eq!(to_forward_hex("z"), Some("0".to_string()));
75
76        // All digits
77        assert_eq!(
78            encode_reverse_hex(b"\x01\x23\x45\x67\x89\xab\xcd\xef"),
79            "zyxwvutsrqponmlk".to_string()
80        );
81        assert_eq!(
82            to_forward_hex("zyxwvutsrqponmlkPONMLK"),
83            Some("0123456789abcdefabcdef".to_string())
84        );
85
86        // Invalid digit
87        assert_eq!(to_forward_hex("j"), None);
88    }
89}