@@ -69,21 +69,18 @@ cfg_match! {
69
69
70
70
const CHUNK_SIZE : usize = 16 ;
71
71
72
- let src_bytes = src. as_bytes( ) ;
73
-
74
- let chunk_count = src. len( ) / CHUNK_SIZE ;
72
+ let mut chunks = src. as_bytes( ) . chunks_exact( CHUNK_SIZE ) ;
75
73
76
74
// This variable keeps track of where we should start decoding a
77
75
// chunk. If a multi-byte character spans across chunk boundaries,
78
76
// we need to skip that part in the next chunk because we already
79
77
// handled it.
80
78
let mut intra_chunk_offset = 0 ;
81
79
82
- for chunk_index in 0 ..chunk_count {
83
- let ptr = src_bytes. as_ptr( ) as * const __m128i;
80
+ for ( chunk_index, chunk) in chunks. by_ref( ) . enumerate( ) {
84
81
// We don't know if the pointer is aligned to 16 bytes, so we
85
82
// use `loadu`, which supports unaligned loading.
86
- let chunk = unsafe { _mm_loadu_si128( ptr . add ( chunk_index ) ) } ;
83
+ let chunk = unsafe { _mm_loadu_si128( chunk . as_ptr ( ) as * const __m128i ) } ;
87
84
88
85
// For character in the chunk, see if its byte value is < 0, which
89
86
// indicates that it's part of a UTF-8 char.
@@ -124,7 +121,7 @@ cfg_match! {
124
121
}
125
122
126
123
// There might still be a tail left to analyze
127
- let tail_start = chunk_count * CHUNK_SIZE + intra_chunk_offset;
124
+ let tail_start = src . len ( ) - chunks . remainder ( ) . len ( ) + intra_chunk_offset;
128
125
if tail_start < src. len( ) {
129
126
analyze_source_file_generic(
130
127
& src[ tail_start..] ,
@@ -194,21 +191,18 @@ cfg_match! {
194
191
195
192
const CHUNK_SIZE : usize = 16 ;
196
193
197
- let src_bytes = src. as_bytes( ) ;
198
-
199
- let chunk_count = src. len( ) / CHUNK_SIZE ;
194
+ let mut chunks = src. as_bytes( ) . chunks_exact( CHUNK_SIZE ) ;
200
195
201
196
// This variable keeps track of where we should start decoding a
202
197
// chunk. If a multi-byte character spans across chunk boundaries,
203
198
// we need to skip that part in the next chunk because we already
204
199
// handled it.
205
200
let mut intra_chunk_offset = 0 ;
206
201
207
- for chunk_index in 0 ..chunk_count {
208
- let ptr = src_bytes. as_ptr( ) as * const __m128i;
202
+ for ( chunk_index, chunk) in chunks. by_ref( ) . enumerate( ) {
209
203
// We don't know if the pointer is aligned to 16 bytes, so we
210
204
// use `loadu`, which supports unaligned loading.
211
- let chunk = unsafe { _mm_loadu_si128( ptr . add ( chunk_index ) ) } ;
205
+ let chunk = unsafe { _mm_loadu_si128( chunk . as_ptr ( ) as * const __m128i ) } ;
212
206
213
207
// For character in the chunk, see if its byte value is < 0, which
214
208
// indicates that it's part of a UTF-8 char.
@@ -249,7 +243,7 @@ cfg_match! {
249
243
}
250
244
251
245
// There might still be a tail left to analyze
252
- let tail_start = chunk_count * CHUNK_SIZE + intra_chunk_offset;
246
+ let tail_start = src . len ( ) - chunks . remainder ( ) . len ( ) + intra_chunk_offset;
253
247
if tail_start < src. len( ) {
254
248
analyze_source_file_generic(
255
249
& src[ tail_start..] ,
0 commit comments