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
#![allow(unsafe_code)]

use super::StrBuf;

pub struct Repeat<T>(pub T, pub usize);

impl Repeat<&str> {
    pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
        let buf = bytes_repeat(self.0.as_bytes(), self.1);
        unsafe { StrBuf::new_unchecked(buf) }
    }
}

const fn bytes_repeat<const N: usize>(bytes: &[u8], n: usize) -> [u8; N] {
    constfn_assert!(bytes.len().checked_mul(n).is_some());
    constfn_assert!(bytes.len() * n == N);
    let mut buf = [0; N];
    let mut i = 0;
    let mut j = 0;
    while i < n {
        let mut k = 0;
        while k < bytes.len() {
            buf[j] = bytes[k];
            j += 1;
            k += 1;
        }
        i += 1;
    }
    buf
}

/// Creates a new string slice by repeating a string slice n times.
///
/// # Examples
///
/// ```
/// const S: &str = "abc";
/// const SSSS: &str = const_str::repeat!(S, 4);
/// assert_eq!(SSSS, "abcabcabcabc");
/// ```
///
#[macro_export]
macro_rules! repeat {
    ($s: expr, $n: expr) => {{
        const INPUT: &str = $s;
        const N: usize = $n;
        const OUTPUT_LEN: usize = INPUT.len() * N;
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::Repeat(INPUT, N).const_eval();
        $crate::__strbuf_as_str!(&OUTPUT_BUF)
    }};
}