@@ -682,34 +682,30 @@ pub mod checksum {
682
682
}
683
683
684
684
/// Compute an RFC 1071 compliant checksum (without the final complement).
685
- pub fn data ( mut data : & [ u8 ] ) -> u16 {
685
+ pub fn data ( data : & [ u8 ] ) -> u16 {
686
686
let mut accum = 0 ;
687
687
688
688
// For each 32-byte chunk...
689
689
const CHUNK_SIZE : usize = 32 ;
690
- while data. len ( ) >= CHUNK_SIZE {
691
- let chunk = & data[ ..CHUNK_SIZE ] ;
692
- let mut i = 0 ;
690
+ const WORD_SIZE : usize = 2 ;
691
+ for chunk in data. chunks_exact ( CHUNK_SIZE ) {
693
692
// ... take by 2 bytes and sum them.
694
- while i + 1 < CHUNK_SIZE {
695
- accum += u16:: from_be_bytes ( [ chunk[ i] , chunk[ i + 1 ] ] ) as u32 ;
696
- i += 2 ;
693
+ for pair in chunk. chunks_exact ( WORD_SIZE ) {
694
+ accum += u16:: from_be_bytes ( [ pair[ 0 ] , pair[ 1 ] ] ) as u32 ;
697
695
}
698
-
699
- data = & data[ CHUNK_SIZE ..] ;
700
696
}
701
697
702
698
// Sum the rest that does not fit the last 32-byte chunk,
703
699
// taking by 2 bytes.
704
- let mut i = 0 ;
705
- while i + 1 < data. len ( ) {
706
- accum += u16:: from_be_bytes ( [ data[ i] , data[ i + 1 ] ] ) as u32 ;
707
- i += 2 ;
700
+ let remainder = data. chunks_exact ( CHUNK_SIZE ) . remainder ( ) ;
701
+ for pair in remainder. chunks_exact ( WORD_SIZE ) {
702
+ accum += u16:: from_be_bytes ( [ pair[ 0 ] , pair[ 1 ] ] ) as u32 ;
708
703
}
709
704
710
705
// Add the last remaining odd byte, if any.
711
- if i < data. len ( ) {
712
- accum += ( data[ i] as u32 ) << 8 ;
706
+ let last = remainder. chunks_exact ( WORD_SIZE ) . remainder ( ) ;
707
+ if !last. is_empty ( ) {
708
+ accum += ( last[ 0 ] as u32 ) << 8 ;
713
709
}
714
710
715
711
propagate_carries ( accum)
0 commit comments