const_combine/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#[macro_export]
macro_rules! const_combine {
    ($A:expr, $B:expr) => {
        unsafe {
            std::str::from_utf8_unchecked(
                $crate::internal::combine::<{ $A.len() + $B.len() }>($A, $B).as_slice(),
            )
        }
    };
}

/// useful when above doesn't work due to const expression limitations
#[macro_export]
macro_rules! const_combine_bounded_with {
    ($A:expr, $B:expr, $max_size:expr) => {
        unsafe {
            std::str::from_utf8_unchecked($crate::internal::take_n(
                &$crate::internal::combine::<$max_size>($A, $B),
                $A.len() + $B.len(),
            ))
        }
    };
}

/// does not work for strings larger than 1024 bytes
#[macro_export]
macro_rules! const_combine_bounded {
    ($A:expr, $B:expr) => {
        $crate::const_combine_bounded_with!($A, $B, 1024)
    };
}

pub mod bounded {
    pub use super::{
        const_combine_bounded as const_combine,
        const_combine_bounded_with as const_combine_with_len,
    };
}

pub mod internal {
    // based on https://users.rust-lang.org/t/concatenate-const-strings/51712/5
    pub const fn combine<const L: usize>(a: &'static str, b: &'static str) -> [u8; L] {
        let mut out = [0u8; L];
        out = copy_slice(a.as_bytes(), out, 0);
        out = copy_slice(b.as_bytes(), out, a.len());
        out
    }

    pub const fn copy_slice<const L: usize>(
        input: &[u8],
        mut output: [u8; L],
        offset: usize,
    ) -> [u8; L] {
        let mut index = 0;
        loop {
            output[offset + index] = input[index];
            index += 1;
            if index == input.len() {
                break;
            }
        }
        output
    }

    pub const fn take_n(input: &[u8], n: usize) -> &[u8] {
        let mut index = input.len();
        let mut slice = input;
        loop {
            match slice.split_last() {
                None => return slice,
                Some(_) if index == n => return slice,
                Some((_, tail)) => {
                    slice = tail;
                    index -= 1;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn concat_strings() {
        const STR: &str = "abc";

        assert_eq!(const_combine!(STR, "def"), "abcdef");
        assert_eq!(const_combine!("def", STR), "defabc");
        assert_eq!(const_combine!(STR, STR), "abcabc");
    }

    #[test]
    fn concat_strings_sized() {
        const STR: &str = "abc";

        assert_eq!(const_combine_bounded!(STR, "def"), "abcdef");
        assert_eq!(const_combine_bounded!("def", STR), "defabc");
        assert_eq!(const_combine_bounded!(STR, STR), "abcabc");
    }
}