@@ -32,17 +32,18 @@ use core::str::next_code_point;
3232
3333use ascii:: * ;
3434use borrow:: Cow ;
35+ use char;
3536use cmp;
3637use fmt;
3738use hash:: { Hash , Hasher } ;
3839use iter:: FromIterator ;
3940use mem;
4041use ops;
42+ use rustc_unicode:: str:: { Utf16Item , utf16_items} ;
4143use slice;
4244use str;
4345use string:: String ;
4446use sys_common:: AsInner ;
45- use rustc_unicode:: str:: { Utf16Item , utf16_items} ;
4647use vec:: Vec ;
4748
4849const UTF8_REPLACEMENT_CHARACTER : & ' static [ u8 ] = b"\xEF \xBF \xBD " ;
@@ -107,7 +108,7 @@ impl CodePoint {
107108 pub fn to_char ( & self ) -> Option < char > {
108109 match self . value {
109110 0xD800 ... 0xDFFF => None ,
110- _ => Some ( unsafe { mem :: transmute ( self . value ) } )
111+ _ => Some ( unsafe { char :: from_u32_unchecked ( self . value ) } )
111112 }
112113 }
113114
@@ -213,18 +214,16 @@ impl Wtf8Buf {
213214 // Attempt to not use an intermediate buffer by just pushing bytes
214215 // directly onto this string.
215216 let slice = slice:: from_raw_parts_mut (
216- self . bytes . as_mut_ptr ( ) . offset ( cur_len as isize ) ,
217- 4
217+ self . bytes . as_mut_ptr ( ) . offset ( cur_len as isize ) , 4
218218 ) ;
219- let used = encode_utf8_raw ( code_point. value , mem:: transmute ( slice) )
220- . unwrap_or ( 0 ) ;
219+ let used = encode_utf8_raw ( code_point. value , slice) . unwrap ( ) ;
221220 self . bytes . set_len ( cur_len + used) ;
222221 }
223222 }
224223
225224 #[ inline]
226225 pub fn as_slice ( & self ) -> & Wtf8 {
227- unsafe { mem :: transmute ( & * self . bytes ) }
226+ unsafe { Wtf8 :: from_bytes_unchecked ( & self . bytes ) }
228227 }
229228
230229 /// Reserves capacity for at least `additional` more bytes to be inserted
@@ -457,7 +456,16 @@ impl Wtf8 {
457456 /// Since WTF-8 is a superset of UTF-8, this always succeeds.
458457 #[ inline]
459458 pub fn from_str ( value : & str ) -> & Wtf8 {
460- unsafe { mem:: transmute ( value. as_bytes ( ) ) }
459+ unsafe { Wtf8 :: from_bytes_unchecked ( value. as_bytes ( ) ) }
460+ }
461+
462+ /// Creates a WTF-8 slice from a WTF-8 byte slice.
463+ ///
464+ /// Since the byte slice is not checked for valid WTF-8, this functions is
465+ /// marked unsafe.
466+ #[ inline]
467+ unsafe fn from_bytes_unchecked ( value : & [ u8 ] ) -> & Wtf8 {
468+ mem:: transmute ( value)
461469 }
462470
463471 /// Returns the length, in WTF-8 bytes.
@@ -682,7 +690,7 @@ fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
682690#[ inline]
683691fn decode_surrogate_pair ( lead : u16 , trail : u16 ) -> char {
684692 let code_point = 0x10000 + ( ( ( ( lead - 0xD800 ) as u32 ) << 10 ) | ( trail - 0xDC00 ) as u32 ) ;
685- unsafe { mem :: transmute ( code_point) }
693+ unsafe { char :: from_u32_unchecked ( code_point) }
686694}
687695
688696/// Copied from core::str::StrPrelude::is_char_boundary
@@ -699,7 +707,7 @@ pub fn is_code_point_boundary(slice: &Wtf8, index: usize) -> bool {
699707#[ inline]
700708pub unsafe fn slice_unchecked ( s : & Wtf8 , begin : usize , end : usize ) -> & Wtf8 {
701709 // memory layout of an &[u8] and &Wtf8 are the same
702- mem :: transmute ( slice:: from_raw_parts (
710+ Wtf8 :: from_bytes_unchecked ( slice:: from_raw_parts (
703711 s. bytes . as_ptr ( ) . offset ( begin as isize ) ,
704712 end - begin
705713 ) )
@@ -821,7 +829,6 @@ mod tests {
821829 use prelude:: v1:: * ;
822830 use borrow:: Cow ;
823831 use super :: * ;
824- use mem:: transmute;
825832
826833 #[ test]
827834 fn code_point_from_u32 ( ) {
@@ -962,7 +969,7 @@ mod tests {
962969 string. push_wtf8 ( Wtf8 :: from_str ( " 💩" ) ) ;
963970 assert_eq ! ( string. bytes, b"a\xC3 \xA9 \xF0 \x9F \x92 \xA9 " ) ;
964971
965- fn w ( value : & [ u8 ] ) -> & Wtf8 { unsafe { transmute ( value ) } }
972+ fn w ( v : & [ u8 ] ) -> & Wtf8 { unsafe { Wtf8 :: from_bytes_unchecked ( v ) } }
966973
967974 let mut string = Wtf8Buf :: new ( ) ;
968975 string. push_wtf8 ( w ( b"\xED \xA0 \xBD " ) ) ; // lead
0 commit comments