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

use crate::{Iter, Null, SSlice, Utf8Error};

/// A null-terminated UTF-8 string.
///
/// This type is designed to be used as a regular string slice (a.k.a. `str`), but null-terminated.
pub struct CStr(crate::SSlice<u8, Null>);

impl CStr {
    /// Creates a new [`CStr`] from the provided pointer.
    ///
    /// # Safety
    ///
    /// `p` must reference valid UTF-8 data until the first null byte. Those bytes must remain
    /// shared for the lifetime `'a`.
    #[inline(always)]
    pub const unsafe fn from_ptr<'a>(p: *const u8) -> &'a Self {
        unsafe { &*(p as *const Self) }
    }

    /// Creates a new [`CStr`] from the provided pointer.
    ///
    /// # Safety
    ///
    /// `p` must reference value UTF-8 data until the first null byte. Those bytes must remain
    /// exclusive to the produced instance for the lifetime `'a`.
    #[inline(always)]
    pub unsafe fn from_mut_ptr<'a>(p: *mut u8) -> &'a mut Self {
        unsafe { &mut *(p as *mut Self) }
    }

    /// Creates a new [`CStr`] from the provided `SSlice<u8>`.
    ///
    /// # Safety
    ///
    /// The provided slice must reference valid `UTF-8` data.
    #[inline(always)]
    pub const unsafe fn from_sslice_unchecked(sslice: &SSlice<u8, Null>) -> &Self {
        unsafe { &*(sslice as *const _ as *const Self) }
    }

    /// Creates a new [`CStr`] from the provided `SSlice<u8>`.
    ///
    /// # Safety
    ///
    /// The provided slice must reference valid `UTF-8` data.
    #[inline(always)]
    pub unsafe fn from_sslice_unchecked_mut(sslice: &mut SSlice<u8, Null>) -> &mut Self {
        unsafe { &mut *(sslice as *mut _ as *mut Self) }
    }

    /// Creates a new [`CStr`] from the provided `SSlice<u8>`.
    #[inline(always)]
    pub fn from_sslice(sslice: &SSlice<u8, Null>) -> Result<&Self, Utf8Error> {
        match crate::utf8::verify(sslice) {
            Ok(_) => unsafe { Ok(Self::from_sslice_unchecked(sslice)) },
            Err(err) => Err(err),
        }
    }

    /// Creates a new [`CStr`] from the provided `SSlice<u8>`.
    #[inline(always)]
    pub fn from_sslice_mut(sslice: &mut SSlice<u8, Null>) -> Result<&mut Self, Utf8Error> {
        match crate::utf8::verify(sslice) {
            Ok(_) => unsafe { Ok(Self::from_sslice_unchecked_mut(sslice)) },
            Err(err) => Err(err),
        }
    }

    /// Returns the inner [`SSlice<u8, Null>`].
    #[inline(always)]
    pub fn as_sslice(&self) -> &SSlice<u8, Null> {
        &self.0
    }

    /// Returns the inner [`SSlice<u8, Null>`].
    #[inline(always)]
    pub fn as_sslice_mut(&mut self) -> &mut SSlice<u8, Null> {
        &mut self.0
    }

    /// Turns this [`CStr`] into an [`str`].
    #[inline(always)]
    pub fn as_str(&self) -> &str {
        unsafe { core::str::from_utf8_unchecked(self.0.as_slice()) }
    }

    /// Turns this [`CStr`] into an [`str`].
    #[inline(always)]
    pub fn as_str_mut(&mut self) -> &mut str {
        unsafe { core::str::from_utf8_unchecked_mut(self.0.as_slice_mut()) }
    }

    /// Returns a raw pointer to the first byte of the string.
    #[inline(always)]
    pub fn as_ptr(&self) -> *const u8 {
        self.0.as_ptr()
    }

