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};
pub struct CStr(crate::SSlice<u8, Null>);
impl CStr {
#[inline(always)]
pub const unsafe fn from_ptr<'a>(p: *const u8) -> &'a Self {
unsafe { &*(p as *const Self) }
}
#[inline(always)]
pub unsafe fn from_mut_ptr<'a>(p: *mut u8) -> &'a mut Self {
unsafe { &mut *(p as *mut Self) }
}
#[inline(always)]
pub const unsafe fn from_sslice_unchecked(sslice: &SSlice<u8, Null>) -> &Self {
unsafe { &*(sslice as *const _ as *const Self) }
}
#[inline(always)]
pub unsafe fn from_sslice_unchecked_mut(sslice: &mut SSlice<u8, Null>) -> &mut Self {
unsafe { &mut *(sslice as *mut _ as *mut Self) }
}
#[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),
}
}
#[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),
}
}
#[inline(always)]
pub fn as_sslice(&self) -> &SSlice<u8, Null> {
&self.0
}
#[inline(always)]
pub fn as_sslice_mut(&mut self) -> &mut SSlice<u8, Null> {
&mut self.0
}
#[inline(always)]
pub fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(self.0.as_slice()) }
}
#[inline(always)]
pub fn as_str_mut(&mut self) -> &mut str {
unsafe { core::str::from_utf8_unchecked_mut(self.0.as_slice_mut()) }
}
#[inline(always)]
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
#[inline(always)]
pub fn as_mut_ptr(&mut self) -> *mut u8 {
self.0.as_mut_ptr()
}
#[inline(always)]
pub fn as_bytes(&self) -> &SSlice<u8, Null> {
&self.0
}
#[inline(always)]
pub unsafe fn as_bytes_mut(&mut self) -> &mut SSlice<u8, Null> {
&mut self.0
}
#[inline(always)]
pub fn bytes(&self) -> &Iter<u8, Null> {
self.0.iter()
}
#[inline(always)]
pub unsafe fn bytes_mut(&mut self) -> &mut Iter<u8, Null> {
self.0.iter_mut()
}
#[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()
}
}
#[repr(transparent)]
pub struct Chars(CStr);
impl Chars {
#[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();
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 {
return Some(unsafe { char::from_u32_unchecked(code_point) });
} else {
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 {}