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

This commit is contained in:
Lenny222 2012-01-01 17:30:12 +01:00 committed by Brian Anderson
parent 8319b5a252
commit f8d7a1c258
3 changed files with 63 additions and 15 deletions

View file

@ -41,7 +41,7 @@
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;
@ -136,6 +136,34 @@
}
}
/*
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

View file

@ -7,10 +7,10 @@
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,
@ -832,7 +832,18 @@ 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
@ -840,15 +851,8 @@ fn connect(v: [str], sep: str) -> str {
*/
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;
}

View file

@ -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('ß') == 'ß');
}