const-str 1.1.0

compile-time string operations
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#![allow(unsafe_code)]

use super::StrBuf;
use super::ToStr;

use crate::slice::advance;
use crate::utf8::CharEscapeDebug;
use crate::utf8::CharEscapeDebugArgs;

#[derive(Clone, Copy)]
pub struct FmtSpec {
    pub alternate: bool,
}

pub struct Display<T>(pub T, pub FmtSpec);

macro_rules! delegate_display {
    ($($ty: ty,)+) => {
        $(
            impl Display<$ty> {
                pub const fn output_len(&self) -> usize {
                    ToStr(self.0).output_len()
                }

                pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                    ToStr(self.0).const_eval()
                }
            }
        )+
    };
}

delegate_display!(&str, char, bool, u8, u16, u32, u64, usize, i8, i16, i32, i64, isize,);

#[doc(hidden)]
#[macro_export]
macro_rules! __fmt_display {
    ($x: expr, $spec: expr) => {{
        const OUTPUT_LEN: usize = $crate::__ctfe::Display($x, $spec).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::Display($x, $spec).const_eval();
        OUTPUT_BUF.as_str()
    }};
}

pub struct Debug<T>(pub T, pub FmtSpec);

macro_rules! delegate_debug {
    ($($ty: ty,)+) => {
        $(
            impl Debug<$ty> {
                pub const fn output_len(&self) -> usize {
                    ToStr(self.0).output_len()
                }

                pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                    ToStr(self.0).const_eval()
                }
            }
        )+
    };
}

delegate_debug!(bool, u8, u16, u32, u64, usize, i8, i16, i32, i64, isize,);

impl Debug<char> {
    pub const fn output_len(&self) -> usize {
        let escape = CharEscapeDebug::new(
            self.0,
            CharEscapeDebugArgs {
                escape_single_quote: true,
                escape_double_quote: false,
            },
        );

        escape.as_bytes().len() + 2
    }

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

        macro_rules! push {
            ($x: expr) => {{
                buf[pos] = $x;
                pos += 1;
            }};
        }

        push!(b'\'');
        {
            let e = CharEscapeDebug::new(
                self.0,
                CharEscapeDebugArgs {
                    escape_single_quote: true,
                    escape_double_quote: false,
                },
            );
            let bytes = e.as_bytes();
            let mut i = 0;
            while i < bytes.len() {
                push!(bytes[i]);
                i += 1;
            }
        }
        push!(b'\'');

        assert!(pos == N);

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

impl Debug<&str> {
    pub const fn output_len(&self) -> usize {
        let mut s = self.0.as_bytes();
        let mut ans = 2;
        while let Some((ch, count)) = crate::utf8::next_char(s) {
            s = advance(s, count);
            let e = CharEscapeDebug::new(
                ch,
                CharEscapeDebugArgs {
                    escape_single_quote: false,
                    escape_double_quote: true,
                },
            );
            ans += e.as_bytes().len()
        }
        ans
    }

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

        macro_rules! push {
            ($x: expr) => {{
                buf[pos] = $x;
                pos += 1;
            }};
        }

        push!(b'"');

        let mut s = self.0.as_bytes();
        while let Some((ch, count)) = crate::utf8::next_char(s) {
            s = advance(s, count);
            let e = CharEscapeDebug::new(
                ch,
                CharEscapeDebugArgs {
                    escape_single_quote: false,
                    escape_double_quote: true,
                },
            );
            let bytes = e.as_bytes();
            let mut i = 0;
            while i < bytes.len() {
                push!(bytes[i]);
                i += 1;
            }
        }

        push!(b'"');

