Add std::str::is_whitespace

This commit is contained in:
Brian Anderson 2011-08-01 18:37:22 -07:00
parent 1ad68eafd2
commit c9b16ac4c2
2 changed files with 22 additions and 0 deletions

View File

@ -52,6 +52,7 @@ export bytes_ivec;
export unsafe_from_bytes_ivec; export unsafe_from_bytes_ivec;
export is_empty; export is_empty;
export is_not_empty; export is_not_empty;
export is_whitespace;
export replace; export replace;
export char_slice; export char_slice;
export trim_left; export trim_left;
@ -166,6 +167,18 @@ pred is_empty(s: str) -> bool { for c: u8 in s { ret false; } ret true; }
/// Returns true if the string has length greater than 0 /// Returns true if the string has length greater than 0
pred is_not_empty(s: str) -> bool { !is_empty(s) } pred is_not_empty(s: str) -> bool { !is_empty(s) }
fn is_whitespace(s: str) -> bool {
let i = 0u;
let len = char_len(s);
while i < len {
if !char::is_whitespace(char_at(s, i)) {
ret false;
}
i += 1u
}
ret true;
}
// Returns the number of bytes (a.k.a. UTF-8 code units) in s. // Returns the number of bytes (a.k.a. UTF-8 code units) in s.
// Contrast with a function that would return the number of code // Contrast with a function that would return the number of code
// points (char's), combining character sequences, words, etc. See // points (char's), combining character sequences, words, etc. See

View File

@ -192,6 +192,15 @@ fn trim() {
assert str::trim(" hey dude ") == "hey dude"; assert str::trim(" hey dude ") == "hey dude";
} }
#[test]
fn is_whitespace() {
assert str::is_whitespace("");
assert str::is_whitespace(" ");
assert str::is_whitespace("\u2009"); // Thin space
assert str::is_whitespace(" \n\t ");
assert !str::is_whitespace(" _ ");
}
// Local Variables: // Local Variables:
// mode: rust; // mode: rust;
// fill-column: 78; // fill-column: 78;