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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#![allow(clippy::len_without_is_empty)]

use crate::chr::{encode_utf8, Utf8Encoded};

#[doc(hidden)]
pub struct __NormalizeConcatArg<T: 'static>(pub &'static [T]);

impl __NormalizeConcatArg<char> {
    pub const fn conv(self) -> __StrConcatArg {
        __StrConcatArg::Char(self.0)
    }
}

impl __NormalizeConcatArg<&'static str> {
    pub const fn conv(self) -> __StrConcatArg {
        __StrConcatArg::Str(self.0)
    }
}

#[doc(hidden)]
#[derive(Copy, Clone)]
pub enum __StrConcatArg {
    Char(&'static [char]),
    Str(&'static [&'static str]),
}

#[doc(hidden)]
pub struct __MakeSepArg<T>(pub T);

#[doc(hidden)]
#[derive(Copy, Clone)]
pub enum __SepArg {
    Char(char),
    Str(&'static str),
}

impl __SepArg {
    const fn len(self) -> usize {
        match self {
            Self::Char(x) => x.len_utf8(),
            Self::Str(x) => x.len(),
        }
    }
}

#[doc(hidden)]
pub struct __ElemDispatch<T>(pub T);

macro_rules! __ref_unref_impls {
    ($($ref_token:tt $deref_token:tt)?) => {
        impl __MakeSepArg<$($ref_token)? char> {
            pub const fn conv(self) -> __SepArg {
                __SepArg::Char($($deref_token)? self.0)
            }
        }

        impl __MakeSepArg<$($ref_token)? &'static str> {
            pub const fn conv(self) -> __SepArg {
                __SepArg::Str(self.0)
            }
        }

        impl __ElemDispatch<$($ref_token)? char> {
            pub const fn as_bytesable(self) -> Utf8Encoded {
                encode_utf8($($deref_token)? self.0)
            }
            pub const fn len(self) -> usize {
                self.0.len_utf8()
            }
        }

        impl __ElemDispatch<$($ref_token)? &'static str> {
            pub const fn as_bytesable(self) -> &'static str {
                self.0
            }
            pub const fn len(self) -> usize {
                self.0.len()
            }
        }
    };
}

__ref_unref_impls! {}
__ref_unref_impls! {& *}

#[doc(hidden)]
#[macro_export]
macro_rules! __with_str_concat_slices {
    ($arg:expr, |$slices:ident| $with_slices:expr) => {
        match $arg {
            $crate::string::__StrConcatArg::Char($slices) => $with_slices,
            $crate::string::__StrConcatArg::Str($slices) => $with_slices,
        }
    };
}

////////////////////////////////////////////////////////////////////////////////

#[macro_export]
macro_rules! string_concat {
    ($(&)? []) => {
        ""
    };
    ($slice:expr $(,)*) => {{
        const __ARGS_81608BFNA5: $crate::string::__StrConcatArg =
            $crate::string::__NormalizeConcatArg($slice).conv();
        {
            const LEN: $crate::__::usize = $crate::string::concat_sum_lengths(__ARGS_81608BFNA5);

            const CONC: &$crate::string::ArrayStr<LEN> =
                &$crate::string::concat_strs(__ARGS_81608BFNA5);

            const STR: &$crate::__::str = CONC.as_str();

            STR
        }
    }};
}

pub const fn concat_sum_lengths(arg: __StrConcatArg) -> usize {
    let mut sum = 0usize;

    __with_str_concat_slices! {arg, |slices| {
        crate::for_range! {i in 0..slices.len() =>
            sum += __ElemDispatch(slices[i]).len();
        }
    }}

    sum
}

