X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibcore%2Fchar.rs;h=9c12b3f68d3de71603f732bdb14a3448d88c554c;hb=31e21613eefcf30e6b7a10954bbbfc873d30ad88;hp=fbc444c2be3548640913fa6d194a45ff3c15f901;hpb=b5302217f026ba00e9f0514992b5f8226f7cb541;p=rust.git diff --git a/src/libcore/char.rs b/src/libcore/char.rs index fbc444c2be3..9c12b3f68d3 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -15,11 +15,11 @@ #![allow(non_snake_case)] #![doc(primitive = "char")] +use iter::Iterator; use mem::transmute; use ops::FnMut; -use option::Option; use option::Option::{None, Some}; -use iter::{range_step, Iterator, RangeStep}; +use option::Option; use slice::SliceExt; // UTF-8 ranges and tags for encoding characters @@ -141,7 +141,7 @@ pub fn to_digit(c: char, radix: uint) -> Option { #[unstable = "pending decisions about costructors for primitives"] pub fn from_digit(num: uint, radix: uint) -> Option { if radix > 36 { - panic!("from_digit: radix is to high (maximum 36)"); + panic!("from_digit: radix is too high (maximum 36)"); } if num < radix { unsafe { @@ -156,15 +156,7 @@ pub fn from_digit(num: uint, radix: uint) -> Option { } } -/// -/// Returns the hexadecimal Unicode escape of a `char` -/// -/// The rules are as follows: -/// -/// - chars in [0,0xff] get 2-digit escapes: `\\xNN` -/// - chars in [0x100,0xffff] get 4-digit escapes: `\\u{NNNN}` -/// - chars above 0x10000 get 8-digit escapes: `\\u{{NNN}NNNNN}` -/// +/// Deprecated, call the escape_unicode method instead. #[deprecated = "use the Char::escape_unicode method"] pub fn escape_unicode(c: char, mut f: F) where F: FnMut(char) { for char in c.escape_unicode() { @@ -172,18 +164,7 @@ pub fn escape_unicode(c: char, mut f: F) where F: FnMut(char) { } } -/// -/// Returns a 'default' ASCII and C++11-like literal escape of a `char` -/// -/// The default is chosen with a bias toward producing literals that are -/// legal in a variety of languages, including C++11 and similar C-family -/// languages. The exact rules are: -/// -/// - Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively. -/// - Single-quote, double-quote and backslash chars are backslash-escaped. -/// - Any other chars in the range [0x20,0x7e] are not escaped. -/// - Any other chars are given hex Unicode escapes; see `escape_unicode`. -/// +/// Deprecated, call the escape_default method instead. #[deprecated = "use the Char::escape_default method"] pub fn escape_default(c: char, mut f: F) where F: FnMut(char) { for c in c.escape_default() { @@ -267,13 +248,11 @@ pub trait Char { /// Returns an iterator that yields the hexadecimal Unicode escape /// of a character, as `char`s. /// - /// The rules are as follows: - /// - /// * Characters in [0,0xff] get 2-digit escapes: `\\xNN` - /// * Characters in [0x100,0xffff] get 4-digit escapes: `\\u{NNNN}`. - /// * Characters above 0x10000 get 8-digit escapes: `\\u{{NNN}NNNNN}`. + /// All characters are escaped with Rust syntax of the form `\\u{NNNN}` + /// where `NNNN` is the shortest hexadecimal representation of the code + /// point. #[unstable = "pending error conventions, trait organization"] - fn escape_unicode(self) -> UnicodeEscapedChars; + fn escape_unicode(self) -> EscapeUnicode; /// Returns an iterator that yields the 'default' ASCII and /// C++11-like literal escape of a character, as `char`s. @@ -288,7 +267,7 @@ pub trait Char { /// * Any other chars in the range [0x20,0x7e] are not escaped. /// * Any other chars are given hex Unicode escapes; see `escape_unicode`. #[unstable = "pending error conventions, trait organization"] - fn escape_default(self) -> DefaultEscapedChars; + fn escape_default(self) -> EscapeDefault; /// Returns the amount of bytes this character would need if encoded in /// UTF-8. @@ -358,23 +337,23 @@ fn from_digit(num: uint, radix: uint) -> Option { from_digit(num, radix) } fn from_u32(i: u32) -> Option { from_u32(i) } #[unstable = "pending error conventions, trait organization"] - fn escape_unicode(self) -> UnicodeEscapedChars { - UnicodeEscapedChars { c: self, state: UnicodeEscapedCharsState::Backslash } + fn escape_unicode(self) -> EscapeUnicode { + EscapeUnicode { c: self, state: EscapeUnicodeState::Backslash } } #[unstable = "pending error conventions, trait organization"] - fn escape_default(self) -> DefaultEscapedChars { + fn escape_default(self) -> EscapeDefault { let init_state = match self { - '\t' => DefaultEscapedCharsState::Backslash('t'), - '\r' => DefaultEscapedCharsState::Backslash('r'), - '\n' => DefaultEscapedCharsState::Backslash('n'), - '\\' => DefaultEscapedCharsState::Backslash('\\'), - '\'' => DefaultEscapedCharsState::Backslash('\''), - '"' => DefaultEscapedCharsState::Backslash('"'), - '\x20' ... '\x7e' => DefaultEscapedCharsState::Char(self), - _ => DefaultEscapedCharsState::Unicode(self.escape_unicode()) + '\t' => EscapeDefaultState::Backslash('t'), + '\r' => EscapeDefaultState::Backslash('r'), + '\n' => EscapeDefaultState::Backslash('n'), + '\\' => EscapeDefaultState::Backslash('\\'), + '\'' => EscapeDefaultState::Backslash('\''), + '"' => EscapeDefaultState::Backslash('"'), + '\x20' ... '\x7e' => EscapeDefaultState::Char(self), + _ => EscapeDefaultState::Unicode(self.escape_unicode()) }; - DefaultEscapedChars { state: init_state } + EscapeDefault { state: init_state } } #[inline] @@ -451,72 +430,86 @@ fn encode_utf16(&self, dst: &mut [u16]) -> Option { /// An iterator over the characters that represent a `char`, as escaped by /// Rust's unicode escaping rules. -pub struct UnicodeEscapedChars { +pub struct EscapeUnicode { c: char, - state: UnicodeEscapedCharsState + state: EscapeUnicodeState } -enum UnicodeEscapedCharsState { +enum EscapeUnicodeState { Backslash, Type, - Value(RangeStep), + LeftBrace, + Value(uint), + RightBrace, + Done, } -impl Iterator for UnicodeEscapedChars { +impl Iterator for EscapeUnicode { fn next(&mut self) -> Option { match self.state { - UnicodeEscapedCharsState::Backslash => { - self.state = UnicodeEscapedCharsState::Type; + EscapeUnicodeState::Backslash => { + self.state = EscapeUnicodeState::Type; Some('\\') } - UnicodeEscapedCharsState::Type => { - let (typechar, pad) = if self.c <= '\x7f' { ('x', 2) } - else if self.c <= '\u{ffff}' { ('u', 4) } - else { ('U', 8) }; - self.state = UnicodeEscapedCharsState::Value(range_step(4 * (pad - 1), -1, -4i32)); - Some(typechar) + EscapeUnicodeState::Type => { + self.state = EscapeUnicodeState::LeftBrace; + Some('u') } - UnicodeEscapedCharsState::Value(ref mut range_step) => match range_step.next() { - Some(offset) => { - let offset = offset as uint; - let v = match ((self.c as i32) >> offset) & 0xf { - i @ 0 ... 9 => '0' as i32 + i, - i => 'a' as i32 + (i - 10) - }; - Some(unsafe { transmute(v) }) + EscapeUnicodeState::LeftBrace => { + let mut n = 0u; + while (self.c as u32) >> (4 * (n + 1)) != 0 { + n += 1; } - None => None + self.state = EscapeUnicodeState::Value(n); + Some('{') + } + EscapeUnicodeState::Value(offset) => { + let v = match ((self.c as i32) >> (offset * 4)) & 0xf { + i @ 0 ... 9 => '0' as i32 + i, + i => 'a' as i32 + (i - 10) + }; + if offset == 0 { + self.state = EscapeUnicodeState::RightBrace; + } else { + self.state = EscapeUnicodeState::Value(offset - 1); + } + Some(unsafe { transmute(v) }) + } + EscapeUnicodeState::RightBrace => { + self.state = EscapeUnicodeState::Done; + Some('}') } + EscapeUnicodeState::Done => None, } } } /// An iterator over the characters that represent a `char`, escaped /// for maximum portability. -pub struct DefaultEscapedChars { - state: DefaultEscapedCharsState +pub struct EscapeDefault { + state: EscapeDefaultState } -enum DefaultEscapedCharsState { +enum EscapeDefaultState { Backslash(char), Char(char), Done, - Unicode(UnicodeEscapedChars), + Unicode(EscapeUnicode), } -impl Iterator for DefaultEscapedChars { +impl Iterator for EscapeDefault { fn next(&mut self) -> Option { match self.state { - DefaultEscapedCharsState::Backslash(c) => { - self.state = DefaultEscapedCharsState::Char(c); + EscapeDefaultState::Backslash(c) => { + self.state = EscapeDefaultState::Char(c); Some('\\') } - DefaultEscapedCharsState::Char(c) => { - self.state = DefaultEscapedCharsState::Done; + EscapeDefaultState::Char(c) => { + self.state = EscapeDefaultState::Done; Some(c) } - DefaultEscapedCharsState::Done => None, - DefaultEscapedCharsState::Unicode(ref mut iter) => iter.next() + EscapeDefaultState::Done => None, + EscapeDefaultState::Unicode(ref mut iter) => iter.next() } } }