konst_macro_rules/
string.rs

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
use core::fmt;

// copied from core
// https://github.com/rust-lang/rust/blob/673d0db5e393e9c64897005b470bfeb6d5aec61b/library/core/src/str/validations.rs#L232
const UTF8_CHAR_WIDTH: &[u8; 256] = &[
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x1F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x3F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x5F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x7F
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, // 0x9F
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, // 0xBF
    0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, // 0xDF
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF
    4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF
];

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Utf8Error {
    valid_up_to: usize,
}

impl Utf8Error {
    /// The index up to which a `&str` can be validly constructed from the input `&[u8]`.
    ///
    /// `&input[..error.valid_up_to()]` is valid utf8.
    pub const fn valid_up_to(&self) -> usize {
        self.valid_up_to
    }

    /// For erroring with an error message.
    pub const fn panic(&self) -> ! {
        let offset = self.valid_up_to();
        [/*Could not interpret bytes from offset as a str*/][offset]
    }
}

impl fmt::Display for Utf8Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "invalid utf-8 sequence starting from index {}",
            self.valid_up_to
        )
    }
}

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

pub const fn check_utf8(mut bytes: &[u8]) -> Result<(), Utf8Error> {
    let in_len = bytes.len();

    macro_rules! try_nexts {
        ($rema:ident, [$second:ident $(,$nexts:ident)*], $extra_checks:expr ) => ({
            if let [$second, $($nexts,)* ref rem @ ..] = *$rema  {
                if $( is_continuation_byte($nexts) && )* $extra_checks {
                    bytes = rem;
                } else {
                    return Err(Utf8Error{valid_up_to: in_len - bytes.len()});
                }

            } else {
                return Err(Utf8Error{valid_up_to: in_len - bytes.len()});
            }
        })
    }

    while let [first, ref rema @ ..] = *bytes {
        let utf8len = UTF8_CHAR_WIDTH[first as usize];
        if bytes.len() < utf8len as usize {
            return Err(Utf8Error {
                valid_up_to: in_len - bytes.len(),
            });
        }

        match utf8len {
            1 => {
                bytes = rema;
                continue;
            }
            2 => try_nexts!(rema, [second], is_continuation_byte(second)),
            3 => try_nexts!(
                rema,
                [second, third],
                matches! {
                    (first, second),
                    (0xE0, 0xA0..=0xBF)
                    | (0xE1..=0xEC, 0x80..=0xBF)
                    | (0xED, 0x80..=0x9F)
                    | (0xEE..=0xEF, 0x80..=0xBF)
                }
            ),
            4 => try_nexts!(
                rema,
                [second, third, fourth],
                matches!(
                    (first, second),
                    (0xF0, 0x90..=0xBF) | (0xF1..=0xF3, 0x80..=0xBF) | (0xF4, 0x80..=0x8F)
                )
            ),
            _ => {
                return Err(Utf8Error {
                    valid_up_to: in_len - bytes.len(),
                })
            }
        }
    }
    Ok(())
}

const fn is_continuation_byte(b: u8) -> bool {
    (b & 0b11_000000) == 0b10_000000
}

#[cfg(not(feature = "rust_1_55"))]
#[macro_export]
macro_rules! from_utf8_macro {
    ($slice:expr) => {
        match $slice {
            x => unsafe {
                match $crate::string::check_utf8(x) {
                    $crate::__::Ok(()) => {
                        let ptr = x as *const [$crate::__::u8] as *const $crate::__::str;
                        unsafe { Ok($crate::utils::Dereference { ptr }.reff) }
                    }
                    $crate::__::Err(e) => $crate::__::Err(e),
                }
            },
        }
    };
}

#[cfg(feature = "rust_1_55")]
#[macro_export]
macro_rules! from_utf8_macro {
    ($slice:expr) => {
        $crate::string::from_utf8_fn($slice)
    };
}

#[cfg(feature = "rust_1_55")]
#[inline]
pub const fn from_utf8_fn(slice: &[u8]) -> Result<&str, Utf8Error> {
    match check_utf8(slice) {
        Ok(()) => unsafe { Ok(core::str::from_utf8_unchecked(slice)) },
        Err(e) => Err(e),
    }
}