Skip to content

Commit 7be6593

Browse files
committed
Rename variables and change loop structure.
JAVA-5842
1 parent 1bc094c commit 7be6593

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

bson/src/main/org/bson/io/ByteBufferBsonInput.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,45 +193,45 @@ private int computeCStringLength(final int prevPos) {
193193

194194
// `>>> 3` means dividing without remainder by `Long.BYTES` because `Long.BYTES` is 2^3
195195
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);
199200
/*
200201
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).
202203
*/
203-
long mask = word - 0x0101010101010101L;
204+
long mask = chunk - 0x0101010101010101L;
204205
/*
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.
208209
*/
209-
mask &= ~word;
210+
mask &= ~chunk;
210211
/*
211212
0x8080808080808080:
212213
10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000
213214
214215
mask:
215216
00000000 00000000 11111111 00000000 00000001 00000001 00000000 00000111
216217
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.
219220
220221
result:
221222
00000000 00000000 10000000 00000000 00000000 00000000 00000000 00000000
222223
^^^^^^^^
223-
The high bit is set only at the 0x00 byte position.
224+
The MSB is set only at the 0x00 byte position.
224225
*/
225226
mask &= 0x8080808080808080L;
226227
if (mask != 0) {
227228
/*
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.
229230
*/
230231
int offset = Long.numberOfTrailingZeros(mask) >>> 3;
231232
// Find the NULL terminator at pos + offset
232233
return (pos - prevPos) + offset + 1;
233234
}
234-
pos += 8;
235235
}
236236

237237
// Process remaining bytes one by one.

0 commit comments

Comments
 (0)