@@ -132,11 +132,9 @@ pub mod reader {
132132 use std:: char;
133133
134134 use std:: isize;
135- use std:: old_io:: extensions:: u64_from_be_bytes;
136135 use std:: mem:: transmute;
137136 use std:: num:: Int ;
138- use std:: option:: Option ;
139- use std:: option:: Option :: { None , Some } ;
137+ use std:: slice:: bytes;
140138
141139 use serialize;
142140
@@ -199,20 +197,24 @@ pub mod reader {
199197 return vuint_at_slow ( data, start) ;
200198 }
201199
202- // Lookup table for parsing EBML Element IDs as per http://ebml.sourceforge.net/specs/
203- // The Element IDs are parsed by reading a big endian u32 positioned at data[start].
204- // Using the four most significant bits of the u32 we lookup in the table below how the
205- // element ID should be derived from it.
200+ // Lookup table for parsing EBML Element IDs as per
201+ // http://ebml.sourceforge.net/specs/ The Element IDs are parsed by
202+ // reading a big endian u32 positioned at data[start]. Using the four
203+ // most significant bits of the u32 we lookup in the table below how
204+ // the element ID should be derived from it.
206205 //
207- // The table stores tuples (shift, mask) where shift is the number the u32 should be right
208- // shifted with and mask is the value the right shifted value should be masked with.
209- // If for example the most significant bit is set this means it's a class A ID and the u32
210- // should be right shifted with 24 and masked with 0x7f. Therefore we store (24, 0x7f) at
211- // index 0x8 - 0xF (four bit numbers where the most significant bit is set).
206+ // The table stores tuples (shift, mask) where shift is the number the
207+ // u32 should be right shifted with and mask is the value the right
208+ // shifted value should be masked with. If for example the most
209+ // significant bit is set this means it's a class A ID and the u32
210+ // should be right shifted with 24 and masked with 0x7f. Therefore we
211+ // store (24, 0x7f) at index 0x8 - 0xF (four bit numbers where the most
212+ // significant bit is set).
212213 //
213- // By storing the number of shifts and masks in a table instead of checking in order if
214- // the most significant bit is set, the second most significant bit is set etc. we can
215- // replace up to three "and+branch" with a single table lookup which gives us a measured
214+ // By storing the number of shifts and masks in a table instead of
215+ // checking in order if the most significant bit is set, the second
216+ // most significant bit is set etc. we can replace up to three
217+ // "and+branch" with a single table lookup which gives us a measured
216218 // speedup of around 2x on x86_64.
217219 static SHIFT_MASK_TABLE : [ ( uint , u32 ) ; 16 ] = [
218220 ( 0 , 0x0 ) , ( 0 , 0x0fffffff ) ,
@@ -318,17 +320,23 @@ pub mod reader {
318320
319321 pub fn doc_as_u16 ( d : Doc ) -> u16 {
320322 assert_eq ! ( d. end, d. start + 2 ) ;
321- u64_from_be_bytes ( d. data , d. start , 2 ) as u16
323+ let mut b = [ 0 ; 2 ] ;
324+ bytes:: copy_memory ( & mut b, & d. data [ d. start ..d. end ] ) ;
325+ unsafe { ( * ( b. as_ptr ( ) as * const u16 ) ) . to_be ( ) }
322326 }
323327
324328 pub fn doc_as_u32 ( d : Doc ) -> u32 {
325329 assert_eq ! ( d. end, d. start + 4 ) ;
326- u64_from_be_bytes ( d. data , d. start , 4 ) as u32
330+ let mut b = [ 0 ; 4 ] ;
331+ bytes:: copy_memory ( & mut b, & d. data [ d. start ..d. end ] ) ;
332+ unsafe { ( * ( b. as_ptr ( ) as * const u32 ) ) . to_be ( ) }
327333 }
328334
329335 pub fn doc_as_u64 ( d : Doc ) -> u64 {
330336 assert_eq ! ( d. end, d. start + 8 ) ;
331- u64_from_be_bytes ( d. data , d. start , 8 )
337+ let mut b = [ 0 ; 8 ] ;
338+ bytes:: copy_memory ( & mut b, & d. data [ d. start ..d. end ] ) ;
339+ unsafe { ( * ( b. as_ptr ( ) as * const u64 ) ) . to_be ( ) }
332340 }
333341
334342 pub fn doc_as_i8 ( d : Doc ) -> i8 { doc_as_u8 ( d) as i8 }
@@ -689,11 +697,10 @@ pub mod reader {
689697}
690698
691699pub mod writer {
692- use std:: clone :: Clone ;
693- use std:: old_io :: extensions :: u64_to_be_bytes ;
700+ use std:: mem ;
701+ use std:: num :: Int ;
694702 use std:: old_io:: { Writer , Seek } ;
695703 use std:: old_io;
696- use std:: mem;
697704
698705 use super :: { EsVec , EsMap , EsEnum , EsVecLen , EsVecElt , EsMapLen , EsMapKey ,
699706 EsEnumVid , EsU64 , EsU32 , EsU16 , EsU8 , EsInt , EsI64 , EsI32 , EsI16 , EsI8 ,
@@ -794,43 +801,34 @@ pub mod writer {
794801 }
795802
796803 pub fn wr_tagged_u64 ( & mut self , tag_id : uint , v : u64 ) -> EncodeResult {
797- u64_to_be_bytes ( v, 8 , |v| {
798- self . wr_tagged_bytes ( tag_id, v)
799- } )
804+ let bytes: [ u8 ; 8 ] = unsafe { mem:: transmute ( v. to_be ( ) ) } ;
805+ self . wr_tagged_bytes ( tag_id, & bytes)
800806 }
801807
802808 pub fn wr_tagged_u32 ( & mut self , tag_id : uint , v : u32 ) -> EncodeResult {
803- u64_to_be_bytes ( v as u64 , 4 , |v| {
804- self . wr_tagged_bytes ( tag_id, v)
805- } )
809+ let bytes: [ u8 ; 4 ] = unsafe { mem:: transmute ( v. to_be ( ) ) } ;
810+ self . wr_tagged_bytes ( tag_id, & bytes)
806811 }
807812
808813 pub fn wr_tagged_u16 ( & mut self , tag_id : uint , v : u16 ) -> EncodeResult {
809- u64_to_be_bytes ( v as u64 , 2 , |v| {
810- self . wr_tagged_bytes ( tag_id, v)
811- } )
814+ let bytes: [ u8 ; 2 ] = unsafe { mem:: transmute ( v. to_be ( ) ) } ;
815+ self . wr_tagged_bytes ( tag_id, & bytes)
812816 }
813817
814818 pub fn wr_tagged_u8 ( & mut self , tag_id : uint , v : u8 ) -> EncodeResult {
815819 self . wr_tagged_bytes ( tag_id, & [ v] )
816820 }
817821
818822 pub fn wr_tagged_i64 ( & mut self , tag_id : uint , v : i64 ) -> EncodeResult {
819- u64_to_be_bytes ( v as u64 , 8 , |v| {
820- self . wr_tagged_bytes ( tag_id, v)
821- } )
823+ self . wr_tagged_u64 ( tag_id, v as u64 )
822824 }
823825
824826 pub fn wr_tagged_i32 ( & mut self , tag_id : uint , v : i32 ) -> EncodeResult {
825- u64_to_be_bytes ( v as u64 , 4 , |v| {
826- self . wr_tagged_bytes ( tag_id, v)
827- } )
827+ self . wr_tagged_u32 ( tag_id, v as u32 )
828828 }
829829
830830 pub fn wr_tagged_i16 ( & mut self , tag_id : uint , v : i16 ) -> EncodeResult {
831- u64_to_be_bytes ( v as u64 , 2 , |v| {
832- self . wr_tagged_bytes ( tag_id, v)
833- } )
831+ self . wr_tagged_u16 ( tag_id, v as u16 )
834832 }
835833
836834 pub fn wr_tagged_i8 ( & mut self , tag_id : uint , v : i8 ) -> EncodeResult {
0 commit comments