@@ -132,11 +132,9 @@ pub mod reader {
132
132
use std:: char;
133
133
134
134
use std:: isize;
135
- use std:: old_io:: extensions:: u64_from_be_bytes;
136
135
use std:: mem:: transmute;
137
136
use std:: num:: Int ;
138
- use std:: option:: Option ;
139
- use std:: option:: Option :: { None , Some } ;
137
+ use std:: slice:: bytes;
140
138
141
139
use serialize;
142
140
@@ -199,20 +197,24 @@ pub mod reader {
199
197
return vuint_at_slow ( data, start) ;
200
198
}
201
199
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.
206
205
//
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).
212
213
//
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
216
218
// speedup of around 2x on x86_64.
217
219
static SHIFT_MASK_TABLE : [ ( uint , u32 ) ; 16 ] = [
218
220
( 0 , 0x0 ) , ( 0 , 0x0fffffff ) ,
@@ -318,17 +320,23 @@ pub mod reader {
318
320
319
321
pub fn doc_as_u16 ( d : Doc ) -> u16 {
320
322
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 ( ) }
322
326
}
323
327
324
328
pub fn doc_as_u32 ( d : Doc ) -> u32 {
325
329
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 ( ) }
327
333
}
328
334
329
335
pub fn doc_as_u64 ( d : Doc ) -> u64 {
330
336
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 ( ) }
332
340
}
333
341
334
342
pub fn doc_as_i8 ( d : Doc ) -> i8 { doc_as_u8 ( d) as i8 }
@@ -689,11 +697,10 @@ pub mod reader {
689
697
}
690
698
691
699
pub 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 ;
694
702
use std:: old_io:: { Writer , Seek } ;
695
703
use std:: old_io;
696
- use std:: mem;
697
704
698
705
use super :: { EsVec , EsMap , EsEnum , EsVecLen , EsVecElt , EsMapLen , EsMapKey ,
699
706
EsEnumVid , EsU64 , EsU32 , EsU16 , EsU8 , EsInt , EsI64 , EsI32 , EsI16 , EsI8 ,
@@ -794,43 +801,34 @@ pub mod writer {
794
801
}
795
802
796
803
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)
800
806
}
801
807
802
808
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)
806
811
}
807
812
808
813
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)
812
816
}
813
817
814
818
pub fn wr_tagged_u8 ( & mut self , tag_id : uint , v : u8 ) -> EncodeResult {
815
819
self . wr_tagged_bytes ( tag_id, & [ v] )
816
820
}
817
821
818
822
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 )
822
824
}
823
825
824
826
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 )
828
828
}
829
829
830
830
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 )
834
832
}
835
833
836
834
pub fn wr_tagged_i8 ( & mut self , tag_id : uint , v : i8 ) -> EncodeResult {
0 commit comments