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: &str) -> Option<String> {
33    reverse_hex
34        .bytes()
35        .map(|b| to_forward_hex_digit(b).map(char::from))
36        .collect()
37}
38
39/// Encodes `data` as hex string using `z-k` "digits".
40pub fn encode_reverse_hex(data: &[u8]) -> String {
41    let chars = REVERSE_HEX_CHARS;
42    let encoded = data
43        .iter()
44        .flat_map(|b| [chars[usize::from(b >> 4)], chars[usize::from(b & 0xf)]])
45        .collect();
46    String::from_utf8(encoded).unwrap()
47}
48
49/// Calculates common prefix length of two byte sequences. The length
50/// to be returned is a number of hexadecimal digits.
51pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize {
52    std::iter::zip(bytes_a, bytes_b)
53        .enumerate()
54        .find_map(|(i, (a, b))| match a ^ b {
55            0 => None,
56            d if d & 0xf0 == 0 => Some(i * 2 + 1),
57            _ => Some(i * 2),
58        })
59        .unwrap_or_else(|| bytes_a.len().min(bytes_b.len()) * 2)
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_reverse_hex() {
68        // Empty string
69        assert_eq!(encode_reverse_hex(b""), "".to_string());
70        assert_eq!(to_forward_hex(""), Some("".to_string()));
71
72        // Single digit
73        assert_eq!(to_forward_hex("z"), Some("0".to_string()));
74
75        // All digits
76        assert_eq!(
77            encode_reverse_hex(b"\x01\x23\x45\x67\x89\xab\xcd\xef"),
78            "zyxwvutsrqponmlk".to_string()
79        );
80        assert_eq!(
81            to_forward_hex("zyxwvutsrqponmlkPONMLK"),
82            Some("0123456789abcdefabcdef".to_string())
83        );
84
85        // Invalid digit
86        assert_eq!(to_forward_hex("j"), None);
87    }
88}