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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![allow(unsafe_code)]

use super::StrBuf;

pub struct Concat<'a>(pub &'a [&'a str]);

impl<'a> Concat<'a> {
    pub const fn output_len(&self) -> usize {
        let mut ans = 0;
        let mut iter = self.0;
        while let [x, xs @ ..] = iter {
            ans += x.len();
            iter = xs;
        }
        ans
    }

    pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
        let mut buf = [0; N];
        let mut pos = 0;

        let mut iter = self.0;
        while let [x, xs @ ..] = iter {
            let x = x.as_bytes();
            let mut i = 0;
            while i < x.len() {
                buf[pos] = x[i];
                pos += 1;
                i += 1;
            }
            iter = xs;
        }
        assert!(pos == N);

        unsafe { StrBuf::new_unchecked(buf) }
    }
}

/// Concatenates values into a string slice.
///
/// The input type must be one of
///
/// + [`&str`]
/// + [`char`]
/// + [`bool`]
/// + [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`]
/// + [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`isize`]
///
///
/// # Examples
///
/// ```
/// const PROMPT: &str = "The answer is";
/// const ANSWER: usize = 42;
/// const MESSAGE: &str = const_str::concat!(PROMPT, " ", ANSWER);
///
/// assert_eq!(MESSAGE, "The answer is 42");
/// ```
///
#[macro_export]
macro_rules! concat {
    ($($x: expr),+ $(,)?) => {{
        const STRS: &[&str] = &[$( $crate::to_str!($x) ),+];
        const OUTPUT_LEN: usize = $crate::__ctfe::Concat(STRS).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> = $crate::__ctfe::Concat(STRS).const_eval();
        OUTPUT_BUF.as_str()
    }}
}

pub struct Join<'a>(pub &'a [&'a str], pub &'a str);

impl<'a> Join<'a> {
    pub const fn output_len(&self) -> usize {
        let mut ans = 0;
        let mut i = 0;
        while i < self.0.len() {
            ans += self.0[i].len();
            if i < self.0.len() - 1 {
                ans += self.1.len();
            }
            i += 1;
        }
        ans
    }

    pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
        let mut buf = [0; N];
        let mut pos = 0;

        let mut i = 0;
        while i < self.0.len() {
            let x = self.0[i].as_bytes();
            let mut j = 0;
            while j < x.len() {
                buf[pos] = x[j];
                pos += 1;
                j += 1;
            }
            if i < self.0.len() - 1 {
                let sep = self.1.as_bytes();
                let mut j = 0;
                while j < sep.len() {
                    buf[pos] = sep[j];
                    pos += 1;
                    j += 1;
                }
            }
            i += 1;
        }

        unsafe { StrBuf::new_unchecked(buf) }
    }
}

/// Concatenates string slices into a string slice, separated by a given separator.
///
/// # Examples
///
/// ```
/// const WORDS: &[&str] = &["hello", "world"];
/// const MESSAGE1: &str = const_str::join!(WORDS, " ");
/// assert_eq!(MESSAGE1, "hello world");
///
/// const NUMS: &[&str] = &["1", "2", "3"];
/// const MESSAGE2: &str = const_str::join!(NUMS, ", ");
/// assert_eq!(MESSAGE2, "1, 2, 3");
///
/// const EMPTY: &[&str] = &[];
/// const MESSAGE3: &str = const_str::join!(EMPTY, ", ");
/// assert_eq!(MESSAGE3, "");
/// ```
#[macro_export]
macro_rules! join {
    ($strs: expr, $sep: expr) => {{
        const STRS: &[&str] = $strs;
        const SEP: &str = $sep;
        const OUTPUT_LEN: usize = $crate::__ctfe::Join(STRS, SEP).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::Join(STRS, SEP).const_eval();
        OUTPUT_BUF.as_str()
    }};
}