Skip to content

Commit 1f795ff

Browse files
killerswanbrson
authored andcommitted
Re-implementing str::to_upper and str::to_lower using str::map
1 parent a8b6573 commit 1f795ff

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/libcore/str.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -674,30 +674,18 @@ fn windowed(nn: uint, ss: str) -> [str] {
674674
Function: to_lower
675675
676676
Convert a string to lowercase
677-
678-
FIXME: rewrite with map
679677
*/
680678
fn to_lower(s: str) -> str {
681-
let outstr = "";
682-
chars_iter(s) { |c|
683-
push_char(outstr, char::to_lower(c));
684-
}
685-
ret outstr;
679+
map(s, char::to_lower)
686680
}
687681

688682
/*
689683
Function: to_upper
690684
691685
Convert a string to uppercase
692-
693-
FIXME: rewrite with map
694686
*/
695687
fn to_upper(s: str) -> str {
696-
let outstr = "";
697-
chars_iter(s) { |c|
698-
push_char(outstr, char::to_upper(c));
699-
}
700-
ret outstr;
688+
map(s, char::to_upper)
701689
}
702690

703691
// FIXME: This is super-inefficient
@@ -1583,8 +1571,9 @@ mod tests {
15831571

15841572
#[test]
15851573
fn test_to_upper() {
1586-
// to_upper doesn't understand unicode yet,
1587-
// but we need to at least preserve it
1574+
// char::to_upper, and hence str::to_upper
1575+
// are culturally insensitive: I'm not sure they
1576+
// really work for anything but English ASCII, but YMMV
15881577

15891578
let unicode = "\u65e5\u672c";
15901579
let input = "abcDEF" + unicode + "xyz:.;";
@@ -1593,6 +1582,12 @@ mod tests {
15931582
assert (eq(expected, actual));
15941583
}
15951584

1585+
#[test]
1586+
fn test_to_lower() {
1587+
assert "" == map("", char::to_lower);
1588+
assert "ymca" == map("YMCA", char::to_lower);
1589+
}
1590+
15961591
#[test]
15971592
fn test_slice() {
15981593
assert (eq("ab", slice("abc", 0u, 2u)));

0 commit comments

Comments
 (0)