diff --git a/src/lib/str.rs b/src/lib/str.rs index f32daa67057..28bf8fa2dfc 100644 --- a/src/lib/str.rs +++ b/src/lib/str.rs @@ -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 diff --git a/src/test/stdtest/str.rs b/src/test/stdtest/str.rs index 08e78a56137..0a27f3b4311 100644 --- a/src/test/stdtest/str.rs +++ b/src/test/stdtest/str.rs @@ -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;