devela/text/char/impls/
c8.rs
1use super::*;
4#[cfg(feature = "ascii")]
5use crate::AsciiChar;
6use crate::{Char, DataOverflow};
7
8impl char8 {
9 #[must_use]
14 const fn from_char_unchecked(c: char) -> char8 {
15 char8(c as u32 as u8)
16 }
17
18 pub const MIN: char8 = char8(0x00);
22
23 pub const MAX: char8 = char8(0xFF);
25
26 #[must_use]
30 #[cfg(feature = "ascii")]
31 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "ascii")))]
32 pub const fn from_ascii_char(c: AsciiChar) -> char8 {
33 char8(c as u8)
34 }
35
36 #[must_use]
38 #[cfg(feature = "_char7")]
39 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char7")))]
40 pub const fn from_char7(c: char7) -> char8 {
41 char8(c.0.get())
42 }
43 #[cfg(feature = "_char16")]
48 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
49 pub const fn try_from_char16(c: char16) -> Result<char8, DataOverflow> {
50 if Char::byte_len(c.to_u32()) == 1 {
51 Ok(char8(c.to_u32() as u8))
52 } else {
53 Err(DataOverflow(Some(c.to_u32() as usize)))
54 }
55 }
56 pub const fn try_from_char(c: char) -> Result<char8, DataOverflow> {
61 if Char::byte_len(c as u32) == 1 {
62 Ok(char8(c as u32 as u8))
63 } else {
64 Err(DataOverflow(Some(c as u32 as usize)))
65 }
66 }
67
68 #[cfg(feature = "ascii")]
78 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "ascii")))]
79 pub const fn try_to_ascii_char(self) -> Result<AsciiChar, DataOverflow> {
80 if Char::is_7bit(self.to_u32()) {
81 #[cfg(any(feature = "safe_text", not(feature = "unsafe_str")))]
82 if let Some(c) = AsciiChar::from_u8(self.0) {
83 return Ok(c);
84 } else {
85 unreachable![]
86 }
87
88 #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
89 return Ok(unsafe { AsciiChar::from_u8_unchecked(self.0) });
91 }
92 Err(DataOverflow(Some(self.to_u32() as usize)))
93 }
94
95 #[cfg(feature = "_char7")]
100 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char7")))]
101 pub const fn try_to_char7(self) -> Result<char7, DataOverflow> {
102 char7::try_from_char8(self)
103 }
104 #[must_use]
106 #[cfg(feature = "_char16")]
107 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
108 pub const fn to_char16(self) -> char16 {
109 char16::from_char8(self)
110 }
111 #[must_use]
113 pub const fn to_char(self) -> char {
114 self.0 as char
115 }
116 #[must_use]
118 pub const fn to_u32(self) -> u32 {
119 self.0 as u32
120 }
121
122 #[must_use]
129 #[allow(clippy::unusual_byte_groupings, clippy::single_match_else)]
130 pub const fn to_utf8_bytes(self) -> [u8; 2] {
131 let c = self.0;
132 match c {
133 0x0000..=0x007F => [c, 0],
136
137 0x0080.. => {
141 let y = 0b10_000000 | (0b0011_1111 & c);
142 let x = 0b110_00000 | (c >> 6);
143 [x, y]
144 }
145 }
146 }
147
148 #[must_use]
156 pub const fn is_noncharacter(self) -> bool {
157 Char::is_noncharacter(self.0 as u32)
158 }
159
160 #[must_use]
164 pub const fn is_character(self) -> bool {
165 !self.is_noncharacter()
166 }
167
168 #[must_use]
170 pub const fn is_ascii(self) -> bool {
171 self.0 <= 0x7F
172 }
173
174 #[must_use]
179 pub const fn to_ascii_uppercase(self) -> char8 {
180 Self::from_char_unchecked(char::to_ascii_uppercase(&self.to_char()))
181 }
182
183 #[must_use]
188 pub const fn to_ascii_lowercase(self) -> char8 {
189 Self::from_char_unchecked(char::to_ascii_lowercase(&self.to_char()))
190 }
191}
192
193#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char8")))]
194#[cfg(all(not(feature = "safe_text"), feature = "unsafe_layout"))]
195unsafe impl crate::MemPod for char8 {}