Add str::is_empty, is_not_empty preds

This commit is contained in:
Brian Anderson 2011-07-18 15:46:37 -07:00
parent b6fc86ae5a
commit 689f5f487c
2 changed files with 24 additions and 0 deletions

View file

@ -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

View file

@ -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();
}