readUnsignedInt method
Reads a variable-length unsigned integer using LEB128 encoding.
The first bit of each byte indicates continuation (1) or termination (0). The remaining 7 bits contain data. This encoding is optimized for small values.
Returns the decoded unsigned integer value
Implementation
int readUnsignedInt() {
assert(_bufferPosition <= _bufferData.length);
int variableByteDecode = 0;
int variableByteShift = 0;
// Optimized variable-length integer decoding with reduced array access
int pos = _bufferPosition;
final data = _bufferData;
// Unroll first few iterations for common cases
int byte = data[pos++];
if ((byte & 0x80) == 0) {
_bufferPosition = pos;
return byte & 0x7f;
}
variableByteDecode = byte & 0x7f;
variableByteShift = 7;
byte = data[pos++];
if ((byte & 0x80) == 0) {
_bufferPosition = pos;
return variableByteDecode | ((byte & 0x7f) << variableByteShift);
}
variableByteDecode |= (byte & 0x7f) << variableByteShift;
variableByteShift += 7;
// Continue with loop for longer values
while ((data[pos] & 0x80) != 0) {
variableByteDecode |= (data[pos] & 0x7f) << variableByteShift;
variableByteShift += 7;
++pos;
}
// read the seven data bits from the last byte
variableByteDecode |= (data[pos] & 0x7f) << variableByteShift;
_bufferPosition = pos + 1;
return variableByteDecode;
}