Skip to content

"str": extract to_upper/lower_case() into "char" #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export is_alphabetic,
is_XID_start, is_XID_continue,
is_lowercase, is_uppercase,
is_whitespace, is_alphanumeric,
to_digit, maybe_digit, cmp;
to_digit, to_lowercase, to_uppercase, maybe_digit, cmp;

import is_alphabetic = unicode::derived_property::Alphabetic;
import is_XID_start = unicode::derived_property::XID_Start;
Expand Down Expand Up @@ -136,6 +136,34 @@ pure fn maybe_digit(c: char) -> option::t<u8> {
}
}

/*
Function: to_lowercase

Convert a char to the corresponding lower case.

FIXME: works only on ASCII
*/
pure fn to_lowercase(c: char) -> char {
alt c {
'A' to 'Z' { ((c as u8) + 32u8) as char }
_ { c }
}
}

/*
Function: to_uppercase

Convert a char to the corresponding upper case.

FIXME: works only on ASCII
*/
pure fn to_uppercase(c: char) -> char {
alt c {
'a' to 'z' { ((c as u8) - 32u8) as char }
_ { c }
}
}

/*
Function: cmp

Expand Down
32 changes: 18 additions & 14 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ String manipulation.
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
byte_len_range, index,
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
split_str, concat, connect, to_upper, replace, char_slice, trim_left,
trim_right, trim, unshift_char, shift_char, pop_char, push_char,
is_utf8, from_chars, to_chars, char_len, char_len_range, char_at,
bytes, is_ascii, shift_byte, pop_byte,
split_str, concat, connect, to_lower, to_upper, replace, char_slice,
trim_left, trim_right, trim, unshift_char, shift_char, pop_char,
push_char, is_utf8, from_chars, to_chars, char_len, char_len_range,
char_at, bytes, is_ascii, shift_byte, pop_byte,
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
contains, iter_chars, loop_chars, loop_chars_sub,
Expand Down Expand Up @@ -832,23 +832,27 @@ fn connect(v: [str], sep: str) -> str {
ret s;
}

// FIXME: This only handles ASCII
/*
Function: to_lower

Convert a string to lowercase
*/
fn to_lower(s: str) -> str {
let outstr = "";
iter_chars(s) { |c|
push_char(outstr, char::to_lowercase(c));
}
ret outstr;
}
/*
Function: to_upper

Convert a string to uppercase
*/
fn to_upper(s: str) -> str {
let outstr = "";
let ascii_a = 'a' as u8;
let ascii_z = 'z' as u8;
let diff = 32u8;
for byte: u8 in s {
let next;
if ascii_a <= byte && byte <= ascii_z {
next = byte - diff;
} else { next = byte; }
push_byte(outstr, next);
iter_chars(s) { |c|
push_char(outstr, char::to_uppercase(c));
}
ret outstr;
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/stdtest/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ fn test_to_digit_fail_1() {
fn test_to_digit_fail_2() {
char::to_digit('$');
}

#[test]
fn test_to_lowercase() {
assert (char::to_lowercase('H') == 'h');
assert (char::to_lowercase('e') == 'e');
//assert (char::to_lowercase('Ö') == 'ö');
assert (char::to_lowercase('ß') == 'ß');
}

#[test]
fn test_to_uppercase() {
assert (char::to_uppercase('l') == 'L');
assert (char::to_uppercase('Q') == 'Q');
//assert (char::to_uppercase('ü') == 'Ü');
assert (char::to_uppercase('ß') == 'ß');
}