diff options
| author | bors <bors@rust-lang.org> | 2013-04-24 13:33:29 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-04-24 13:33:29 -0700 |
| commit | ee3789b4e434d90661601f00867da4980a74a676 (patch) | |
| tree | fc406c99510acbf1fdeb9da2e25d3a38feb92fad /src/libcore | |
| parent | e26f992d5e199a1ff8c26733650d254d63be066a (diff) | |
| parent | 3759b5711d1f34a92ef51abd069f074fb55b3b5b (diff) | |
| download | rust-ee3789b4e434d90661601f00867da4980a74a676.tar.gz rust-ee3789b4e434d90661601f00867da4980a74a676.zip | |
auto merge of #6029 : Kimundi/rust/ascii-encoding, r=thestinger
Replaced {str, char, u8}::is_ascii
Replaced str::to_lower and str::to_upper
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/char.rs | 16 | ||||
| -rw-r--r-- | src/libcore/num/uint-template/u8.rs | 7 | ||||
| -rw-r--r-- | src/libcore/path.rs | 10 | ||||
| -rw-r--r-- | src/libcore/str.rs | 60 | ||||
| -rw-r--r-- | src/libcore/str/ascii.rs | 13 | ||||
| -rw-r--r-- | src/libcore/unstable/extfmt.rs | 8 |
6 files changed, 30 insertions, 84 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs index c07a31490c3..8af61dcb861 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -100,12 +100,6 @@ pub fn is_alphanumeric(c: char) -> bool { unicode::general_category::No(c); } -/// Indicates whether the character is an ASCII character -#[inline(always)] -pub fn is_ascii(c: char) -> bool { - c - ('\x7F' & c) == '\x00' -} - /// Indicates whether the character is numeric (Nd, Nl, or No) #[inline(always)] pub fn is_digit(c: char) -> bool { @@ -116,7 +110,7 @@ pub fn is_digit(c: char) -> bool { /** * Checks if a character parses as a numeric digit in the given radix. - * Compared to `is_digit()`, this function only recognizes the ascii + * Compared to `is_digit()`, this function only recognizes the * characters `0-9`, `a-z` and `A-Z`. * * Returns `true` if `c` is a valid digit under `radix`, and `false` @@ -163,7 +157,7 @@ pub fn to_digit(c: char, radix: uint) -> Option<uint> { } /** - * Converts a number to the ascii character representing it. + * Converts a number to the character representing it. * * Returns `Some(char)` if `num` represents one digit under `radix`, * using one character of `0-9` or `a-z`, or `None` if it doesn't. @@ -317,12 +311,6 @@ fn test_to_digit() { } #[test] -fn test_is_ascii() { - assert!(str::all(~"banana", is_ascii)); - assert!(! str::all(~"ประเทศไทย中华Việt Nam", is_ascii)); -} - -#[test] fn test_is_digit() { assert!(is_digit('2')); assert!(is_digit('7')); diff --git a/src/libcore/num/uint-template/u8.rs b/src/libcore/num/uint-template/u8.rs index ce23bebacda..5c548d72093 100644 --- a/src/libcore/num/uint-template/u8.rs +++ b/src/libcore/num/uint-template/u8.rs @@ -10,16 +10,9 @@ //! Operations and constants for `u8` -pub use self::inst::is_ascii; - mod inst { pub type T = u8; #[allow(non_camel_case_types)] pub type T_SIGNED = i8; pub static bits: uint = 8; - - // Type-specific functions here. These must be reexported by the - // parent module so that they appear in core::u8 and not core::u8::u8; - - pub fn is_ascii(x: T) -> bool { return 0 as T == x & 128 as T; } } diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 8328d42c35e..edc61299af9 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -19,6 +19,7 @@ use libc; use option::{None, Option, Some}; use str; use to_str::ToStr; +use ascii::{AsciiCast, AsciiStr}; #[deriving(Clone, Eq)] pub struct WindowsPath { @@ -753,7 +754,9 @@ impl GenericPath for WindowsPath { fn is_restricted(&self) -> bool { match self.filestem() { Some(stem) => { - match stem.to_lower() { + // FIXME: #4318 Instead of to_ascii and to_str_ascii, could use + // to_ascii_consume and to_str_consume to not do a unnecessary copy. + match stem.to_ascii().to_lower().to_str_ascii() { ~"con" | ~"aux" | ~"com1" | ~"com2" | ~"com3" | ~"com4" | ~"lpt1" | ~"lpt2" | ~"lpt3" | ~"prn" | ~"nul" => true, _ => false @@ -809,7 +812,10 @@ impl GenericPath for WindowsPath { host: copy self.host, device: match self.device { None => None, - Some(ref device) => Some(device.to_upper()) + + // FIXME: #4318 Instead of to_ascii and to_str_ascii, could use + // to_ascii_consume and to_str_consume to not do a unnecessary copy. + Some(ref device) => Some(device.to_ascii().to_upper().to_str_ascii()) }, is_absolute: self.is_absolute, components: normalize(self.components) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 9590f148e30..92c965256ce 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -27,7 +27,6 @@ use option::{None, Option, Some}; use iterator::Iterator; use ptr; use str; -use u8; use uint; use vec; use to_str::ToStr; @@ -787,22 +786,6 @@ pub fn each_split_within<'a>(ss: &'a str, } } -/// Convert a string to lowercase. ASCII only -pub fn to_lower(s: &str) -> ~str { - 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 { - do map(s) |c| { - assert!(char::is_ascii(c)); - (unsafe{libc::toupper(c as libc::c_char)}) as char - } -} - /** * Replace all occurrences of one string with another * @@ -1610,13 +1593,6 @@ pub fn ends_with<'a,'b>(haystack: &'a str, needle: &'b str) -> bool { Section: String properties */ -/// Determines if a string contains only ASCII characters -pub fn is_ascii(s: &str) -> bool { - let mut i: uint = len(s); - while i > 0u { i -= 1u; if !u8::is_ascii(s[i]) { return false; } } - return true; -} - /// Returns true if the string has length 0 pub fn is_empty(s: &str) -> bool { len(s) == 0u } @@ -2403,8 +2379,6 @@ pub trait StrSlice<'self> { fn each_split_str<'a>(&self, sep: &'a str, it: &fn(&'self str) -> bool); fn starts_with<'a>(&self, needle: &'a str) -> bool; fn substr(&self, begin: uint, n: uint) -> &'self str; - fn to_lower(&self) -> ~str; - fn to_upper(&self) -> ~str; fn escape_default(&self) -> ~str; fn escape_unicode(&self) -> ~str; fn trim(&self) -> &'self str; @@ -2565,12 +2539,6 @@ impl<'self> StrSlice<'self> for &'self str { fn substr(&self, begin: uint, n: uint) -> &'self str { substr(*self, begin, n) } - /// Convert a string to lowercase - #[inline] - fn to_lower(&self) -> ~str { to_lower(*self) } - /// Convert a string to uppercase - #[inline] - fn to_upper(&self) -> ~str { to_upper(*self) } /// Escape each char in `s` with char::escape_default. #[inline] fn escape_default(&self) -> ~str { escape_default(*self) } @@ -3085,27 +3053,6 @@ mod tests { } #[test] - fn test_to_upper() { - // libc::toupper, and hence str::to_upper - // are culturally insensitive: they only work for ASCII - // (see Issue #1347) - let unicode = ~""; //"\u65e5\u672c"; // uncomment once non-ASCII works - let input = ~"abcDEF" + unicode + ~"xyz:.;"; - let expected = ~"ABCDEF" + unicode + ~"XYZ:.;"; - let actual = to_upper(input); - assert!(expected == actual); - } - - #[test] - fn test_to_lower() { - // 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] fn test_unsafe_slice() { assert!("ab" == unsafe {raw::slice_bytes("abc", 0, 2)}); assert!("bc" == unsafe {raw::slice_bytes("abc", 1, 3)}); @@ -3338,13 +3285,6 @@ mod tests { } #[test] - fn test_is_ascii() { - assert!((is_ascii(~""))); - assert!((is_ascii(~"a"))); - assert!((!is_ascii(~"\u2009"))); - } - - #[test] fn test_shift_byte() { let mut s = ~"ABC"; let b = unsafe{raw::shift_byte(&mut s)}; diff --git a/src/libcore/str/ascii.rs b/src/libcore/str/ascii.rs index f6c0176eafc..9180c995ca2 100644 --- a/src/libcore/str/ascii.rs +++ b/src/libcore/str/ascii.rs @@ -199,6 +199,7 @@ impl ToStrConsume for ~[Ascii] { #[cfg(test)] mod tests { use super::*; + use str; macro_rules! v2ascii ( ( [$($e:expr),*]) => ( [$(Ascii{chr:$e}),*]); @@ -221,6 +222,9 @@ mod tests { assert_eq!('['.to_ascii().to_lower().to_char(), '['); assert_eq!('`'.to_ascii().to_upper().to_char(), '`'); assert_eq!('{'.to_ascii().to_upper().to_char(), '{'); + + assert!(str::all(~"banana", |c| c.is_ascii())); + assert!(! str::all(~"ประเทศไทย中华Việt Nam", |c| c.is_ascii())); } #[test] @@ -234,6 +238,15 @@ mod tests { assert_eq!("abCDef&?#".to_ascii().to_lower().to_str_ascii(), ~"abcdef&?#"); assert_eq!("abCDef&?#".to_ascii().to_upper().to_str_ascii(), ~"ABCDEF&?#"); + + assert_eq!("".to_ascii().to_lower().to_str_ascii(), ~""); + assert_eq!("YMCA".to_ascii().to_lower().to_str_ascii(), ~"ymca"); + assert_eq!("abcDEFxyz:.;".to_ascii().to_upper().to_str_ascii(), ~"ABCDEFXYZ:.;"); + + assert!("".is_ascii()); + assert!("a".is_ascii()); + assert!(!"\u2009".is_ascii()); + } #[test] diff --git a/src/libcore/unstable/extfmt.rs b/src/libcore/unstable/extfmt.rs index ee33e2ed20b..b812be5575a 100644 --- a/src/libcore/unstable/extfmt.rs +++ b/src/libcore/unstable/extfmt.rs @@ -520,7 +520,13 @@ pub mod rt { match cv.ty { TyDefault => uint_to_str_prec(u, 10, prec), TyHexLower => uint_to_str_prec(u, 16, prec), - TyHexUpper => str::to_upper(uint_to_str_prec(u, 16, prec)), + + // FIXME: #4318 Instead of to_ascii and to_str_ascii, could use + // to_ascii_consume and to_str_consume to not do a unnecessary copy. + TyHexUpper => { + let s = uint_to_str_prec(u, 16, prec); + s.to_ascii().to_upper().to_str_ascii() + } TyBits => uint_to_str_prec(u, 2, prec), TyOctal => uint_to_str_prec(u, 8, prec) }; |
