devela/text/char/impls/
c7.rs
1use super::*;
4#[cfg(feature = "ascii")]
5use crate::AsciiChar;
6use crate::{Char, DataOverflow, NonExtremeU8};
7
8impl char7 {
9 #[must_use]
14 const fn from_char_unchecked(c: char) -> char7 {
15 char7::new_unchecked(c as u32 as u8)
16 }
17
18 #[must_use]
21 const fn new_unchecked(value: u8) -> char7 {
22 #[cfg(any(feature = "safe_text", not(feature = "unsafe_niche")))]
23 if let Some(c) = NonExtremeU8::new(value) {
24 char7(c)
25 } else {
26 unreachable![]
27 }
28 #[cfg(all(not(feature = "safe_text"), feature = "unsafe_niche"))]
29 unsafe {
30 char7(NonExtremeU8::new_unchecked(value))
31 }
32 }
33
34 pub const MIN: char7 = char7::new_unchecked(0x00);
38
39 pub const MAX: char7 = char7::new_unchecked(0x7F);
41
42 #[must_use]
46 #[cfg(feature = "ascii")]
47 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "ascii")))]
48 pub const fn from_ascii_char(c: AsciiChar) -> char7 {
49 char7::new_unchecked(c as u8)
50 }
51
52 #[cfg(feature = "_char8")]
57 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char8")))]
58 pub const fn try_from_char8(c: char8) -> Result<char7, DataOverflow> {
59 if Char::is_7bit(c.to_u32()) {
60 Ok(char7::new_unchecked(c.to_u32() as u8))
61 } else {
62 Err(DataOverflow(Some(c.to_u32() as usize)))
63 }
64 }
65 #[cfg(feature = "_char16")]
70 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
71 pub const fn try_from_char16(c: char16) -> Result<char7, DataOverflow> {
72 if Char::is_7bit(c.to_u32()) {
73 Ok(char7::new_unchecked(c.to_u32() as u8))
74 } else {
75 Err(DataOverflow(Some(c.to_u32() as usize)))
76 }
77 }
78 pub const fn try_from_char(c: char) -> Result<char7, DataOverflow> {
83 if Char::is_7bit(c as u32) {
84 Ok(char7::new_unchecked(c as u32 as u8))
85 } else {
86 Err(DataOverflow(Some(c as u32 as usize)))
87 }
88 }
89
90 #[must_use]
94 #[cfg(feature = "ascii")]
95 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "ascii")))]
96 pub const fn to_ascii_char(c: char7) -> AsciiChar {
97 #[cfg(any(feature = "safe_text", not(feature = "unsafe_niche")))]
98 return if let Some(c) = AsciiChar::from_u8(c.0.get()) { c } else { unreachable!() };
99
100 #[cfg(all(not(feature = "safe_text"), feature = "unsafe_niche"))]
101 unsafe {
102 AsciiChar::from_u8_unchecked(c.0.get())
103 }
104 }
105
106 #[must_use]
108 #[cfg(feature = "_char8")]
109 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char8")))]
110 pub const fn to_char8(self) -> char8 {
111 char8::from_char7(self)
112 }
113 #[must_use]
115 #[cfg(feature = "_char16")]
116 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
117 pub const fn to_char16(self) -> char16 {
118 char16::from_char7(self)
119 }
120 #[must_use]
122 pub const fn to_char(self) -> char {
123 self.0.get() as char
124 }
125 #[must_use]
127 pub const fn to_u32(self) -> u32 {
128 self.0.get() as u32
129 }
130
131 #[must_use]
135 #[allow(clippy::unusual_byte_groupings)]
136 pub const fn to_utf8_bytes(self) -> [u8; 1] {
137 [self.0.get()]
140 }
141
142 #[must_use]
150 pub const fn is_noncharacter(self) -> bool {
151 false
152 }
153
154 #[must_use]
158 pub const fn is_character(self) -> bool {
159 true
160 }
161
162 #[must_use]
164 pub const fn is_ascii(self) -> bool {
165 true
166 }
167
168 #[must_use]
173 pub const fn to_ascii_uppercase(self) -> char7 {
174 Self::from_char_unchecked(char::to_ascii_uppercase(&self.to_char()))
175 }
176
177 #[must_use]
182 pub const fn to_ascii_lowercase(self) -> char7 {
183 Self::from_char_unchecked(char::to_ascii_lowercase(&self.to_char()))
184 }
185}