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
use core::fmt;
use core::str::{self, Utf8Error};
#[cfg(feature = "std")]
use std::ffi::{CStr, OsStr};
#[cfg(feature = "std")]
use std::path::Path;
use c_char;
use error::Error;
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CUtf8(str);
#[cfg(feature = "try_from")]
mod try_from {
use super::*;
use core::convert::TryFrom;
impl<'a> TryFrom<&'a [u8]> for &'a CUtf8 {
type Error = Error;
#[inline]
fn try_from(bytes: &[u8]) -> Result<&CUtf8, Self::Error> {
CUtf8::from_bytes(bytes)
}
}
#[cfg(feature = "std")]
impl<'a> TryFrom<&'a CStr> for &'a CUtf8 {
type Error = Utf8Error;
#[inline]
fn try_from(c: &CStr) -> Result<&CUtf8, Self::Error> {
CUtf8::from_c_str(c)
}
}
impl<'a> TryFrom<&'a str> for &'a CUtf8 {
type Error = Error;
#[inline]
fn try_from(s: &str) -> Result<&CUtf8, Self::Error> {
CUtf8::from_str(s)
}
}
}
impl AsRef<str> for CUtf8 {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}
#[cfg(feature = "std")]
impl AsRef<CStr> for CUtf8 {
#[inline]
fn as_ref(&self) -> &CStr {
self.as_c_str()
}
}
impl AsRef<[u8]> for CUtf8 {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
#[cfg(feature = "std")]
impl AsRef<Path> for CUtf8 {
#[inline]
fn as_ref(&self) -> &Path {
self.as_str().as_ref()
}
}
#[cfg(feature = "std")]
impl AsRef<OsStr> for CUtf8 {
#[inline]
fn as_ref(&self) -> &OsStr {
self.as_str().as_ref()
}
}
impl fmt::Debug for CUtf8 {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str_with_nul().fmt(f)
}
}
impl fmt::Display for CUtf8 {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl<'a> Default for &'a CUtf8 {
#[inline]
fn default() -> &'a CUtf8 { CUtf8::EMPTY }
}
const EMPTY: &CUtf8 = c_utf8!("");
impl CUtf8 {
pub const EMPTY: &'static CUtf8 = EMPTY;
#[inline]
pub fn from_bytes(bytes: &[u8]) -> Result<&CUtf8, Error> {
CUtf8::from_str(str::from_utf8(bytes)?)
}
#[inline]
pub fn from_str(s: &str) -> Result<&CUtf8, Error> {
if ::is_nul_terminated(s) {
unsafe { Ok(CUtf8::from_str_unchecked(s)) }
} else {
Err(Error::Nul)
}
}
#[cfg(feature = "std")]
#[inline]
pub fn from_c_str(c: &CStr) -> Result<&CUtf8, Utf8Error> {
let s = str::from_utf8(c.to_bytes_with_nul())?;
unsafe { Ok(CUtf8::from_str_unchecked(s)) }
}
#[inline]
pub unsafe fn from_ptr<'a>(raw: *const c_char) -> Result<&'a CUtf8, Utf8Error> {
#[cfg(feature = "std")] {
CUtf8::from_c_str(CStr::from_ptr(raw))
}
#[cfg(not(feature = "std"))] {
use core::slice;
extern {
fn strlen(cs: *const c_char) -> usize;
}
let n = strlen(raw) + 1;
let s = str::from_utf8(slice::from_raw_parts(raw as *const u8, n))?;
Ok(CUtf8::from_str_unchecked(s))
}
}
#[inline]
pub fn len(&self) -> usize {
self.0.len().wrapping_sub(1)
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.len() == 1
}
#[inline]
pub unsafe fn from_bytes_unchecked(b: &[u8]) -> &CUtf8 {
&*(b as *const [u8] as *const CUtf8)
}
#[inline]
pub unsafe fn from_str_unchecked(s: &str) -> &CUtf8 {
&*(s as *const str as *const CUtf8)
}
#[inline]
pub unsafe fn from_str_unchecked_mut(s: &mut str) -> &mut CUtf8 {
&mut *(s as *mut str as *mut CUtf8)
}
#[cfg(feature = "std")]
#[inline]
pub unsafe fn from_c_str_unchecked(c: &CStr) -> &CUtf8 {
Self::from_bytes_unchecked(c.to_bytes_with_nul())
}
#[inline]
pub fn as_ptr(&self) -> *const c_char {
self.as_bytes().as_ptr() as *const c_char
}
#[cfg(feature = "std")]
#[inline]
pub fn as_c_str(&self) -> &CStr {
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
}
#[inline]
pub fn as_str(&self) -> &str {
let len = self.0.len().saturating_sub(1);
unsafe { self.0.get_unchecked(..len) }
}
#[inline]
pub fn as_str_with_nul(&self) -> &str {
&self.0
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
self.as_str().as_bytes()
}
#[inline]
pub fn as_bytes_with_nul(&self) -> &[u8] {
self.as_str_with_nul().as_bytes()
}
}
#[cfg(all(test, nightly))]
mod benches {
use super::*;
use test::{Bencher, black_box};
#[bench]
fn from_bytes(b: &mut Bencher) {
let s = "abcdéfghîjklmnöpqrstúvwxÿz\0";
b.iter(|| {
let s = black_box(s.as_bytes());
black_box(CUtf8::from_bytes(s).unwrap());
});
}
}