Skip to content

Commit 0493a7c

Browse files
killerswannikomatsakis
authored andcommitted
Added str::map and str::all functions
1 parent 477c3a8 commit 0493a7c

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/libcore/str.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,33 @@ fn escape(s: str) -> str {
11131113
r
11141114
}
11151115

1116+
/*
1117+
Function: all
1118+
1119+
Return true if a predicate matches all characters
1120+
1121+
If the string contains no characters
1122+
*/
1123+
fn all(ss: str, ff: fn&(char) -> bool) -> bool {
1124+
str::loop_chars(ss, ff)
1125+
}
1126+
1127+
/*
1128+
Function: map
1129+
1130+
Apply a function to each character
1131+
*/
1132+
fn map(ss: str, ff: fn&(char) -> char) -> str {
1133+
let result = "";
1134+
1135+
str::iter_chars(ss, {|cc|
1136+
str::push_char(result, ff(cc));
1137+
});
1138+
1139+
ret result;
1140+
}
1141+
1142+
11161143
#[cfg(test)]
11171144
mod tests {
11181145

@@ -1572,4 +1599,19 @@ mod tests {
15721599
assert(escape("abc\ndef") == "abc\\ndef");
15731600
assert(escape("abc\"def") == "abc\\\"def");
15741601
}
1575-
}
1602+
1603+
#[test]
1604+
fn test_map () {
1605+
assert "" == map("", char::to_upper);
1606+
assert "YMCA" == map("ymca", char::to_upper);
1607+
}
1608+
1609+
#[test]
1610+
fn test_all () {
1611+
assert true == all("", char::is_uppercase);
1612+
assert false == all("ymca", char::is_uppercase);
1613+
assert true == all("YMCA", char::is_uppercase);
1614+
assert false == all("yMCA", char::is_uppercase);
1615+
assert false == all("YMCy", char::is_uppercase);
1616+
}
1617+
}

0 commit comments

Comments
 (0)