    /// Returns a raw pointer to the first byte of the string.
    ///
    /// Keep in mind that the [`CStr`] must remain valid UTF-8.
    #[inline(always)]
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.0.as_mut_ptr()
    }

    /// Returns a reference to the underlying [`SSlice<u8, Null>`] instance.
    #[inline(always)]
    pub fn as_bytes(&self) -> &SSlice<u8, Null> {
        &self.0
    }

    /// Returns a reference to the underlying [`SSlice<u8, Null>`] instance.
    ///
    /// # Safety
    ///
    /// The bytes owned by the [`CStr`] must remain valid UTF-8.
    #[inline(always)]
    pub unsafe fn as_bytes_mut(&mut self) -> &mut SSlice<u8, Null> {
        &mut self.0
    }

    /// Returns an iterator over the bytes of the [`CStr`].
    #[inline(always)]
    pub fn bytes(&self) -> &Iter<u8, Null> {
        self.0.iter()
    }

    /// Returns an iterator over the bytes of the [`CStr`].
    ///
    /// # Safety
    ///
    /// The bytes owned by the [`CStr`] must remain valid UTF-8.
    #[inline(always)]
    pub unsafe fn bytes_mut(&mut self) -> &mut Iter<u8, Null> {
        self.0.iter_mut()
    }

    /// Returns an iterator over the characters of this [`CStr`].
    #[inline(always)]
    pub fn chars(&self) -> &Chars {
        unsafe { &*(self as *const CStr as *const Chars) }
    }
}

impl AsRef<SSlice<u8, Null>> for CStr {
    #[inline(always)]
    fn as_ref(&self) -> &SSlice<u8, Null> {
        self.as_bytes()
    }
}

/// An iterator over the characters of a [`CStr`].
#[repr(transparent)]
pub struct Chars(CStr);

impl Chars {
    /// Returns a [`CStr`] owning the remaining characters of this iterator.
    #[inline(always)]
    pub fn remainder(&self) -> &CStr {
        &self.0
    }
}

impl<'a> Iterator for &'a Chars {
    type Item = char;

    fn next(&mut self) -> Option<char> {
        let mut state = 0;
        let mut code_point = 0;
        let mut b;

        unsafe {
            b = *self.0.as_ptr();

            // We have to check whether we are at the end of the string.
            if b == 0 {
                return None;
            }

            *self = &*(self.0.as_ptr().add(1) as *const Chars);
        }

        loop {
            crate::utf8::decode(&mut state, &mut code_point, b);

            if state == 0 {
                // Safety:
                //  `decode` is known to produce valid code points.
                return Some(unsafe { char::from_u32_unchecked(code_point) });
            } else {
                // Safety:
                //  We know our input is valid UTF-8. This *must* be a continuation character, and
                //  cannot be the end of the string.
                unsafe {
                    b = *self.0.as_ptr();
                    *self = &*(self.0.as_ptr().add(1) as *const Chars);
                }
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();
        (len, Some(len))
    }
}

impl<'a> ExactSizeIterator for &'a Chars {
    fn len(&self) -> usize {
        let mut count = 0;
        let mut state = 0;
        let mut code_point = 0;

        for &byte in self.0.bytes() {
            crate::utf8::decode(&mut state, &mut code_point, byte);

            if state == 0 {
                count += 1;
            }
        }

        count
    }
}

impl fmt::Debug for CStr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self.as_str(), f)
    }
}

impl fmt::Display for CStr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self.as_str(), f)
    }
}

impl PartialEq<str> for CStr {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.0.iter().copied().eq(other.bytes())
    }
}

impl PartialEq<CStr> for str {
    #[inline]
    fn eq(&self, other: &CStr) -> bool {
        self.bytes().eq(other.0.iter().copied())
    }
}

impl PartialEq for CStr {
    #[inline]
    fn eq(&self, other: &CStr) -> bool {
        self.0.iter().eq(other.0.iter())
    }
}

impl Eq for CStr {}