Skip to content

std::ascii: Fix is_digit() and is_control() #10685

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 2 commits into from
Nov 27, 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
13 changes: 11 additions & 2 deletions src/libstd/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Ascii {
/// 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
Expand All @@ -85,7 +85,7 @@ impl Ascii {
/// 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)
Expand Down Expand Up @@ -498,6 +498,15 @@ mod tests {
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()));
}
Expand Down