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