From 0eb552a835500ed964836a8e0e3111843bff8aa1 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Thu, 23 Jul 2015 12:24:27 +0200 Subject: wtf8, char: Replace uses of `mem::transmute` with more specific functions --- src/libcore/char.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 88aa805668c..84a0ed5ab3f 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -84,10 +84,17 @@ pub fn from_u32(i: u32) -> Option { 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] +pub unsafe fn from_u32_unchecked(i: u32) -> char { + transmute(i) +} + /// Converts a number to the character representing it. /// /// # Return value @@ -115,12 +122,11 @@ pub fn from_digit(num: u32, radix: u32) -> Option { 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 +324,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; -- cgit 1.4.1-3-g733a5 From c2fca7c95742cdd25198eae42d233d49db7026ea Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Fri, 24 Jul 2015 00:45:21 +0200 Subject: Add unstable attribute to `char::from_u32_unchecked` --- src/libcore/char.rs | 1 + src/libstd/lib.rs | 1 + 2 files changed, 2 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 84a0ed5ab3f..c6d0e97a0cd 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -91,6 +91,7 @@ pub fn from_u32(i: u32) -> Option { /// 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) } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 82bc1314ad5..03e61247a83 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -209,6 +209,7 @@ #![feature(borrow_state)] #![feature(box_raw)] #![feature(box_syntax)] +#![feature(char_from_unchecked)] #![feature(char_internals)] #![feature(clone_from_slice)] #![feature(collections)] -- cgit 1.4.1-3-g733a5