#![cfg_attr(rustfmt, rustfmt_skip)]
#![cfg_attr(feature = "std", allow(deprecated))]
#[cfg(feature = "quickcheck")]
use quickcheck::{Arbitrary, Gen};
use core::mem;
use core::cmp::Ordering;
use core::{fmt, char};
#[cfg(feature = "std")]
use std::error::Error;
#[cfg(feature = "std")]
use std::ascii::AsciiExt;
#[allow(non_camel_case_types)]
#[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Hash, Copy)]
#[repr(u8)]
pub enum AsciiChar {
Null = 0,
SOH = 1,
SOX = 2,
ETX = 3,
EOT = 4,
ENQ = 5,
ACK = 6,
Bell = 7,
BackSpace = 8,
Tab = 9,
LineFeed = 10,
VT = 11,
FF = 12,
CarriageReturn = 13,
SI = 14,
SO = 15,
DLE = 16,
DC1 = 17,
DC2 = 18,
DC3 = 19,
DC4 = 20,
NAK = 21,
SYN = 22,
ETB = 23,
CAN = 24,
EM = 25,
SUB = 26,
ESC = 27,
FS = 28,
GS = 29,
RS = 30,
US = 31,
Space = 32,
Exclamation = 33,
Quotation = 34,
Hash = 35,
Dollar = 36,
Percent = 37,
Ampersand = 38,
Apostrophe = 39,
ParenOpen = 40,
ParenClose = 41,
Asterisk = 42,
Plus = 43,
Comma = 44,
Minus = 45,
Dot = 46,
Slash = 47,
_0 = 48,
_1 = 49,
_2 = 50,
_3 = 51,
_4 = 52,
_5 = 53,
_6 = 54,
_7 = 55,
_8 = 56,
_9 = 57,
Colon = 58,
Semicolon = 59,
LessThan = 60,
Equal = 61,
GreaterThan = 62,
Question = 63,
At = 64,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
BracketOpen = 91,
BackSlash = 92,
BracketClose = 93,
Caret = 94,
UnderScore = 95,
Grave = 96,
a = 97,
b = 98,
c = 99,
d = 100,
e = 101,
f = 102,
g = 103,
h = 104,
i = 105,
j = 106,
k = 107,
l = 108,
m = 109,
n = 110,
o = 111,
p = 112,
q = 113,
r = 114,
s = 115,
t = 116,
u = 117,
v = 118,
w = 119,
x = 120,
y = 121,
z = 122,
CurlyBraceOpen = 123,
VerticalBar = 124,
CurlyBraceClose = 125,
Tilde = 126,
DEL = 127,
}
impl AsciiChar {
#[inline]
pub fn from<C: ToAsciiChar>(ch: C) -> Result<Self, ToAsciiCharError> {
ch.to_ascii_char()
}
#[inline]
pub unsafe fn from_unchecked<C: ToAsciiChar>(ch: C) -> Self {
ch.to_ascii_char_unchecked()
}
#[inline]
pub fn as_byte(self) -> u8 {
self as u8
}
#[inline]
pub fn as_char(self) -> char {
self.as_byte() as char
}
#[inline]
pub fn is_alphabetic(self) -> bool {
let c = self.as_byte() | 0b010_0000;
c >= b'a' && c <= b'z'
}
#[inline]
pub fn is_digit(self) -> bool {
self >= AsciiChar::_0 && self <= AsciiChar::_9
}
#[inline]
pub fn is_alphanumeric(self) -> bool {
self.is_alphabetic() || self.is_digit()
}
#[inline]
pub fn is_blank(self) -> bool {
self == AsciiChar::Space || self == AsciiChar::Tab
}
#[inline]
pub fn is_whitespace(self) -> bool {
self.is_blank() || self == AsciiChar::LineFeed || self == AsciiChar::CarriageReturn
}
#[inline]
pub fn is_control(self) -> bool {
self < AsciiChar::Space || self == AsciiChar::DEL
}
#[inline]
pub fn is_graph(self) -> bool {
self.as_byte().wrapping_sub(b' ' + 1) < 0x5E
}
#[inline]
pub fn is_print(self) -> bool {
self.as_byte().wrapping_sub(b' ') < 0x5F
}
#[inline]
pub fn is_lowercase(self) -> bool {
self.as_byte().wrapping_sub(b'a') < 26
}
#[inline]
pub fn is_uppercase(self) -> bool {
self.as_byte().wrapping_sub(b'A') < 26
}
#[inline]
pub fn is_punctuation(self) -> bool {
self.is_graph() && !self.is_alphanumeric()
}
#[inline]
pub fn is_hex(self) -> bool {
self.is_digit() || (self.as_byte() | 0x20u8).wrapping_sub(b'a') < 6
}
pub fn as_printable_char(self) -> char {
unsafe {
match self as u8 {
b' '...b'~' => self.as_char(),
127 => '␡',
_ => char::from_u32_unchecked(self as u32 + '␀' as u32),
}
}
}
pub fn make_ascii_uppercase(&mut self) {
*self = self.to_ascii_uppercase()
}
pub fn make_ascii_lowercase(&mut self) {
*self = self.to_ascii_lowercase()
}
#[inline]
pub fn to_ascii_uppercase(&self) -> Self {
unsafe {
match *self as u8 {
b'a'...b'z' => AsciiChar::from_unchecked(self.as_byte() - (b'a' - b'A')),
_ => *self,
}
}
}
#[inline]
pub fn to_ascii_lowercase(&self) -> Self {
unsafe {
match *self as u8 {
b'A'...b'Z' => AsciiChar::from_unchecked(self.as_byte() + (b'a' - b'A')),
_ => *self,
}
}
}
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
self.to_ascii_lowercase() == other.to_ascii_lowercase()
}
}
impl fmt::Display for AsciiChar {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_char().fmt(f)
}
}
impl fmt::Debug for AsciiChar {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_char().fmt(f)
}
}
#[cfg(feature = "std")]
impl AsciiExt for AsciiChar {
type Owned = AsciiChar;
#[inline]
fn is_ascii(&self) -> bool {
true
}
#[inline]
fn to_ascii_uppercase(&self) -> AsciiChar {
unsafe {
self.as_byte()
.to_ascii_uppercase()
.to_ascii_char_unchecked()
}
}
#[inline]
fn to_ascii_lowercase(&self) -> AsciiChar {
unsafe {
self.as_byte()
.to_ascii_lowercase()
.to_ascii_char_unchecked()
}
}
fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
self.as_byte().eq_ignore_ascii_case(&other.as_byte())
}
#[inline]
fn make_ascii_uppercase(&mut self) {
*self = self.to_ascii_uppercase();
}
#[inline]
fn make_ascii_lowercase(&mut self) {
*self = self.to_ascii_lowercase();
}
}
macro_rules! impl_into_partial_eq_ord {($wider:ty, $to_wider:expr) => {
impl From<AsciiChar> for $wider {
#[inline]
fn from(a: AsciiChar) -> $wider {
$to_wider(a)
}
}
impl PartialEq<$wider> for AsciiChar {
#[inline]
fn eq(&self, rhs: &$wider) -> bool {
$to_wider(*self) == *rhs
}
}
impl PartialEq<AsciiChar> for $wider {
#[inline]
fn eq(&self, rhs: &AsciiChar) -> bool {
*self == $to_wider(*rhs)
}
}
impl PartialOrd<$wider> for AsciiChar {
#[inline]
fn partial_cmp(&self, rhs: &$wider) -> Option<Ordering> {
$to_wider(*self).partial_cmp(rhs)
}
}
impl PartialOrd<AsciiChar> for $wider {
#[inline]
fn partial_cmp(&self, rhs: &AsciiChar) -> Option<Ordering> {
self.partial_cmp(&$to_wider(*rhs))
}
}
}}
impl_into_partial_eq_ord!{u8, AsciiChar::as_byte}
impl_into_partial_eq_ord!{char, AsciiChar::as_char}
#[derive(PartialEq)]
pub struct ToAsciiCharError(());
const ERRORMSG_CHAR: &'static str = "not an ASCII character";
#[cfg(not(feature = "std"))]
impl ToAsciiCharError {
#[inline]
pub fn description(&self) -> &'static str {
ERRORMSG_CHAR
}
}
impl fmt::Debug for ToAsciiCharError {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
write!(fmtr, "{}", ERRORMSG_CHAR)
}
}
impl fmt::Display for ToAsciiCharError {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
write!(fmtr, "{}", ERRORMSG_CHAR)
}
}
#[cfg(feature = "std")]
impl Error for ToAsciiCharError {
#[inline]
fn description(&self) -> &'static str {
ERRORMSG_CHAR
}
}
pub trait ToAsciiChar {
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar;
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError>;
}
impl ToAsciiChar for AsciiChar {
#[inline]
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError> {
Ok(self)
}
#[inline]
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar {
self
}
}
impl ToAsciiChar for u8 {
#[inline]
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError> {
unsafe {
if self <= 0x7F {
return Ok(self.to_ascii_char_unchecked());
}
}
Err(ToAsciiCharError(()))
}
#[inline]
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar {
mem::transmute(self)
}
}
impl ToAsciiChar for char {
#[inline]
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError> {
unsafe {
if self as u32 <= 0x7F {
return Ok(self.to_ascii_char_unchecked());
}
}
Err(ToAsciiCharError(()))
}
#[inline]
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar {
(self as u8).to_ascii_char_unchecked()
}
}
#[cfg(feature = "quickcheck")]
impl Arbitrary for AsciiChar {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let mode = g.gen_range(0, 100);
match mode {
0...14 => {
unsafe { AsciiChar::from_unchecked(g.gen_range(0, 0x1F) as u8) }
}
15...39 => {
*g.choose(&[
AsciiChar::Space, AsciiChar::Tab, AsciiChar::LineFeed, AsciiChar::Tilde,
AsciiChar::Grave, AsciiChar::Exclamation, AsciiChar::At, AsciiChar::Hash,
AsciiChar::Dollar, AsciiChar::Percent, AsciiChar::Ampersand,
AsciiChar::Asterisk, AsciiChar::ParenOpen, AsciiChar::ParenClose,
AsciiChar::UnderScore, AsciiChar::Minus, AsciiChar::Equal, AsciiChar::Plus,
AsciiChar::BracketOpen, AsciiChar::BracketClose, AsciiChar::CurlyBraceOpen,
AsciiChar::CurlyBraceClose, AsciiChar::Colon, AsciiChar::Semicolon,
AsciiChar::Apostrophe, AsciiChar::Quotation, AsciiChar::BackSlash,
AsciiChar::VerticalBar, AsciiChar::Caret, AsciiChar::Comma, AsciiChar::LessThan,
AsciiChar::GreaterThan, AsciiChar::Dot, AsciiChar::Slash, AsciiChar::Question,
AsciiChar::_0, AsciiChar::_1, AsciiChar::_2, AsciiChar::_3, AsciiChar::_3,
AsciiChar::_4 , AsciiChar::_6, AsciiChar::_7, AsciiChar::_8, AsciiChar::_9,
]).unwrap()
}
40...99 => {
unsafe { AsciiChar::from_unchecked(g.gen_range(0, 0x7F) as u8) }
}
_ => unreachable!(),
}
}
fn shrink(&self) -> Box<Iterator<Item = Self>> {
Box::new((*self as u8).shrink().filter_map(
|x| AsciiChar::from(x).ok(),
))
}
}
#[cfg(test)]
mod tests {
use super::{AsciiChar, ToAsciiChar, ToAsciiCharError};
use AsciiChar::*;
#[test]
fn to_ascii_char() {
fn generic<C: ToAsciiChar>(ch: C) -> Result<AsciiChar, ToAsciiCharError> {
ch.to_ascii_char()
}
assert_eq!(generic(A), Ok(A));
assert_eq!(generic(b'A'), Ok(A));
assert_eq!(generic('A'), Ok(A));
assert!(generic(200).is_err());
assert!(generic('λ').is_err());
}
#[test]
fn as_byte_and_char() {
assert_eq!(A.as_byte(), b'A');
assert_eq!(A.as_char(), 'A');
}
#[test]
fn is_digit() {
assert_eq!('0'.to_ascii_char().unwrap().is_digit(), true);
assert_eq!('9'.to_ascii_char().unwrap().is_digit(), true);
assert_eq!('/'.to_ascii_char().unwrap().is_digit(), false);
assert_eq!(':'.to_ascii_char().unwrap().is_digit(), false);
}
#[test]
fn is_control() {
assert_eq!(US.is_control(), true);
assert_eq!(DEL.is_control(), true);
assert_eq!(Space.is_control(), false);
}
#[test]
fn cmp_wider() {
assert_eq!(A, 'A');
assert_eq!(b'b', b);
assert!(a < 'z');
}
#[test]
fn ascii_case() {
assert_eq!(At.to_ascii_lowercase(), At);
assert_eq!(At.to_ascii_uppercase(), At);
assert_eq!(A.to_ascii_lowercase(), a);
assert_eq!(A.to_ascii_uppercase(), A);
assert_eq!(a.to_ascii_lowercase(), a);
assert_eq!(a.to_ascii_uppercase(), A);
assert!(LineFeed.eq_ignore_ascii_case(&LineFeed));
assert!(!LineFeed.eq_ignore_ascii_case(&CarriageReturn));
assert!(z.eq_ignore_ascii_case(&Z));
assert!(Z.eq_ignore_ascii_case(&z));
assert!(!Z.eq_ignore_ascii_case(&DEL));
}
#[test]
#[cfg(feature = "std")]
fn ascii_ext() {
#[allow(deprecated)]
use std::ascii::AsciiExt;
assert!(AsciiExt::is_ascii(&Null));
assert!(AsciiExt::is_ascii(&DEL));
assert!(AsciiExt::eq_ignore_ascii_case(&a, &A));
assert!(!AsciiExt::eq_ignore_ascii_case(&A, &At));
assert_eq!(AsciiExt::to_ascii_lowercase(&A), a);
assert_eq!(AsciiExt::to_ascii_uppercase(&A), A);
assert_eq!(AsciiExt::to_ascii_lowercase(&a), a);
assert_eq!(AsciiExt::to_ascii_uppercase(&a), A);
assert_eq!(AsciiExt::to_ascii_lowercase(&At), At);
assert_eq!(AsciiExt::to_ascii_uppercase(&At), At);
let mut mutable = (A,a);
AsciiExt::make_ascii_lowercase(&mut mutable.0);
AsciiExt::make_ascii_uppercase(&mut mutable.1);
assert_eq!(mutable.0, a);
assert_eq!(mutable.1, A);
}
#[test]
#[cfg(feature = "std")]
fn fmt_ascii() {
assert_eq!(format!("{}", t), "t");
assert_eq!(format!("{:?}", t), "'t'");
assert_eq!(format!("{}", LineFeed), "\n");
assert_eq!(format!("{:?}", LineFeed), "'\\n'");
}
}