#[cfg(feature = "unsafe_str")]
use crate::transmute;
use ::core::fmt;
#[doc = crate::_tags!(text)]
#[doc = crate::_doc_location!("text/char")]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(u8)]
pub enum CharAscii {
#[default]
Null = 0,
StartOfHeading = 1,
StartOfText = 2,
EndOfText = 3,
EndOfTransmission = 4,
Enquiry = 5,
Acknowledge = 6,
Bell = 7,
Backspace = 8,
AsciiCharacterTabulation = 9,
LineFeed = 10,
LineTabulation = 11,
FormFeed = 12,
CarriageReturn = 13,
ShiftOut = 14,
ShiftIn = 15,
DataLinkEscape = 16,
DeviceControlOne = 17,
DeviceControlTwo = 18,
DeviceControlThree = 19,
DeviceControlFour = 20,
NegativeAcknowledge = 21,
SynchronousIdle = 22,
EndOfTransmissionBlock = 23,
Cancel = 24,
EndOfMedium = 25,
Substitute = 26,
Escape = 27,
InformationSeparatorFour = 28,
InformationSeparatorThree = 29,
InformationSeparatorTwo = 30,
InformationSeparatorOne = 31,
Space = 32,
ExclamationMark = 33,
QuotationMark = 34,
NumberSign = 35,
DollarSign = 36,
PercentSign = 37,
Ampersand = 38,
Apostrophe = 39,
LeftParenthesis = 40,
RightParenthesis = 41,
Asterisk = 42,
PlusSign = 43,
Comma = 44,
HyphenMinus = 45,
FullStop = 46,
Solidus = 47,
Digit0 = 48,
Digit1 = 49,
Digit2 = 50,
Digit3 = 51,
Digit4 = 52,
Digit5 = 53,
Digit6 = 54,
Digit7 = 55,
Digit8 = 56,
Digit9 = 57,
Colon = 58,
Semicolon = 59,
LessThanSign = 60,
EqualsSign = 61,
GreaterThanSign = 62,
QuestionMark = 63,
CommercialAt = 64,
CapitalA = 65,
CapitalB = 66,
CapitalC = 67,
CapitalD = 68,
CapitalE = 69,
CapitalF = 70,
CapitalG = 71,
CapitalH = 72,
CapitalI = 73,
CapitalJ = 74,
CapitalK = 75,
CapitalL = 76,
CapitalM = 77,
CapitalN = 78,
CapitalO = 79,
CapitalP = 80,
CapitalQ = 81,
CapitalR = 82,
CapitalS = 83,
CapitalT = 84,
CapitalU = 85,
CapitalV = 86,
CapitalW = 87,
CapitalX = 88,
CapitalY = 89,
CapitalZ = 90,
LeftSquareBracket = 91,
ReverseSolidus = 92,
RightSquareBracket = 93,
CircumflexAccent = 94,
LowLine = 95,
GraveAccent = 96,
SmallA = 97,
SmallB = 98,
SmallC = 99,
SmallD = 100,
SmallE = 101,
SmallF = 102,
SmallG = 103,
SmallH = 104,
SmallI = 105,
SmallJ = 106,
SmallK = 107,
SmallL = 108,
SmallM = 109,
SmallN = 110,
SmallO = 111,
SmallP = 112,
SmallQ = 113,
SmallR = 114,
SmallS = 115,
SmallT = 116,
SmallU = 117,
SmallV = 118,
SmallW = 119,
SmallX = 120,
SmallY = 121,
SmallZ = 122,
LeftCurlyBracket = 123,
VerticalLine = 124,
RightCurlyBracket = 125,
Tilde = 126,
Delete = 127,
}
crate::_impl_init![ConstInit: Self::Null => CharAscii];
impl CharAscii {
#[must_use]
pub const fn from_u8(b: u8) -> Option<Self> {
match b {
0 => Some(Self::Null),
1 => Some(Self::StartOfHeading),
2 => Some(Self::StartOfText),
3 => Some(Self::EndOfText),
4 => Some(Self::EndOfTransmission),
5 => Some(Self::Enquiry),
6 => Some(Self::Acknowledge),
7 => Some(Self::Bell),
8 => Some(Self::Backspace),
9 => Some(Self::AsciiCharacterTabulation),
10 => Some(Self::LineFeed),
11 => Some(Self::LineTabulation),
12 => Some(Self::FormFeed),
13 => Some(Self::CarriageReturn),
14 => Some(Self::ShiftOut),
15 => Some(Self::ShiftIn),
16 => Some(Self::DataLinkEscape),
17 => Some(Self::DeviceControlOne),
18 => Some(Self::DeviceControlTwo),
19 => Some(Self::DeviceControlThree),
20 => Some(Self::DeviceControlFour),
21 => Some(Self::NegativeAcknowledge),
22 => Some(Self::SynchronousIdle),
23 => Some(Self::EndOfTransmissionBlock),
24 => Some(Self::Cancel),
25 => Some(Self::EndOfMedium),
26 => Some(Self::Substitute),
27 => Some(Self::Escape),
28 => Some(Self::InformationSeparatorFour),
29 => Some(Self::InformationSeparatorThree),
30 => Some(Self::InformationSeparatorTwo),
31 => Some(Self::InformationSeparatorOne),
32 => Some(Self::Space),
33 => Some(Self::ExclamationMark),
34 => Some(Self::QuotationMark),
35 => Some(Self::NumberSign),
36 => Some(Self::DollarSign),
37 => Some(Self::PercentSign),
38 => Some(Self::Ampersand),
39 => Some(Self::Apostrophe),
40 => Some(Self::LeftParenthesis),
41 => Some(Self::RightParenthesis),
42 => Some(Self::Asterisk),
43 => Some(Self::PlusSign),
44 => Some(Self::Comma),
45 => Some(Self::HyphenMinus),
46 => Some(Self::FullStop),
47 => Some(Self::Solidus),
48 => Some(Self::Digit0),
49 => Some(Self::Digit1),
50 => Some(Self::Digit2),
51 => Some(Self::Digit3),
52 => Some(Self::Digit4),
53 => Some(Self::Digit5),
54 => Some(Self::Digit6),
55 => Some(Self::Digit7),
56 => Some(Self::Digit8),
57 => Some(Self::Digit9),
58 => Some(Self::Colon),
59 => Some(Self::Semicolon),
60 => Some(Self::LessThanSign),
61 => Some(Self::EqualsSign),
62 => Some(Self::GreaterThanSign),
63 => Some(Self::QuestionMark),
64 => Some(Self::CommercialAt),
65 => Some(Self::CapitalA),
66 => Some(Self::CapitalB),
67 => Some(Self::CapitalC),
68 => Some(Self::CapitalD),
69 => Some(Self::CapitalE),
70 => Some(Self::CapitalF),
71 => Some(Self::CapitalG),
72 => Some(Self::CapitalH),
73 => Some(Self::CapitalI),
74 => Some(Self::CapitalJ),
75 => Some(Self::CapitalK),
76 => Some(Self::CapitalL),
77 => Some(Self::CapitalM),
78 => Some(Self::CapitalN),
79 => Some(Self::CapitalO),
80 => Some(Self::CapitalP),
81 => Some(Self::CapitalQ),
82 => Some(Self::CapitalR),
83 => Some(Self::CapitalS),
84 => Some(Self::CapitalT),
85 => Some(Self::CapitalU),
86 => Some(Self::CapitalV),
87 => Some(Self::CapitalW),
88 => Some(Self::CapitalX),
89 => Some(Self::CapitalY),
90 => Some(Self::CapitalZ),
91 => Some(Self::LeftSquareBracket),
92 => Some(Self::ReverseSolidus),
93 => Some(Self::RightSquareBracket),
94 => Some(Self::CircumflexAccent),
95 => Some(Self::LowLine),
96 => Some(Self::GraveAccent),
97 => Some(Self::SmallA),
98 => Some(Self::SmallB),
99 => Some(Self::SmallC),
100 => Some(Self::SmallD),
101 => Some(Self::SmallE),
102 => Some(Self::SmallF),
103 => Some(Self::SmallG),
104 => Some(Self::SmallH),
105 => Some(Self::SmallI),
106 => Some(Self::SmallJ),
107 => Some(Self::SmallK),
108 => Some(Self::SmallL),
109 => Some(Self::SmallM),
110 => Some(Self::SmallN),
111 => Some(Self::SmallO),
112 => Some(Self::SmallP),
113 => Some(Self::SmallQ),
114 => Some(Self::SmallR),
115 => Some(Self::SmallS),
116 => Some(Self::SmallT),
117 => Some(Self::SmallU),
118 => Some(Self::SmallV),
119 => Some(Self::SmallW),
120 => Some(Self::SmallX),
121 => Some(Self::SmallY),
122 => Some(Self::SmallZ),
123 => Some(Self::LeftCurlyBracket),
124 => Some(Self::VerticalLine),
125 => Some(Self::RightCurlyBracket),
126 => Some(Self::Tilde),
127 => Some(Self::Delete),
_ => None,
}
}
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
pub const unsafe fn from_u8_unchecked(b: u8) -> Self {
unsafe { transmute(b) }
}
#[must_use]
pub const fn digit(d: u8) -> Option<Self> {
if d < 10 {
let sum = {
#[cfg(any(feature = "safe_text", not(feature = "unsafe_hint")))]
{
b'0' + d
}
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_hint"))]
unsafe {
b'0'.unchecked_add(d)
}
};
Self::from_u8(sum)
} else {
None
}
}
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
pub const unsafe fn digit_unchecked(d: u8) -> Self {
debug_assert!(d < 10);
unsafe {
let byte = b'0'.unchecked_add(d);
Self::from_u8_unchecked(byte)
}
}
#[must_use]
pub const fn as_u8(self) -> u8 {
self as u8
}
#[must_use]
pub const fn as_char(self) -> char {
self as u8 as char
}
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
pub const fn as_str(&self) -> &str {
Self::slice_as_str(core::slice::from_ref(self))
}
}
impl CharAscii {
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
pub const fn slice_as_str(slice: &[CharAscii]) -> &str {
let ascii_ptr: *const [CharAscii] = slice;
let str_ptr = ascii_ptr as *const str;
unsafe { &*str_ptr }
}
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
pub const fn slice_as_bytes(slice: &[CharAscii]) -> &[u8] {
CharAscii::slice_as_str(slice).as_bytes()
}
}
impl fmt::Display for CharAscii {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.as_char(), f)
}
}