Skip to main content

keramics_core/
formatters.rs

1/* Copyright 2024-2026 Joachim Metz <joachim.metz@gmail.com>
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. You may
5 * obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 * License for the specific language governing permissions and limitations
11 * under the License.
12 */
13
14/// Formats an array for debugging purposes.
15pub fn debug_format_array(array: &[String]) -> String {
16    // TODO: pass string for indentation?
17    let number_of_values: usize = array.len();
18    if number_of_values == 0 {
19        return String::from("[]");
20    }
21    let string_value: &String = &array[0];
22    let mut use_short_hand: bool = true;
23
24    for array_value in array.iter() {
25        if string_value != array_value {
26            use_short_hand = false;
27            break;
28        }
29    }
30    if use_short_hand {
31        return format!("[{}; {}]", string_value, number_of_values);
32    }
33    if number_of_values > 16 {
34        let mut string_parts: Vec<String> = Vec::new();
35        let last_value_index: usize = number_of_values - 16;
36        for value_index in (0..last_value_index).step_by(16) {
37            string_parts.push(array[value_index..value_index + 16].join(", "));
38        }
39        string_parts.push(array[last_value_index..].join(", "));
40
41        return format!("[\n        {}\n    ]", string_parts.join(",\n        "));
42    }
43    format!("[{}]", array.join(", "))
44}
45
46/// Formats a bytes as a hexdump.
47pub fn format_as_hexdump(data: &[u8], group: bool) -> String {
48    let mut ascii_values: [char; 16] = ['.'; 16];
49    let mut character_index: usize = 0;
50    let mut data_offset: usize = 0;
51    let data_size: usize = data.len();
52    let groups_data_size: usize = (data_size / 16) * 16;
53    let mut group_data_offset: usize = 0;
54    let mut string_parts: Vec<String> = Vec::new();
55
56    while data_offset < groups_data_size {
57        if character_index == 0 {
58            if group
59                && group_data_offset != data_offset
60                && data[group_data_offset..group_data_offset + 16]
61                    == data[data_offset..data_offset + 16]
62                && data_offset + 16 < groups_data_size
63            {
64                if group_data_offset + 16 == data_offset {
65                    string_parts.push(String::from("...\n"))
66                }
67                data_offset += 16;
68                continue;
69            }
70            group_data_offset = data_offset;
71
72            string_parts.push(format!("{:08x}  ", data_offset));
73        }
74        let byte_value: u8 = data[data_offset];
75
76        ascii_values[character_index] = match byte_value {
77            0x20..0x7f => byte_value as char,
78            _ => '.',
79        };
80        string_parts.push(format!("{:02x} ", byte_value));
81
82        if character_index == 7 {
83            string_parts.push(String::from(" "));
84        }
85        if character_index == 15 {
86            character_index += 1;
87
88            string_parts.push(String::from(" "));
89
90            for ascii_value in ascii_values.iter().take(character_index) {
91                string_parts.push(format!("{}", ascii_value));
92            }
93            string_parts.push(String::from("\n"));
94
95            character_index = 0;
96        } else {
97            character_index += 1;
98        }
99        data_offset += 1;
100    }
101    while data_offset < data_size {
102        character_index = data_offset % 16;
103        if character_index == 0 {
104            string_parts.push(format!("{:08x}  ", data_offset));
105        }
106        let byte_value: u8 = data[data_offset];
107
108        ascii_values[character_index] = match byte_value {
109            0x20..0x7f => byte_value as char,
110            _ => '.',
111        };
112        string_parts.push(format!("{:02x} ", data[data_offset]));
113
114        if character_index == 7 {
115            string_parts.push(String::from(" "));
116        }
117        data_offset += 1;
118    }
119    if data_size > 0 && character_index > 0 {
120        character_index += 1;
121
122        for index in character_index..16 {
123            if index == 7 {
124                string_parts.push(String::from(" "));
125            }
126            string_parts.push(String::from("   "));
127        }
128        string_parts.push(String::from(" "));
129
130        for ascii_value in ascii_values.iter().take(character_index) {
131            string_parts.push(format!("{}", ascii_value));
132        }
133        string_parts.push(String::from("\n"));
134    }
135    string_parts.push(String::from("\n"));
136
137    string_parts.join("")
138}
139
140/// Formats a bytes as a string.
141pub fn format_as_string(data: &[u8]) -> String {
142    data.iter()
143        .map(|byte_value| format!("{:02x}", byte_value))
144        .collect::<Vec<String>>()
145        .join("")
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151
152    #[test]
153    fn test_debug_format_array() {
154        let test_data: Vec<String> = ["0"; 16]
155            .iter()
156            .map(|v| v.to_string())
157            .collect::<Vec<String>>();
158
159        let string: String = debug_format_array(&test_data);
160        assert_eq!(string, "[0; 16]");
161
162        let test_data: Vec<String> = [
163            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
164        ]
165        .iter()
166        .map(|v| v.to_string())
167        .collect::<Vec<String>>();
168
169        let string: String = debug_format_array(&test_data);
170        assert_eq!(
171            string,
172            "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]"
173        );
174    }
175
176    #[test]
177    fn test_debug_format_array_with_0bytes() {
178        let test_data: Vec<String> = Vec::new();
179
180        let string: String = debug_format_array(&test_data);
181        assert_eq!(string, "[]");
182    }
183
184    #[test]
185    fn test_debug_format_array_with_3bytes() {
186        let test_data: Vec<String> = ["1"; 3].iter().map(|value| value.to_string()).collect();
187
188        let string: String = debug_format_array(&test_data);
189        assert_eq!(string, "[1; 3]");
190    }
191
192    #[test]
193    fn test_debug_format_array_with_32bytes() {
194        let test_data: Vec<String> = (0..32).map(|value| value.to_string()).collect();
195
196        let first_row: String = (0..16)
197            .map(|value| value.to_string())
198            .collect::<Vec<String>>()
199            .join(", ");
200
201        let last_row: String = (16..32)
202            .map(|value| value.to_string())
203            .collect::<Vec<String>>()
204            .join(", ");
205
206        let string: String = debug_format_array(&test_data);
207        assert_eq!(
208            string,
209            format!("[\n        {},\n        {}\n    ]", first_row, last_row)
210        );
211    }
212
213    #[test]
214    fn test_format_as_hexdump_0bytes() {
215        let test_data: [u8; 0] = [];
216        let expected_string = ["", ""].join("\n");
217
218        let string: String = format_as_hexdump(&test_data, false);
219        assert_eq!(string, expected_string);
220    }
221
222    #[test]
223    fn test_format_as_hexdump_3bytes() {
224        let test_data: [u8; 3] = [0x61, 0x62, 0x63];
225        let expected_string = [
226            "00000000  61 62 63                                          abc",
227            "",
228            "",
229        ]
230        .join("\n");
231
232        let string: String = format_as_hexdump(&test_data, false);
233        assert_eq!(string, expected_string);
234    }
235
236    #[test]
237    fn test_format_as_hexdump_9bytes() {
238        let test_data: [u8; 9] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
239        let expected_string = [
240            "00000000  00 01 02 03 04 05 06 07  08                       .........",
241            "",
242            "",
243        ]
244        .join("\n");
245
246        let string: String = format_as_hexdump(&test_data, false);
247        assert_eq!(string, expected_string);
248    }
249
250    #[test]
251    fn test_format_as_hexdump_16bytes() {
252        let test_data: [u8; 16] = [
253            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
254            0x0e, 0x0f,
255        ];
256        let expected_string = [
257            "00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  ................",
258            "",
259            "",
260        ]
261        .join("\n");
262
263        let string: String = format_as_hexdump(&test_data, false);
264        assert_eq!(string, expected_string);
265    }
266
267    #[test]
268    fn test_format_as_hexdump_with_group() {
269        let test_data: [u8; 428] = [
270            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
274            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
275            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
276            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
277            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
280            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
282            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
285            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
288            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
298            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
299            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301        ];
302        let expected_string = [
303            "00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................",
304            "...",
305            "00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................",
306            "000001a0  00 00 00 00 00 00 00 00  00 00 00 00              ............",
307            "",
308            "",
309        ]
310        .join("\n");
311
312        let string: String = format_as_hexdump(&test_data, true);
313        assert_eq!(string, expected_string);
314    }
315
316    #[test]
317    fn test_format_as_hexdump_with_group_16byte_aligned() {
318        let test_data: [u8; 512] = [
319            0x63, 0x6f, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x78, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,
320            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x28, 0x8c, 0x38, 0x27,
321            0x71, 0x65, 0x6d, 0x75, 0x00, 0x05, 0x00, 0x03, 0x57, 0x69, 0x32, 0x6b, 0x00, 0x00,
322            0x00, 0x00, 0x00, 0x40, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x48, 0x00,
323            0x00, 0x79, 0x04, 0x11, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xef, 0xc4, 0xb6, 0x1f,
324            0x53, 0xca, 0xa7, 0x86, 0x45, 0x28, 0x90, 0xe2, 0x55, 0xba, 0x79, 0x1a, 0x1c, 0x4c,
325            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
333            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
336            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
337            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
338            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
350            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
352            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356        ];
357        // typos:disable
358        let expected_string = [
359            "00000000  63 6f 6e 65 63 74 69 78  00 00 00 02 00 01 00 00  conectix........",
360            "00000010  00 00 00 00 00 00 02 00  28 8c 38 27 71 65 6d 75  ........(.8'qemu",
361            "00000020  00 05 00 03 57 69 32 6b  00 00 00 00 00 40 48 00  ....Wi2k.....@H.",
362            "00000030  00 00 00 00 00 40 48 00  00 79 04 11 00 00 00 03  .....@H..y......",
363            "00000040  ff ff ef c4 b6 1f 53 ca  a7 86 45 28 90 e2 55 ba  ......S...E(..U.",
364            "00000050  79 1a 1c 4c 00 00 00 00  00 00 00 00 00 00 00 00  y..L............",
365            "00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................",
366            "...",
367            "000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................",
368            "",
369            "",
370        ]
371        .join("\n");
372        // typos:enable
373
374        let string: String = format_as_hexdump(&test_data, true);
375        assert_eq!(string, expected_string);
376    }
377
378    #[test]
379    fn test_format_as_string() {
380        let test_data: [u8; 16] = [
381            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
382            0x0e, 0x0f,
383        ];
384        let string: String = format_as_string(&test_data);
385        assert_eq!(string, "000102030405060708090a0b0c0d0e0f");
386    }
387}