diff options
| author | bors <bors@rust-lang.org> | 2013-11-22 23:06:24 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-11-22 23:06:24 -0800 |
| commit | 6cbc57cadbf8dfb2053893c917fb89ccbca0f253 (patch) | |
| tree | 72681aa65c7192b619604ab188d8ecc2d1da6781 /src/libstd | |
| parent | fb279aa02a7ff2ed069791f60a5159d73c8ed993 (diff) | |
| parent | 09af9d48561380f8de3aa9a8b15b2b4e9af4daef (diff) | |
auto merge of #10611 : cmr/rust/ascii_flesh, r=pcwalton
These are super boring. I can add tests if really desired, but they'd be long and even more boring than the methods.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ascii.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index c4641eff572..fb1cb26ec5d 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -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 { |