        assert!(pos == N);

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

#[doc(hidden)]
#[macro_export]
macro_rules! __fmt_debug {
    ($x: expr, $spec: expr) => {{
        const OUTPUT_LEN: usize = $crate::__ctfe::Debug($x, $spec).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::Debug($x, $spec).const_eval();
        OUTPUT_BUF.as_str()
    }};
}

struct Hex<T>(T, FmtSpec, bool);

pub struct LowerHex<T>(pub T, pub FmtSpec);
pub struct UpperHex<T>(pub T, pub FmtSpec);

macro_rules! impl_integer_hex {
    ($unsigned: ty, $signed: ty) => {
        impl Hex<$unsigned> {
            const fn output_len(&self) -> usize {
                let mut x = self.0;
                let mut ans = 0;
                loop {
                    ans += 1;
                    x /= 16;
                    if x == 0 {
                        break;
                    }
                }
                if self.1.alternate {
                    ans += 2;
                }
                ans
            }

            const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let mut buf = [0; N];
                let mut pos = 0;
                let mut x = self.0;
                loop {
                    let d = crate::ascii::num_to_hex_digit((x % 16) as u8);
                    buf[pos] = if self.2 { d.to_ascii_uppercase() } else { d };
                    pos += 1;
                    x /= 16;
                    if x == 0 {
                        break;
                    }
                }
                if self.1.alternate {
                    buf[pos] = b'x';
                    pos += 1;
                    buf[pos] = b'0';
                    pos += 1;
                }
                assert!(pos == N);
                let buf = crate::bytes::reversed(buf);
                unsafe { StrBuf::new_unchecked(buf) }
            }
        }

        impl LowerHex<$unsigned> {
            pub const fn output_len(&self) -> usize {
                let h = Hex(self.0, self.1, false);
                h.output_len()
            }

            pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let h = Hex(self.0, self.1, false);
                h.const_eval()
            }
        }

        impl UpperHex<$unsigned> {
            pub const fn output_len(&self) -> usize {
                let h = Hex(self.0, self.1, true);
                h.output_len()
            }

            pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let h = Hex(self.0, self.1, true);
                h.const_eval()
            }
        }

        impl LowerHex<$signed> {
            pub const fn output_len(&self) -> usize {
                let h = Hex(self.0 as $unsigned, self.1, false);
                h.output_len()
            }

            pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let h = Hex(self.0 as $unsigned, self.1, false);
                h.const_eval()
            }
        }

        impl UpperHex<$signed> {
            pub const fn output_len(&self) -> usize {
                let h = Hex(self.0 as $unsigned, self.1, true);
                h.output_len()
            }

            pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let h = Hex(self.0 as $unsigned, self.1, true);
                h.const_eval()
            }
        }
    };
}

impl_integer_hex!(u8, i8);
impl_integer_hex!(u16, i16);
impl_integer_hex!(u32, i32);
impl_integer_hex!(u64, i64);
impl_integer_hex!(u128, i128);
impl_integer_hex!(usize, isize);

#[doc(hidden)]
#[macro_export]
macro_rules! __fmt_lowerhex {
    ($x: expr, $spec: expr) => {{
        const OUTPUT_LEN: usize = $crate::__ctfe::LowerHex($x, $spec).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::LowerHex($x, $spec).const_eval();
        OUTPUT_BUF.as_str()
    }};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __fmt_upperhex {
    ($x: expr, $spec: expr) => {{
        const OUTPUT_LEN: usize = $crate::__ctfe::UpperHex($x, $spec).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::UpperHex($x, $spec).const_eval();
        OUTPUT_BUF.as_str()
    }};
}

pub struct Binary<T>(pub T, pub FmtSpec);

macro_rules! impl_integer_binary {
    ($unsigned: ty, $signed: ty) => {
        impl Binary<$unsigned> {
            pub const fn output_len(&self) -> usize {
                let mut x = self.0;
                let mut ans = 0;
                loop {
                    ans += 1;
                    x /= 2;
                    if x == 0 {
                        break;
                    }
                }
                if self.1.alternate {
                    ans += 2;
                }
                ans
            }

            pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let mut buf = [0; N];
                let mut pos = 0;
                let mut x = self.0;
                loop {
                    buf[pos] = b'0' + (x % 2) as u8;
                    pos += 1;
                    x /= 2;
                    if x == 0 {
                        break;
                    }
                }
                if self.1.alternate {
                    buf[pos] = b'b';
                    pos += 1;
                    buf[pos] = b'0';
                    pos += 1;
                }
                assert!(pos == N);
                let buf = crate::bytes::reversed(buf);
                unsafe { StrBuf::new_unchecked(buf) }
            }
        }

        impl Binary<$signed> {
            pub const fn output_len(&self) -> usize {
                let b = Binary(self.0 as $unsigned, self.1);
                b.output_len()
            }

            pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
                let b = Binary(self.0 as $unsigned, self.1);
                b.const_eval()
            }
        }
    };
}

