From 7b96f13d7d046a5f625af73765515715a270757e Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Tue, 26 Nov 2013 20:13:25 -0600 Subject: [PATCH 1/2] std::ascii: Fix is_digit() and is_control() is_digit() incorrectly returned false for '0'. is_control() incorrectly returned true for ' ' (space). --- src/libstd/ascii.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 18b6a1ef52a..d62a03a5ad4 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -67,7 +67,7 @@ pub fn is_alpha(&self) -> bool { /// Check if the character is a number (0-9) #[inline] pub fn is_digit(&self) -> bool { - self.chr >= 0x31 && self.chr <= 0x39 + self.chr >= 0x30 && self.chr <= 0x39 } /// Check if the character is a letter or number @@ -85,7 +85,7 @@ pub fn is_blank(&self) -> bool { /// Check if the character is a control character #[inline] pub fn is_control(&self) -> bool { - self.chr <= 0x20 || self.chr == 0x7F + self.chr < 0x20 || self.chr == 0x7F } /// Checks if the character is printable (except space) From 64883242ebece291170d86483cb1b3656ddc3d23 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Wed, 27 Nov 2013 09:54:54 -0600 Subject: [PATCH 2/2] std::ascii: Add tests for is_digit() and is_control() --- src/libstd/ascii.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index d62a03a5ad4..c43b4e9b6ea 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -498,6 +498,15 @@ fn test_ascii() { assert_eq!('`'.to_ascii().to_upper().to_char(), '`'); assert_eq!('{'.to_ascii().to_upper().to_char(), '{'); + assert!('0'.to_ascii().is_digit()); + assert!('9'.to_ascii().is_digit()); + assert!(!'/'.to_ascii().is_digit()); + assert!(!':'.to_ascii().is_digit()); + + assert!((0x1fu8).to_ascii().is_control()); + assert!(!' '.to_ascii().is_control()); + assert!((0x7fu8).to_ascii().is_control()); + assert!("banana".chars().all(|c| c.is_ascii())); assert!(!"ประเทศไทย中华Việt Nam".chars().all(|c| c.is_ascii())); }