decodeVarint function

(int, int) decodeVarint(
  1. Uint8List bytes
)

Decodes a varint to an integer

Implementation

(int, int) decodeVarint(Uint8List bytes) {
  int result = 0;
  int shift = 0;
  int bytesRead = 0;

  for (final b in bytes) {
    bytesRead++;
    result |= (b & 0x7F) << shift;
    if (b < 0x80) {
      return (result, bytesRead);
    }
    shift += 7;
    if (shift > 63) {
      throw FormatException('Varint too long');
    }
  }

  throw FormatException('Unexpected end of varint');
}