impl_integer_binary!(u8, i8);
impl_integer_binary!(u16, i16);
impl_integer_binary!(u32, i32);
impl_integer_binary!(u64, i64);
impl_integer_binary!(u128, i128);
impl_integer_binary!(usize, isize);

#[doc(hidden)]
#[macro_export]
macro_rules! __fmt_binary {
    ($x: expr, $spec: expr) => {{
        const OUTPUT_LEN: usize = $crate::__ctfe::Binary($x, $spec).output_len();
        const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> =
            $crate::__ctfe::Binary($x, $spec).const_eval();
        OUTPUT_BUF.as_str()
    }};
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_display_runtime() {
        let spec = FmtSpec { alternate: false };

        // Test Display for various types
        let display_str = Display("hello", spec);
        assert_eq!(display_str.output_len(), 5);
        let buf: StrBuf<5> = display_str.const_eval();
        assert_eq!(buf.as_str(), "hello");

        let display_char = Display('A', spec);
        assert_eq!(display_char.output_len(), 1);
        let buf_char: StrBuf<1> = display_char.const_eval();
        assert_eq!(buf_char.as_str(), "A");

        let display_bool = Display(true, spec);
        assert_eq!(display_bool.output_len(), 4);
        let buf_bool: StrBuf<4> = display_bool.const_eval();
        assert_eq!(buf_bool.as_str(), "true");

        let display_u8 = Display(42u8, spec);
        assert_eq!(display_u8.output_len(), 2);
        let buf_u8: StrBuf<2> = display_u8.const_eval();
        assert_eq!(buf_u8.as_str(), "42");

        let display_i32 = Display(-123i32, spec);
        assert_eq!(display_i32.output_len(), 4);
        let buf_i32: StrBuf<4> = display_i32.const_eval();
        assert_eq!(buf_i32.as_str(), "-123");

        // Test more integer types
        let display_u16 = Display(999u16, spec);
        let buf_u16: StrBuf<3> = display_u16.const_eval();
        assert_eq!(buf_u16.as_str(), "999");

        let display_i8 = Display(-99i8, spec);
        let buf_i8: StrBuf<3> = display_i8.const_eval();
        assert_eq!(buf_i8.as_str(), "-99");

        let display_u64 = Display(123456u64, spec);
        let buf_u64: StrBuf<6> = display_u64.const_eval();
        assert_eq!(buf_u64.as_str(), "123456");

        let display_i64 = Display(-999i64, spec);
        let buf_i64: StrBuf<4> = display_i64.const_eval();
        assert_eq!(buf_i64.as_str(), "-999");
    }

    #[test]
    fn test_debug_runtime() {
        let spec = FmtSpec { alternate: false };

        // Test Debug for str
        let debug_str = Debug("test", spec);
        assert_eq!(debug_str.output_len(), 6); // "test" with quotes
        let buf: StrBuf<6> = debug_str.const_eval();
        assert_eq!(buf.as_str(), "\"test\"");

        // Test Debug for char
        let debug_char = Debug('a', spec);
        assert_eq!(debug_char.output_len(), 3); // 'a' with quotes
        let buf2: StrBuf<3> = debug_char.const_eval();
        assert_eq!(buf2.as_str(), "'a'");

        // Test Debug for special chars
        let debug_newline = Debug('\n', spec);
        assert!(debug_newline.output_len() > 2);

        // Test Debug for numeric types
        let debug_u8 = Debug(42u8, spec);
        assert_eq!(debug_u8.output_len(), 2);
        let buf_u8: StrBuf<2> = debug_u8.const_eval();
        assert_eq!(buf_u8.as_str(), "42");

        // Test Debug for more integer types
        let debug_i32 = Debug(-5i32, spec);
        assert_eq!(debug_i32.output_len(), 2);
        let buf_i32: StrBuf<2> = debug_i32.const_eval();
        assert_eq!(buf_i32.as_str(), "-5");

        let debug_bool = Debug(true, spec);
        assert_eq!(debug_bool.output_len(), 4);
        let buf_bool: StrBuf<4> = debug_bool.const_eval();
        assert_eq!(buf_bool.as_str(), "true");

        // Test Debug with alternate formatting
        let spec_alt = FmtSpec { alternate: true };
        let debug_alt = Debug(42u8, spec_alt);
        assert_eq!(debug_alt.output_len(), 2);
        let buf_alt: StrBuf<2> = debug_alt.const_eval();
        assert_eq!(buf_alt.as_str(), "42");
    }

    #[test]
    fn test_lower_hex_runtime() {
        let spec = FmtSpec { alternate: false };
        let spec_alt = FmtSpec { alternate: true };

        // Test LowerHex for unsigned - with output_len
        let hex_u8 = LowerHex(255u8, spec);
        assert_eq!(hex_u8.output_len(), 2);
        let buf: StrBuf<2> = hex_u8.const_eval();
        assert_eq!(buf.as_str(), "ff");

        let hex_u8_alt = LowerHex(255u8, spec_alt);
        assert_eq!(hex_u8_alt.output_len(), 4);
        let buf_alt: StrBuf<4> = hex_u8_alt.const_eval();
        assert_eq!(buf_alt.as_str(), "0xff");

        // Test LowerHex for signed - now with const_eval
        let hex_i32 = LowerHex(-1i32, spec);
        assert_eq!(hex_i32.output_len(), 8);
        let buf_i32: StrBuf<8> = hex_i32.const_eval();
        assert_eq!(buf_i32.as_str(), "ffffffff");

        // Test LowerHex for signed with alternate
        let hex_i32_alt = LowerHex(-1i32, spec_alt);
        assert_eq!(hex_i32_alt.output_len(), 10);
        let buf_i32_alt: StrBuf<10> = hex_i32_alt.const_eval();
        assert_eq!(buf_i32_alt.as_str(), "0xffffffff");
    }

    #[test]
    fn test_upper_hex_runtime() {
        let spec = FmtSpec { alternate: false };
        let spec_alt = FmtSpec { alternate: true };

        // Test UpperHex for unsigned
        let hex_u8 = UpperHex(255u8, spec);
        let buf: StrBuf<2> = hex_u8.const_eval();
        assert_eq!(buf.as_str(), "FF");

        let hex_u8_alt = UpperHex(255u8, spec_alt);
        let buf_alt: StrBuf<4> = hex_u8_alt.const_eval();
        assert_eq!(buf_alt.as_str(), "0xFF");

        // Test UpperHex for signed - now with const_eval
        let hex_i32 = UpperHex(-1i32, spec);
        assert_eq!(hex_i32.output_len(), 8);
        let buf_i32: StrBuf<8> = hex_i32.const_eval();
        assert_eq!(buf_i32.as_str(), "FFFFFFFF");

        // Test more integer types
        let hex_u16 = UpperHex(0xABCDu16, spec);
        let buf_u16: StrBuf<4> = hex_u16.const_eval();
        assert_eq!(buf_u16.as_str(), "ABCD");

        let hex_u64 = UpperHex(0x123456u64, spec);
        assert!(hex_u64.output_len() > 0);
    }

    #[test]
    fn test_binary_runtime() {
        let spec = FmtSpec { alternate: false };
        let spec_alt = FmtSpec { alternate: true };

        // Test Binary for unsigned
        let bin_u8 = Binary(5u8, spec);
        let buf: StrBuf<3> = bin_u8.const_eval();
        assert_eq!(buf.as_str(), "101");

        let bin_u8_alt = Binary(5u8, spec_alt);
        let buf_alt: StrBuf<5> = bin_u8_alt.const_eval();
        assert_eq!(buf_alt.as_str(), "0b101");

        // Test Binary for signed - now with const_eval
        let bin_i32 = Binary(-1i32, spec);
        assert_eq!(bin_i32.output_len(), 32);
        let buf_i32: StrBuf<32> = bin_i32.const_eval();
        // -1 in binary is all 1s
        assert_eq!(buf_i32.as_str(), "11111111111111111111111111111111");

        // Test more types
        let bin_u16 = Binary(7u16, spec);
        let buf_u16: StrBuf<3> = bin_u16.const_eval();
        assert_eq!(buf_u16.as_str(), "111");

        let bin_u64 = Binary(15u64, spec);
        let buf_u64: StrBuf<4> = bin_u64.const_eval();
        assert_eq!(buf_u64.as_str(), "1111");

        let bin_u128 = Binary(3u128, spec_alt);
        assert!(bin_u128.output_len() > 0);
    }
}