From 1f795ff3b0c0cac31c1d9fd2406d0d53e774683a Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Mon, 30 Jan 2012 22:26:42 -0800 Subject: [PATCH] Re-implementing str::to_upper and str::to_lower using str::map --- src/libcore/str.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1c7fe1a9ced..9f40dd5c3a4 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -674,30 +674,18 @@ fn windowed(nn: uint, ss: str) -> [str] { Function: to_lower Convert a string to lowercase - -FIXME: rewrite with map */ fn to_lower(s: str) -> str { - let outstr = ""; - chars_iter(s) { |c| - push_char(outstr, char::to_lower(c)); - } - ret outstr; + map(s, char::to_lower) } /* Function: to_upper Convert a string to uppercase - -FIXME: rewrite with map */ fn to_upper(s: str) -> str { - let outstr = ""; - chars_iter(s) { |c| - push_char(outstr, char::to_upper(c)); - } - ret outstr; + map(s, char::to_upper) } // FIXME: This is super-inefficient @@ -1583,8 +1571,9 @@ mod tests { #[test] fn test_to_upper() { - // to_upper doesn't understand unicode yet, - // but we need to at least preserve it + // char::to_upper, and hence str::to_upper + // are culturally insensitive: I'm not sure they + // really work for anything but English ASCII, but YMMV let unicode = "\u65e5\u672c"; let input = "abcDEF" + unicode + "xyz:.;"; @@ -1593,6 +1582,12 @@ mod tests { assert (eq(expected, actual)); } + #[test] + fn test_to_lower() { + assert "" == map("", char::to_lower); + assert "ymca" == map("YMCA", char::to_lower); + } + #[test] fn test_slice() { assert (eq("ab", slice("abc", 0u, 2u)));