@@ -32,17 +32,18 @@ use core::str::next_code_point;
32
32
33
33
use ascii:: * ;
34
34
use borrow:: Cow ;
35
+ use char;
35
36
use cmp;
36
37
use fmt;
37
38
use hash:: { Hash , Hasher } ;
38
39
use iter:: FromIterator ;
39
40
use mem;
40
41
use ops;
42
+ use rustc_unicode:: str:: { Utf16Item , utf16_items} ;
41
43
use slice;
42
44
use str;
43
45
use string:: String ;
44
46
use sys_common:: AsInner ;
45
- use rustc_unicode:: str:: { Utf16Item , utf16_items} ;
46
47
use vec:: Vec ;
47
48
48
49
const UTF8_REPLACEMENT_CHARACTER : & ' static [ u8 ] = b"\xEF \xBF \xBD " ;
@@ -107,7 +108,7 @@ impl CodePoint {
107
108
pub fn to_char ( & self ) -> Option < char > {
108
109
match self . value {
109
110
0xD800 ... 0xDFFF => None ,
110
- _ => Some ( unsafe { mem :: transmute ( self . value ) } )
111
+ _ => Some ( unsafe { char :: from_u32_unchecked ( self . value ) } )
111
112
}
112
113
}
113
114
@@ -213,18 +214,16 @@ impl Wtf8Buf {
213
214
// Attempt to not use an intermediate buffer by just pushing bytes
214
215
// directly onto this string.
215
216
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
218
218
) ;
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 ( ) ;
221
220
self . bytes . set_len ( cur_len + used) ;
222
221
}
223
222
}
224
223
225
224
#[ inline]
226
225
pub fn as_slice ( & self ) -> & Wtf8 {
227
- unsafe { mem :: transmute ( & * self . bytes ) }
226
+ unsafe { Wtf8 :: from_bytes_unchecked ( & self . bytes ) }
228
227
}
229
228
230
229
/// Reserves capacity for at least `additional` more bytes to be inserted
@@ -457,7 +456,16 @@ impl Wtf8 {
457
456
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
458
457
#[ inline]
459
458
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)
461
469
}
462
470
463
471
/// Returns the length, in WTF-8 bytes.
@@ -682,7 +690,7 @@ fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
682
690
#[ inline]
683
691
fn decode_surrogate_pair ( lead : u16 , trail : u16 ) -> char {
684
692
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) }
686
694
}
687
695
688
696
/// Copied from core::str::StrPrelude::is_char_boundary
@@ -699,7 +707,7 @@ pub fn is_code_point_boundary(slice: &Wtf8, index: usize) -> bool {
699
707
#[ inline]
700
708
pub unsafe fn slice_unchecked ( s : & Wtf8 , begin : usize , end : usize ) -> & Wtf8 {
701
709
// 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 (
703
711
s. bytes . as_ptr ( ) . offset ( begin as isize ) ,
704
712
end - begin
705
713
) )
@@ -821,7 +829,6 @@ mod tests {
821
829
use prelude:: v1:: * ;
822
830
use borrow:: Cow ;
823
831
use super :: * ;
824
- use mem:: transmute;
825
832
826
833
#[ test]
827
834
fn code_point_from_u32 ( ) {
@@ -962,7 +969,7 @@ mod tests {
962
969
string. push_wtf8 ( Wtf8 :: from_str ( " 💩" ) ) ;
963
970
assert_eq ! ( string. bytes, b"a\xC3 \xA9 \xF0 \x9F \x92 \xA9 " ) ;
964
971
965
- fn w ( value : & [ u8 ] ) -> & Wtf8 { unsafe { transmute ( value ) } }
972
+ fn w ( v : & [ u8 ] ) -> & Wtf8 { unsafe { Wtf8 :: from_bytes_unchecked ( v ) } }
966
973
967
974
let mut string = Wtf8Buf :: new ( ) ;
968
975
string. push_wtf8 ( w ( b"\xED \xA0 \xBD " ) ) ; // lead
0 commit comments