about summary refs log tree commit diff
path: root/src/libstd/ascii.rs
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2013-11-22 11:38:50 -0500
committerCorey Richardson <corey@octayn.net>2013-11-23 02:01:10 -0500
commit09af9d48561380f8de3aa9a8b15b2b4e9af4daef (patch)
treee796800b65344221dc0eeb3d9fc36a89098cf7e7 /src/libstd/ascii.rs
parent747213a280ac5505e2537952f1d28efceda0bfcc (diff)
downloadrust-09af9d48561380f8de3aa9a8b15b2b4e9af4daef.tar.gz
rust-09af9d48561380f8de3aa9a8b15b2b4e9af4daef.zip
Add ctype-likes to Ascii
Diffstat (limited to 'src/libstd/ascii.rs')
-rw-r--r--src/libstd/ascii.rs68
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 {