@@ -193,45 +193,45 @@ private int computeCStringLength(final int prevPos) {
193
193
194
194
// `>>> 3` means dividing without remainder by `Long.BYTES` because `Long.BYTES` is 2^3
195
195
int chunks = (limit - pos ) >>> 3 ;
196
- // Process `Long.BYTES` at a time.
197
- for (int i = 0 ; i < chunks ; i ++) {
198
- long word = buffer .getLong (pos );
196
+ // `<< 3` means multiplying by `Long.BYTES` because `Long.BYTES` is 2^3
197
+ int toPos = pos + (chunks << 3 );
198
+ for (; pos < toPos ; pos += Long .BYTES ) {
199
+ long chunk = buffer .getLong (pos );
199
200
/*
200
201
Subtract 0x0101010101010101L to cause a borrow on 0x00 bytes.
201
- if original byte is 00000000, then 00000000 - 00000001 = 11111111 (borrow causes high bit set to 1).
202
+ if original byte is 00000000, then 00000000 - 00000001 = 11111111 (borrow causes the MSB set to 1).
202
203
*/
203
- long mask = word - 0x0101010101010101L ;
204
+ long mask = chunk - 0x0101010101010101L ;
204
205
/*
205
- mask will only have high bits set iff it was a 0x00 byte (0x00 becomes 0xFF because of the borrow).
206
- ~word will have bits that were originally 0 set to 1.
207
- mask & ~word will have high bits set iff original byte was 0x00.
206
+ mask will only have the MSB set iff it was a 0x00 byte (0x00 becomes 0xFF because of the borrow).
207
+ ~chunk will have bits that were originally 0 set to 1.
208
+ mask & ~chunk will have the MSB set iff original byte was 0x00.
208
209
*/
209
- mask &= ~word ;
210
+ mask &= ~chunk ;
210
211
/*
211
212
0x8080808080808080:
212
213
10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000
213
214
214
215
mask:
215
216
00000000 00000000 11111111 00000000 00000001 00000001 00000000 00000111
216
217
217
- ANDing mask with 0x8080808080808080 isolates the high bit (0x80) in positions where
218
- the original byte was 0x00, thereby setting the high bit to 1 only at the 0x00 byte position.
218
+ ANDing mask with 0x8080808080808080 isolates the MSB (0x80) in positions where
219
+ the original byte was 0x00, thereby setting the MSB to 1 only at the 0x00 byte position.
219
220
220
221
result:
221
222
00000000 00000000 10000000 00000000 00000000 00000000 00000000 00000000
222
223
^^^^^^^^
223
- The high bit is set only at the 0x00 byte position.
224
+ The MSB is set only at the 0x00 byte position.
224
225
*/
225
226
mask &= 0x8080808080808080L ;
226
227
if (mask != 0 ) {
227
228
/*
228
- * Performing >>> 3 (i.e., dividing by 8) gives the byte offset from the least significant bit ( LSB) .
229
+ * Performing >>> 3 (i.e., dividing by 8) gives the byte offset from the LSB.
229
230
*/
230
231
int offset = Long .numberOfTrailingZeros (mask ) >>> 3 ;
231
232
// Find the NULL terminator at pos + offset
232
233
return (pos - prevPos ) + offset + 1 ;
233
234
}
234
- pos += 8 ;
235
235
}
236
236
237
237
// Process remaining bytes one by one.
0 commit comments