Skip to content

Commit df97d23

Browse files
committed
auto merge of #9455 : jesseray/rust/master, r=pnkfelix
In "/src/libstd/char.rs", there are function and method definitions for `is_lowercase()`, `is_uppercase()`, `is_whitespace()`, etc. However, there was no function or method for control characters, so I added the `is_control()` function and method definitions along with documentation and tests. Running `./configure && make check` shows that all tests for `is_control()` pass.
2 parents f6c9ff3 + 13571af commit df97d23

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libstd/char.rs

+24
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ pub fn is_alphanumeric(c: char) -> bool {
128128
|| general_category::No(c)
129129
}
130130

131+
///
132+
/// Indicates whether a character is a control character. Control
133+
/// characters are defined in terms of the Unicode General Category
134+
/// 'Cc'.
135+
///
136+
#[inline]
137+
pub fn is_control(c: char) -> bool { general_category::Cc(c) }
138+
131139
/// Indicates whether the character is numeric (Nd, Nl, or No)
132140
#[inline]
133141
pub fn is_digit(c: char) -> bool {
@@ -354,6 +362,7 @@ pub trait Char {
354362
fn is_uppercase(&self) -> bool;
355363
fn is_whitespace(&self) -> bool;
356364
fn is_alphanumeric(&self) -> bool;
365+
fn is_control(&self) -> bool;
357366
fn is_digit(&self) -> bool;
358367
fn is_digit_radix(&self, radix: uint) -> bool;
359368
fn to_digit(&self, radix: uint) -> Option<uint>;
@@ -384,6 +393,8 @@ impl Char for char {
384393

385394
fn is_alphanumeric(&self) -> bool { is_alphanumeric(*self) }
386395

396+
fn is_control(&self) -> bool { is_control(*self) }
397+
387398
fn is_digit(&self) -> bool { is_digit(*self) }
388399

389400
fn is_digit_radix(&self, radix: uint) -> bool { is_digit_radix(*self, radix) }
@@ -494,6 +505,19 @@ fn test_to_digit() {
494505
assert_eq!('$'.to_digit(36u), None);
495506
}
496507

508+
#[test]
509+
fn test_is_control() {
510+
assert!('\u0000'.is_control());
511+
assert!('\u0003'.is_control());
512+
assert!('\u0006'.is_control());
513+
assert!('\u0009'.is_control());
514+
assert!('\u007f'.is_control());
515+
assert!('\u0092'.is_control());
516+
assert!(!'\u0020'.is_control());
517+
assert!(!'\u0055'.is_control());
518+
assert!(!'\u0068'.is_control());
519+
}
520+
497521
#[test]
498522
fn test_is_digit() {
499523
assert!('2'.is_digit());

0 commit comments

Comments
 (0)