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
//! Miscellaneous utils.

use core::slice;

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum ClippedStr<'a> {
    Full(&'a [u8]),
    Clipped(&'a [u8]),
}

impl<'a> ClippedStr<'a> {
    /// Returns bytes corresponding to first `char_count` chars in `s`. If `s` contains less chars,
    /// it's returned in full.
    pub const fn new(s: &'a str, mut char_count: usize) -> Self {
        let s_bytes = s.as_bytes();
        let mut pos = 0;
        while pos < s_bytes.len() && char_count > 0 {
            if s_bytes[pos] < 128 {
                pos += 1;
            } else if s_bytes[pos] >> 5 == 0b_110 {
                pos += 2;
            } else if s_bytes[pos] >> 4 == 0b_1110 {
                pos += 3;
            } else if s_bytes[pos] >> 3 == 0b_11110 {
                pos += 4;
            } else {
                unreachable!(); // Invalid UTF-8 encoding
            }
            char_count -= 1;
        }
        assert!(pos <= s_bytes.len(), "Invalid UTF-8 encoding");
        // SAFETY: Slicing a byte slice with length being in bounds is safe.
        let bytes = unsafe { slice::from_raw_parts(s_bytes.as_ptr(), pos) };
        if pos < s_bytes.len() {
            Self::Clipped(bytes)
        } else {
            Self::Full(bytes)
        }
    }
}

/// Counts the number of chars in a string.
pub(crate) const fn count_chars(s: &str) -> usize {
    let s_bytes = s.as_bytes();
    let mut pos = 0;
    let mut char_count = 0;
    while pos < s_bytes.len() {
        if s_bytes[pos] < 128 {
            pos += 1;
        } else if s_bytes[pos] >> 5 == 0b_110 {
            pos += 2;
        } else if s_bytes[pos] >> 4 == 0b_1110 {
            pos += 3;
        } else if s_bytes[pos] >> 3 == 0b_11110 {
            pos += 4;
        } else {
            unreachable!(); // Invalid UTF-8 encoding
        }
        char_count += 1;
    }
    char_count
}

pub(crate) const fn assert_is_ascii(s: &str) {
    const CLIP_LEN: usize = 32;

    let s_bytes = s.as_bytes();
    let mut pos = 0;
    while pos < s_bytes.len() {
        if s_bytes[pos] < 128 {
            pos += 1;
        } else {
            crate::compile_panic!(
                "String '", s => crate::clip(CLIP_LEN, "…"), "' contains non-ASCII chars; \
                 first at position ", pos => crate::fmt::<usize>()
            );
        }
    }
}

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

    #[test]
    fn extracting_first_chars_from_ascii_string() {
        assert_eq!(ClippedStr::new("Test", 1), ClippedStr::Clipped(b"T"));
        assert_eq!(ClippedStr::new("Test", 2), ClippedStr::Clipped(b"Te"));
        assert_eq!(ClippedStr::new("Test", 3), ClippedStr::Clipped(b"Tes"));
        for char_count in [4, 5, 8, 32, 128] {
            assert_eq!(
                ClippedStr::new("Test", char_count),
                ClippedStr::Full(b"Test")
            );
        }
    }

    #[test]
    fn extracting_first_chars_from_utf8_string() {
        assert_eq!(
            ClippedStr::new("💣Test", 1),
            ClippedStr::Clipped("💣".as_bytes())
        );
        assert_eq!(
            ClippedStr::new("💣Test", 2),
            ClippedStr::Clipped("💣T".as_bytes())
        );
        assert_eq!(
            ClippedStr::new("T💣est", 3),
            ClippedStr::Clipped("T💣e".as_bytes())
        );
        assert_eq!(
            ClippedStr::new("T💣eßtℝ", 4),
            ClippedStr::Clipped("T💣eß".as_bytes())
        );
        assert_eq!(
            ClippedStr::new("Tℝ💣eßt", 4),
            ClippedStr::Clipped("Tℝ💣e".as_bytes())
        );
        assert_eq!(
            ClippedStr::new("Tℝ💣eßt", 5),
            ClippedStr::Clipped("Tℝ💣eß".as_bytes())
        );

        for char_count in [6, 8, 32, 128] {
            assert_eq!(
                ClippedStr::new("Tℝ💣eßt", char_count),
                ClippedStr::Full("Tℝ💣eßt".as_bytes())
            );
        }
    }
}