Re-implementing str::to_upper and str::to_lower using str::map
This commit is contained in:
parent
a8b657397a
commit
1f795ff3b0
1 changed files with 11 additions and 16 deletions
|
@ -674,30 +674,18 @@ fn windowed(nn: uint, ss: str) -> [str] {
|
||||||
Function: to_lower
|
Function: to_lower
|
||||||
|
|
||||||
Convert a string to lowercase
|
Convert a string to lowercase
|
||||||
|
|
||||||
FIXME: rewrite with map
|
|
||||||
*/
|
*/
|
||||||
fn to_lower(s: str) -> str {
|
fn to_lower(s: str) -> str {
|
||||||
let outstr = "";
|
map(s, char::to_lower)
|
||||||
chars_iter(s) { |c|
|
|
||||||
push_char(outstr, char::to_lower(c));
|
|
||||||
}
|
|
||||||
ret outstr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: to_upper
|
Function: to_upper
|
||||||
|
|
||||||
Convert a string to uppercase
|
Convert a string to uppercase
|
||||||
|
|
||||||
FIXME: rewrite with map
|
|
||||||
*/
|
*/
|
||||||
fn to_upper(s: str) -> str {
|
fn to_upper(s: str) -> str {
|
||||||
let outstr = "";
|
map(s, char::to_upper)
|
||||||
chars_iter(s) { |c|
|
|
||||||
push_char(outstr, char::to_upper(c));
|
|
||||||
}
|
|
||||||
ret outstr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: This is super-inefficient
|
// FIXME: This is super-inefficient
|
||||||
|
@ -1583,8 +1571,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_to_upper() {
|
fn test_to_upper() {
|
||||||
// to_upper doesn't understand unicode yet,
|
// char::to_upper, and hence str::to_upper
|
||||||
// but we need to at least preserve it
|
// are culturally insensitive: I'm not sure they
|
||||||
|
// really work for anything but English ASCII, but YMMV
|
||||||
|
|
||||||
let unicode = "\u65e5\u672c";
|
let unicode = "\u65e5\u672c";
|
||||||
let input = "abcDEF" + unicode + "xyz:.;";
|
let input = "abcDEF" + unicode + "xyz:.;";
|
||||||
|
@ -1593,6 +1582,12 @@ mod tests {
|
||||||
assert (eq(expected, actual));
|
assert (eq(expected, actual));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_to_lower() {
|
||||||
|
assert "" == map("", char::to_lower);
|
||||||
|
assert "ymca" == map("YMCA", char::to_lower);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_slice() {
|
fn test_slice() {
|
||||||
assert (eq("ab", slice("abc", 0u, 2u)));
|
assert (eq("ab", slice("abc", 0u, 2u)));
|
||||||
|
|
Loading…
Reference in a new issue