sequin/sequential/
char.rs1use super::Sequential;
2
3#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
4mod utf8 {
5 use super::Sequential;
6
7 mod surrogate_code_point {
8 pub const START: u32 = 0xD800;
9 pub const END: u32 = 0xE000;
10 pub const CARDINALITY: u32 = END - START;
11 }
12
13 const CHAR_MAX: u32 = char::MAX as _;
14
15 unsafe impl Sequential for char {
16 const CARDINALITY: usize = (CHAR_MAX - surrogate_code_point::CARDINALITY + 1) as _;
17
18 fn to_index(&self) -> usize {
19 let code_point = *self as u32;
20
21 let index = match code_point {
22 ..surrogate_code_point::START => code_point,
23 _ => code_point - surrogate_code_point::END,
24 };
25
26 index as usize
27 }
28
29 unsafe fn from_index_unchecked(index: usize) -> Self {
30 let index = index as u32;
31
32 let code_point = match index {
33 ..surrogate_code_point::START => index,
34 _ => index + surrogate_code_point::END,
35 };
36
37 unsafe { Self::from_u32_unchecked(code_point) }
38 }
39 }
40}
41
42#[cfg(feature = "ascii_char")]
43mod ascii {
44 use std::ascii;
45
46 use super::Sequential;
47
48 unsafe impl Sequential for ascii::Char {
49 const CARDINALITY: usize = 128;
50
51 fn to_index(&self) -> usize {
52 self.to_u8() as usize
53 }
54
55 unsafe fn from_index_unchecked(index: usize) -> Self {
56 unsafe { Self::from_u8_unchecked(index as u8) }
57 }
58 }
59}