svdtools/common/
str_utils.rs

1pub fn unwrap_or_empty_str(opt_str: &Option<String>) -> &str {
2    match opt_str {
3        Some(desc) => desc,
4        None => "",
5    }
6}
7
8pub fn get_description(opt_str: &Option<String>) -> String {
9    let desc: &str = unwrap_or_empty_str(opt_str);
10
11    // remove duplicate whitespaces
12    let words: Vec<&str> = desc.split_whitespace().collect();
13
14    words.join(" ")
15}
16
17/// Make everything uppercase except first two character, which should be "0x"
18pub fn format_address(hex_address: u64) -> String {
19    let addr = format! {"{hex_address:x}"};
20    let addr = addr.to_uppercase();
21    format!("0x{addr}")
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn correctly_format_address() {
30        let addr: u32 = 0xde4dBeeF;
31        let formatted_addr = format_address(addr as u64);
32        assert_eq!(formatted_addr, "0xDE4DBEEF");
33    }
34}