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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#![allow(unused_unsafe)]
use Utf8Char;
use Utf16Char;
use Utf8Iterator;
use Utf16Iterator;
use error::*;
extern crate core;
use self::core::{char, u32, mem};
use self::core::ops::Not;
pub trait U8UtfExt {
fn extra_utf8_bytes(self) -> Result<usize,InvalidUtf8FirstByte>;
fn extra_utf8_bytes_unchecked(self) -> usize;
}
impl U8UtfExt for u8 {
fn extra_utf8_bytes(self) -> Result<usize,InvalidUtf8FirstByte> {
use error::InvalidUtf8FirstByte::{ContinuationByte,TooLongSeqence};
match self.not().leading_zeros() {
1 => Err(ContinuationByte),
5...255 => Err(TooLongSeqence),
0 => Ok(0),
n => Ok(n as usize-1),
}
}
fn extra_utf8_bytes_unchecked(self) -> usize {
(self.not().leading_zeros()as usize).saturating_sub(1)
}
}
pub trait U16UtfExt {
fn utf16_needs_extra_unit(self) -> Result<bool,InvalidUtf16FirstUnit>;
fn is_utf16_leading_surrogate(self) -> bool;
}
impl U16UtfExt for u16 {
fn utf16_needs_extra_unit(self) -> Result<bool,InvalidUtf16FirstUnit> {match self {
0x00_00...0xd7_ff => Ok(false),
0xe0_00...0xff_ff => Ok(false),
0xd8_00...0xdb_ff => Ok(true),
0xdc_00...0xdf_ff => Err(InvalidUtf16FirstUnit),
_ => unreachable!()
}}
fn is_utf16_leading_surrogate(self) -> bool {
(self & 0xfc00) == 0xd800
}
}
pub trait CharExt: Sized {
fn to_utf8(self) -> Utf8Char;
fn to_utf16(self) -> Utf16Char;
fn iter_utf8_bytes(self) -> Utf8Iterator;
fn iter_utf16_units(self) -> Utf16Iterator;
fn to_utf8_array(self) -> ([u8; 4], usize);
fn to_utf16_tuple(self) -> (u16, Option<u16>);
fn from_utf8_slice_start(src: &[u8]) -> Result<(Self,usize),InvalidUtf8Slice>;
fn from_utf16_slice_start(src: &[u16]) -> Result<(Self,usize), InvalidUtf16Slice>;
fn from_utf8_array(utf8: [u8; 4]) -> Result<Self,InvalidUtf8Array>;
fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, InvalidUtf16Tuple>;
unsafe fn from_utf8_exact_slice_unchecked(src: &[u8]) -> Self;
unsafe fn from_utf16_tuple_unchecked(utf16: (u16, Option<u16>)) -> Self;
fn from_u32_detailed(c: u32) -> Result<Self,InvalidCodepoint>;
}
impl CharExt for char {
fn to_utf8(self) -> Utf8Char {
self.into()
}
fn iter_utf8_bytes(self) -> Utf8Iterator {
self.to_utf8().into_iter()
}
fn to_utf8_array(self) -> ([u8; 4], usize) {
let len = self.len_utf8();
let mut c = self as u32;
if len == 1 {
([c as u8, 0, 0, 0], 1)
} else {
let mut parts = 0;
parts |= c & 0x3f; c>>=6;
parts<<=8; parts |= c & 0x3f; c>>=6;
parts<<=8; parts |= c & 0x3f; c>>=6;
parts<<=8; parts |= c & 0x3f;
parts |= 0x80_80_80_80;
parts >>= 8*(4-len);
parts |= (0xff_00u32 >> len) & 0xff;
parts &= Not::not(1u32 << 7-len);
let bytes: [u8; 4] = unsafe{ mem::transmute(u32::from_le(parts)) };
(bytes, len)
}
}
fn from_utf8_slice_start(src: &[u8]) -> Result<(Self,usize),InvalidUtf8Slice> {
use errors::InvalidUtf8::*;
use errors::InvalidUtf8Slice::*;
let first = match src.first() {
Some(first) => *first,
None => return Err(TooShort(1)),
};
let bytes = match first.extra_utf8_bytes() {
Err(e) => return Err(Utf8(FirstByte(e))),
Ok(0) => return Ok((first as char, 1)),
Ok(extra) if extra >= src.len()
=> return Err(TooShort(extra+1)),
Ok(extra) => &src[..extra+1],
};
if let Some(i) = bytes.iter().skip(1).position(|&b| (b >> 6) != 0b10 ) {
Err(Utf8(NotAContinuationByte(i+1)))
} else if overlong(bytes[0], bytes[1]) {
Err(Utf8(OverLong))
} else {
let c = unsafe{ char::from_utf8_exact_slice_unchecked(bytes) };
match char::from_u32_detailed(c as u32) {
Ok(c) => Ok((c, bytes.len())),
Err(e) => Err(Codepoint(e)),
}
}
}
fn from_utf8_array(utf8: [u8; 4]) -> Result<Self,InvalidUtf8Array> {
use errors::InvalidUtf8::*;
use errors::InvalidUtf8Array::*;
let src = match utf8[0].extra_utf8_bytes() {
Err(error) => return Err(Utf8(FirstByte(error))),
Ok(0) => return Ok(utf8[0] as char),
Ok(extra) => &utf8[..extra+1],
};
if let Some(i) = src[1..].iter().position(|&b| (b >> 6) != 0b10 ) {
Err(Utf8(NotAContinuationByte(i+1)))
} else if overlong(utf8[0], utf8[1]) {
Err(Utf8(OverLong))
} else {
let c = unsafe{ char::from_utf8_exact_slice_unchecked(src) };
char::from_u32_detailed(c as u32)
.map_err(|e| Codepoint(e) )
}
}
unsafe fn from_utf8_exact_slice_unchecked(src: &[u8]) -> Self {
if src.len() == 1 {
src[0] as char
} else {
let mut c = src[0] as u32 & (0xff >> 2+src.len()-1);
for b in &src[1..] {
c = (c << 6) | (b & 0b00111111) as u32;
}
unsafe{ char::from_u32_unchecked(c) }
}
}
fn to_utf16(self) -> Utf16Char {
Utf16Char::from(self)
}
fn iter_utf16_units(self) -> Utf16Iterator {
self.to_utf16().into_iter()
}
fn to_utf16_tuple(self) -> (u16, Option<u16>) {
let c = self as u32;
if c <= 0x_ff_ff {
(c as u16, None)
} else {
let c = c - 0x_01_00_00;
let high = 0x_d8_00 + (c >> 10);
let low = 0x_dc_00 + (c & 0x_03_ff);
(high as u16, Some(low as u16))
}
}
fn from_utf16_slice_start(src: &[u16]) -> Result<(Self,usize), InvalidUtf16Slice> {
use errors::InvalidUtf16Slice::*;
unsafe {match (src.get(0), src.get(1)) {
(Some(&u @ 0x00_00...0xd7_ff), _) |
(Some(&u @ 0xe0_00...0xff_ff), _)
=> Ok((char::from_u32_unchecked(u as u32), 1)),
(Some(&0xdc_00...0xdf_ff), _) => Err(FirstLowSurrogate),
(None, _) => Err(EmptySlice),
(Some(&f @ 0xd8_00...0xdb_ff), Some(&s @ 0xdc_00...0xdf_ff))
=> Ok((char::from_utf16_tuple_unchecked((f, Some(s))), 2)),
(Some(&0xd8_00...0xdb_ff), Some(_)) => Err(SecondNotLowSurrogate),
(Some(&0xd8_00...0xdb_ff), None) => Err(MissingSecond),
(Some(_), _) => unreachable!()
}}
}
fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, InvalidUtf16Tuple> {
use errors::InvalidUtf16Tuple::*;
unsafe{ match utf16 {
(0x00_00...0xd7_ff, None) |
(0xe0_00...0xff_ff, None) |
(0xd8_00...0xdb_ff, Some(0xdc_00...0xdf_ff))
=> Ok(char::from_utf16_tuple_unchecked(utf16)),
(0xd8_00...0xdb_ff, Some(_)) => Err(InvalidSecond),
(0xd8_00...0xdb_ff, None ) => Err(MissingSecond),
(0xdc_00...0xdf_ff, _ ) => Err(FirstIsTrailingSurrogate),
( _ , Some(_)) => Err(SuperfluousSecond),
( _ , None ) => unreachable!()
}}
}
unsafe fn from_utf16_tuple_unchecked(utf16: (u16, Option<u16>)) -> Self {
match utf16.1 {
Some(second) => combine_surrogates(utf16.0, second),
None => char::from_u32_unchecked(utf16.0 as u32)
}
}
fn from_u32_detailed(c: u32) -> Result<Self,InvalidCodepoint> {
use errors::InvalidCodepoint::*;
unsafe{ match c {
0x00_00_00...0x00_d7_ff => Ok(char::from_u32_unchecked(c)),
0x00_d8_00...0x00_df_ff => Err(Utf16Reserved),
0x00_e0_00...0x10_ff_ff => Ok(char::from_u32_unchecked(c)),
0x11_00_00...u32::MAX => Err(TooHigh),
_ => unreachable!()
}}
}
}
fn overlong(first: u8, second: u8) -> bool {
let both = ((first as u16) << 8) | (second << 2) as u16;
let both = both << 1+both.not().leading_zeros();
both.leading_zeros() >= 5
}
unsafe fn combine_surrogates(first: u16, second: u16) -> char {
let high = (first & 0x_03_ff) as u32;
let low = (second & 0x_03_ff) as u32;
let c = ((high << 10) | low) + 0x_01_00_00;
char::from_u32_unchecked(c)
}