"str": extract to_upper/lower_case() into "char"
This commit is contained in:
parent
8319b5a252
commit
f8d7a1c258
3 changed files with 63 additions and 15 deletions
|
@ -41,7 +41,7 @@ export is_alphabetic,
|
||||||
is_XID_start, is_XID_continue,
|
is_XID_start, is_XID_continue,
|
||||||
is_lowercase, is_uppercase,
|
is_lowercase, is_uppercase,
|
||||||
is_whitespace, is_alphanumeric,
|
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_alphabetic = unicode::derived_property::Alphabetic;
|
||||||
import is_XID_start = unicode::derived_property::XID_Start;
|
import is_XID_start = unicode::derived_property::XID_Start;
|
||||||
|
@ -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
|
Function: cmp
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,10 @@ String manipulation.
|
||||||
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
|
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
|
||||||
byte_len_range, index,
|
byte_len_range, index,
|
||||||
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
|
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
|
||||||
split_str, concat, connect, to_upper, replace, char_slice, trim_left,
|
split_str, concat, connect, to_lower, to_upper, replace, char_slice,
|
||||||
trim_right, trim, unshift_char, shift_char, pop_char, push_char,
|
trim_left, trim_right, trim, unshift_char, shift_char, pop_char,
|
||||||
is_utf8, from_chars, to_chars, char_len, char_len_range, char_at,
|
push_char, is_utf8, from_chars, to_chars, char_len, char_len_range,
|
||||||
bytes, is_ascii, shift_byte, pop_byte,
|
char_at, bytes, is_ascii, shift_byte, pop_byte,
|
||||||
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
|
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
|
||||||
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
|
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
|
||||||
contains, iter_chars, loop_chars, loop_chars_sub,
|
contains, iter_chars, loop_chars, loop_chars_sub,
|
||||||
|
@ -832,7 +832,18 @@ fn connect(v: [str], sep: str) -> str {
|
||||||
ret s;
|
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
|
Function: to_upper
|
||||||
|
|
||||||
|
@ -840,15 +851,8 @@ Convert a string to uppercase
|
||||||
*/
|
*/
|
||||||
fn to_upper(s: str) -> str {
|
fn to_upper(s: str) -> str {
|
||||||
let outstr = "";
|
let outstr = "";
|
||||||
let ascii_a = 'a' as u8;
|
iter_chars(s) { |c|
|
||||||
let ascii_z = 'z' as u8;
|
push_char(outstr, char::to_uppercase(c));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
ret outstr;
|
ret outstr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,3 +60,19 @@ fn test_to_digit_fail_1() {
|
||||||
fn test_to_digit_fail_2() {
|
fn test_to_digit_fail_2() {
|
||||||
char::to_digit('$');
|
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('ß') == 'ß');
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue