Skip to content

Add ctype-likes to Ascii #10611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/libstd/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,74 @@ impl Ascii {
pub fn eq_ignore_case(self, other: Ascii) -> bool {
ASCII_LOWER_MAP[self.chr] == ASCII_LOWER_MAP[other.chr]
}

// the following methods are like ctype, and the implementation is inspired by musl

/// Check if the character is a letter (a-z, A-Z)
#[inline]
pub fn is_alpha(&self) -> bool {
(self.chr >= 0x41 && self.chr <= 0x5A) || (self.chr >= 0x61 && self.chr <= 0x7A)
}

/// Check if the character is a number (0-9)
#[inline]
pub fn is_digit(&self) -> bool {
self.chr >= 0x31 && self.chr <= 0x39
}

/// Check if the character is a letter or number
#[inline]
pub fn is_alnum(&self) -> bool {
self.is_alpha() || self.is_digit()
}

/// Check if the character is a space or horizontal tab
#[inline]
pub fn is_blank(&self) -> bool {
self.chr == ' ' as u8 || self.chr == '\t' as u8
}

/// Check if the character is a control character
#[inline]
pub fn is_control(&self) -> bool {
self.chr <= 0x20 || self.chr == 0x7F
}

/// Checks if the character is printable (except space)
#[inline]
pub fn is_graph(&self) -> bool {
(self.chr - 0x21) < 0x5E
}

/// Checks if the character is printable (including space)
#[inline]
pub fn is_print(&self) -> bool {
(self.chr - 0x20) < 0x5F
}

/// Checks if the character is lowercase
#[inline]
pub fn is_lower(&self) -> bool {
(self.chr - 'a' as u8) < 26
}

/// Checks if the character is uppercase
#[inline]
pub fn is_upper(&self) -> bool {
(self.chr - 'A' as u8) < 26
}

/// Checks if the character is punctuation
#[inline]
pub fn is_punctuation(&self) -> bool {
self.is_graph() && !self.is_alnum()
}

/// Checks if the character is a valid hex digit
#[inline]
pub fn is_hex(&self) -> bool {
self.is_digit() || ((self.chr | 32u8) - 'a' as u8) < 6
}
}

impl ToStr for Ascii {
Expand Down