Skip to content

Commit

Permalink
Cleaned up case related functions a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimundi committed Apr 20, 2013
1 parent ae3b869 commit 276293a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
18 changes: 16 additions & 2 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -234,6 +234,21 @@ pub fn escape_default(c: char) -> ~str {
}
}

/// Returns the amount of bytes this character would need if encoded in utf8
pub fn len_utf8_bytes(c: char) -> uint {
static max_one_b: uint = 128u;
static max_two_b: uint = 2048u;
static max_three_b: uint = 65536u;
static max_four_b: uint = 2097152u;

let code = c as uint;
if code < max_one_b { 1u }
else if code < max_two_b { 2u }
else if code < max_three_b { 3u }
else if code < max_four_b { 4u }
else { fail!(~"invalid character!") }
}
/**
* Compare two chars
*
Expand Down Expand Up @@ -334,7 +349,6 @@ fn test_escape_default() {
assert_eq!(escape_default('\U0001d4b6'), ~"\\U0001d4b6");
}
#[test]
fn test_escape_unicode() {
assert_eq!(escape_unicode('\x00'), ~"\\x00");
Expand Down
41 changes: 19 additions & 22 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -789,16 +789,18 @@ pub fn each_split_within<'a>(ss: &'a str,

/// Convert a string to lowercase. ASCII only
pub fn to_lower(s: &str) -> ~str {
map(s,
|c| unsafe{(libc::tolower(c as libc::c_char)) as char}
)
do map(s) |c| {
assert!(char::is_ascii(c));
(unsafe{libc::tolower(c as libc::c_char)}) as char
}
}

/// Convert a string to uppercase. ASCII only
pub fn to_upper(s: &str) -> ~str {
map(s,
|c| unsafe{(libc::toupper(c as libc::c_char)) as char}
)
do map(s) |c| {
assert!(char::is_ascii(c));
(unsafe{libc::toupper(c as libc::c_char)}) as char
}
}

/**
Expand Down Expand Up @@ -3096,12 +3098,11 @@ mod tests {
#[test]
fn test_to_lower() {
unsafe {
assert!(~"" == map(~"",
|c| libc::tolower(c as c_char) as char));
assert!(~"ymca" == map(~"YMCA",
|c| libc::tolower(c as c_char) as char));
}
// libc::tolower, and hence str::to_lower
// are culturally insensitive: they only work for ASCII
// (see Issue #1347)
assert!(~"" == to_lower(""));
assert!(~"ymca" == to_lower("YMCA"));
}
#[test]
Expand Down Expand Up @@ -3666,12 +3667,8 @@ mod tests {

#[test]
fn test_map() {
unsafe {
assert!(~"" == map(~"", |c|
libc::toupper(c as c_char) as char));
assert!(~"YMCA" == map(~"ymca",
|c| libc::toupper(c as c_char) as char));
}
assert!(~"" == map(~"", |c| unsafe {libc::toupper(c as c_char)} as char));
assert!(~"YMCA" == map(~"ymca", |c| unsafe {libc::toupper(c as c_char)} as char));
}
#[test]
Expand All @@ -3685,11 +3682,11 @@ mod tests {
#[test]
fn test_any() {
assert!(false == any(~"", char::is_uppercase));
assert!(false == any(~"", char::is_uppercase));
assert!(false == any(~"ymca", char::is_uppercase));
assert!(true == any(~"YMCA", char::is_uppercase));
assert!(true == any(~"yMCA", char::is_uppercase));
assert!(true == any(~"Ymcy", char::is_uppercase));
assert!(true == any(~"yMCA", char::is_uppercase));
assert!(true == any(~"Ymcy", char::is_uppercase));
}
#[test]
Expand Down

0 comments on commit 276293a

Please sign in to comment.