diff options
| author | bors <bors@rust-lang.org> | 2015-07-25 04:25:33 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-07-25 04:25:33 +0000 |
| commit | e4f044662be44f18d163db5fec4cde09c6d4342c (patch) | |
| tree | 95c1f0a406e56a7e57bd0dad244bc6cbaf3ad6bb /src/libcore | |
| parent | d38e8a05b533956a3c8d976c9bb137c50f017113 (diff) | |
| parent | c2fca7c95742cdd25198eae42d233d49db7026ea (diff) | |
| download | rust-e4f044662be44f18d163db5fec4cde09c6d4342c.tar.gz rust-e4f044662be44f18d163db5fec4cde09c6d4342c.zip | |
Auto merge of #27233 - tbu-:pr_wtf8, r=alexcrichton
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/char.rs | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 88aa805668c..c6d0e97a0cd 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -84,10 +84,18 @@ pub fn from_u32(i: u32) -> Option<char> { if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) { None } else { - Some(unsafe { transmute(i) }) + Some(unsafe { from_u32_unchecked(i) }) } } +/// Converts a `u32` to an `char`, not checking whether it is a valid unicode +/// codepoint. +#[inline] +#[unstable(feature = "char_from_unchecked", reason = "recently added API")] +pub unsafe fn from_u32_unchecked(i: u32) -> char { + transmute(i) +} + /// Converts a number to the character representing it. /// /// # Return value @@ -115,12 +123,11 @@ pub fn from_digit(num: u32, radix: u32) -> Option<char> { panic!("from_digit: radix is too high (maximum 36)"); } if num < radix { - unsafe { - if num < 10 { - Some(transmute('0' as u32 + num)) - } else { - Some(transmute('a' as u32 + num - 10)) - } + let num = num as u8; + if num < 10 { + Some((b'0' + num) as char) + } else { + Some((b'a' + num - 10) as char) } } else { None @@ -318,16 +325,13 @@ impl Iterator for EscapeUnicode { 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) - }; + let c = from_digit(((self.c as u32) >> (offset * 4)) & 0xf, 16).unwrap(); if offset == 0 { self.state = EscapeUnicodeState::RightBrace; } else { self.state = EscapeUnicodeState::Value(offset - 1); } - Some(unsafe { transmute(v) }) + Some(c) } EscapeUnicodeState::RightBrace => { self.state = EscapeUnicodeState::Done; |
