diff --git a/src/lib/str.rs b/src/lib/str.rs index 8591ec6aecb3..fb0d0e7e773b 100644 --- a/src/lib/str.rs +++ b/src/lib/str.rs @@ -50,6 +50,8 @@ export to_upper; export safe_slice; export bytes_ivec; export unsafe_from_bytes_ivec; +export is_empty; +export is_not_empty; native "rust" mod rustrt { type sbuf; @@ -153,6 +155,16 @@ fn is_ascii(str s) -> bool { fn alloc(uint n_bytes) -> str { ret rustrt::str_alloc(n_bytes); } +/// Returns true if the string has length 0 +pred is_empty(str s) -> bool { + for (u8 c in s) { + ret false; + } + ret true; +} + +/// Returns true if the string has length greater than 0 +pred is_not_empty(str s) -> bool { !is_empty(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 diff --git a/src/test/run-pass/lib-str.rs b/src/test/run-pass/lib-str.rs index 0160f0f1d3ca..cfef1cff1c09 100644 --- a/src/test/run-pass/lib-str.rs +++ b/src/test/run-pass/lib-str.rs @@ -122,6 +122,16 @@ fn test_ends_with() { assert (!str::ends_with("", "abc")); } +fn test_is_empty() { + assert str::is_empty(""); + assert !str::is_empty("a"); +} + +fn test_is_not_empty() { + assert str::is_not_empty("a"); + assert !str::is_not_empty(""); +} + fn main() { test_bytes_len(); test_index_and_rindex(); @@ -133,4 +143,6 @@ fn main() { test_to_upper(); test_slice(); test_ends_with(); + test_is_empty(); + test_is_not_empty(); } \ No newline at end of file