diff options
| author | Kevin Cantu <me@kevincantu.org> | 2012-01-23 00:36:58 -0800 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2012-01-23 22:28:25 -0800 |
| commit | 0493a7c87d7769a3b3b819a7b38cd242f95dfad2 (patch) | |
| tree | ab15c6d01a375c391f3e93db264d5d740653ecbe | |
| parent | 477c3a8eb16a0b81e5211c929851ce221e9268d7 (diff) | |
| download | rust-0493a7c87d7769a3b3b819a7b38cd242f95dfad2.tar.gz rust-0493a7c87d7769a3b3b819a7b38cd242f95dfad2.zip | |
Added str::map and str::all functions
| -rw-r--r-- | src/libcore/str.rs | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1cccaf0f641..a090d57be45 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1113,6 +1113,33 @@ fn escape(s: str) -> str { r } +/* +Function: all + +Return true if a predicate matches all characters + +If the string contains no characters +*/ +fn all(ss: str, ff: fn&(char) -> bool) -> bool { + str::loop_chars(ss, ff) +} + +/* +Function: map + +Apply a function to each character +*/ +fn map(ss: str, ff: fn&(char) -> char) -> str { + let result = ""; + + str::iter_chars(ss, {|cc| + str::push_char(result, ff(cc)); + }); + + ret result; +} + + #[cfg(test)] mod tests { @@ -1572,4 +1599,19 @@ mod tests { assert(escape("abc\ndef") == "abc\\ndef"); assert(escape("abc\"def") == "abc\\\"def"); } -} \ No newline at end of file + + #[test] + fn test_map () { + assert "" == map("", char::to_upper); + assert "YMCA" == map("ymca", char::to_upper); + } + + #[test] + fn test_all () { + assert true == all("", char::is_uppercase); + assert false == all("ymca", char::is_uppercase); + assert true == all("YMCA", char::is_uppercase); + assert false == all("yMCA", char::is_uppercase); + assert false == all("YMCy", char::is_uppercase); + } +} |
