Skip to content

Commit b9a0858

Browse files
committed
---
yaml --- r: 6982 b: refs/heads/master c: f0e9869 h: refs/heads/master v: v3
1 parent bb55b2c commit b9a0858

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: eba891e989bd8e051e853b1760824013ce23da3a
2+
refs/heads/master: f0e98691dbf88d0fac7ae04d131e9059d0335527

trunk/src/libcore/char.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export is_alphabetic,
4141
is_XID_start, is_XID_continue,
4242
is_lowercase, is_uppercase,
4343
is_whitespace, is_alphanumeric,
44-
to_digit, maybe_digit, cmp;
44+
to_digit, to_lowercase, to_uppercase, maybe_digit, cmp;
4545

4646
import is_alphabetic = unicode::derived_property::Alphabetic;
4747
import is_XID_start = unicode::derived_property::XID_Start;
@@ -136,6 +136,34 @@ pure fn maybe_digit(c: char) -> option::t<u8> {
136136
}
137137
}
138138

139+
/*
140+
Function: to_lowercase
141+
142+
Convert a char to the corresponding lower case.
143+
144+
FIXME: works only on ASCII
145+
*/
146+
pure fn to_lowercase(c: char) -> char {
147+
alt c {
148+
'A' to 'Z' { ((c as u8) + 32u8) as char }
149+
_ { c }
150+
}
151+
}
152+
153+
/*
154+
Function: to_uppercase
155+
156+
Convert a char to the corresponding upper case.
157+
158+
FIXME: works only on ASCII
159+
*/
160+
pure fn to_uppercase(c: char) -> char {
161+
alt c {
162+
'a' to 'z' { ((c as u8) - 32u8) as char }
163+
_ { c }
164+
}
165+
}
166+
139167
/*
140168
Function: cmp
141169

trunk/src/libcore/str.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ String manipulation.
77
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
88
byte_len_range, index,
99
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
10-
split_str, concat, connect, to_upper, replace, char_slice, trim_left,
11-
trim_right, trim, unshift_char, shift_char, pop_char, push_char,
12-
is_utf8, from_chars, to_chars, char_len, char_len_range, char_at,
13-
bytes, is_ascii, shift_byte, pop_byte,
10+
split_str, concat, connect, to_lower, to_upper, replace, char_slice,
11+
trim_left, trim_right, trim, unshift_char, shift_char, pop_char,
12+
push_char, is_utf8, from_chars, to_chars, char_len, char_len_range,
13+
char_at, bytes, is_ascii, shift_byte, pop_byte,
1414
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
1515
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
1616
contains, iter_chars, loop_chars, loop_chars_sub,
@@ -832,23 +832,27 @@ fn connect(v: [str], sep: str) -> str {
832832
ret s;
833833
}
834834

835-
// FIXME: This only handles ASCII
835+
/*
836+
Function: to_lower
837+
838+
Convert a string to lowercase
839+
*/
840+
fn to_lower(s: str) -> str {
841+
let outstr = "";
842+
iter_chars(s) { |c|
843+
push_char(outstr, char::to_lowercase(c));
844+
}
845+
ret outstr;
846+
}
836847
/*
837848
Function: to_upper
838849
839850
Convert a string to uppercase
840851
*/
841852
fn to_upper(s: str) -> str {
842853
let outstr = "";
843-
let ascii_a = 'a' as u8;
844-
let ascii_z = 'z' as u8;
845-
let diff = 32u8;
846-
for byte: u8 in s {
847-
let next;
848-
if ascii_a <= byte && byte <= ascii_z {
849-
next = byte - diff;
850-
} else { next = byte; }
851-
push_byte(outstr, next);
854+
iter_chars(s) { |c|
855+
push_char(outstr, char::to_uppercase(c));
852856
}
853857
ret outstr;
854858
}

trunk/src/test/stdtest/char.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,19 @@ fn test_to_digit_fail_1() {
6060
fn test_to_digit_fail_2() {
6161
char::to_digit('$');
6262
}
63+
64+
#[test]
65+
fn test_to_lowercase() {
66+
assert (char::to_lowercase('H') == 'h');
67+
assert (char::to_lowercase('e') == 'e');
68+
//assert (char::to_lowercase('Ö') == 'ö');
69+
assert (char::to_lowercase('ß') == 'ß');
70+
}
71+
72+
#[test]
73+
fn test_to_uppercase() {
74+
assert (char::to_uppercase('l') == 'L');
75+
assert (char::to_uppercase('Q') == 'Q');
76+
//assert (char::to_uppercase('ü') == 'Ü');
77+
assert (char::to_uppercase('ß') == 'ß');
78+
}

0 commit comments

Comments
 (0)