mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-10 23:06:23 +00:00
Add std::str::is_whitespace
This commit is contained in:
parent
1ad68eafd2
commit
c9b16ac4c2
@ -52,6 +52,7 @@ export bytes_ivec;
|
||||
export unsafe_from_bytes_ivec;
|
||||
export is_empty;
|
||||
export is_not_empty;
|
||||
export is_whitespace;
|
||||
export replace;
|
||||
export char_slice;
|
||||
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
|
||||
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.
|
||||
// Contrast with a function that would return the number of code
|
||||
// points (char's), combining character sequences, words, etc. See
|
||||
|
@ -192,6 +192,15 @@ fn trim() {
|
||||
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:
|
||||
// mode: rust;
|
||||
// fill-column: 78;
|
||||
|
Loading…
Reference in New Issue
Block a user