pub const fn concat_strs<const N: usize>(arg: __StrConcatArg) -> ArrayStr<N> {
    let mut out = [0u8; N];
    let mut out_i = 0usize;

    __with_str_concat_slices! {arg, |slices| {
        crate::for_range! {si in 0..slices.len() =>
            let byteser = __ElemDispatch(slices[si]).as_bytesable();
            let slice = byteser.as_bytes();
            crate::for_range! {i in 0..slice.len() =>
                out[out_i] = slice[i];
                out_i += 1;
            }
        }
    }}

    ArrayStr(out)
}

////////////////////////////////////////////////////////////////////////////////

#[macro_export]
macro_rules! string_join {
    ($sep:expr, $(&)? []) => {
        ""
    };
    ($sep:expr, $slice:expr $(,)*) => {{
        const __ARGS_81608BFNA5: $crate::string::StrJoinArgs = $crate::string::StrJoinArgs {
            sep: $crate::string::__MakeSepArg($sep).conv(),
            slice: $slice,
        };

        {
            const LEN: $crate::__::usize = $crate::string::join_sum_lengths(__ARGS_81608BFNA5);

            const CONC: &$crate::string::ArrayStr<LEN> =
                &$crate::string::join_strs(__ARGS_81608BFNA5);

            const STR: &$crate::__::str = CONC.as_str();

            STR
        }
    }};
}

#[derive(Copy, Clone)]
pub struct StrJoinArgs {
    pub sep: __SepArg,
    pub slice: &'static [&'static str],
}

pub const fn join_sum_lengths(StrJoinArgs { sep, slice }: StrJoinArgs) -> usize {
    if slice.is_empty() {
        0
    } else {
        concat_sum_lengths(__StrConcatArg::Str(slice)) + sep.len() * (slice.len() - 1)
    }
}

pub const fn join_strs<const N: usize>(
    StrJoinArgs { sep, slice: slices }: StrJoinArgs,
) -> ArrayStr<N> {
    let mut out = [0u8; N];
    let mut out_i = 0usize;

    let utf8e: Utf8Encoded;
    let sep = match sep {
        __SepArg::Char(c) => {
            utf8e = encode_utf8(c);
            utf8e.as_str()
        }
        __SepArg::Str(s) => s,
    };

    macro_rules! write_str {
        ($str:expr) => {{
            let slice = $str.as_bytes();
            crate::for_range! {i in 0..slice.len() =>
                out[out_i] = slice[i];
                out_i += 1;
            }
        }};
    }

    if let [first, rem_slices @ ..] = slices {
        write_str! {first}

        crate::for_range! {si in 0..rem_slices.len() =>
            write_str!{sep}
            write_str!{rem_slices[si]}
        }
    }

    ArrayStr(out)
}

////////////////////////////////////////////////////////////////////////////////

#[macro_export]
macro_rules! str_from_iter {
    ($($rem:tt)*) => {{
        $crate::__collect_const_iter_with!{
            $crate::__::u8,
            {},
            |array, written_length, item| {
                let byteser = $crate::string::__ElemDispatch(item).as_bytesable();
                let bytes = byteser.as_bytes();
                let item_len = bytes.len();
                let mut i = written_length;
                let mut j = 0;
                while j < item_len {
                    array[i] = $crate::__::MaybeUninit::new(bytes[j]);
                    i += 1;
                    j += 1;
                }
            },
            elem_length = {
                $crate::string::__ElemDispatch(item).len()
            },
            =>
            $($rem)*
        }

        const __STR81608BFNA5: &$crate::__::str =
            match core::str::from_utf8(&__ARR81608BFNA5) {
                $crate::__::Ok(x) => x,
                $crate::__::Err(_) => $crate::__::panic!("created string isn't UTF8"),
            };

        __STR81608BFNA5
    }}
}

////////////////////////////////////////////////////////////////////////////////

pub struct ArrayStr<const N: usize>([u8; N]);

impl<const N: usize> ArrayStr<N> {
    pub const fn as_str(&self) -> &str {
        match core::str::from_utf8(&self.0) {
            Ok(s) => s,
            Err(_) => panic!("bug: konst made an invalid string"),
        }
    }
}