encodeVarint function

Uint8List encodeVarint(
  1. int value
)

Encodes a non-negative integer as a varint.

Implementation

Uint8List encodeVarint(int value) {
  if (value < 0) {
    throw ArgumentError('Value must be non-negative');
  }

  final bytes = <int>[];
  while (value > 0) {
    bytes.add((value & 0x7F) | (bytes.isEmpty ? 0 : 0x80));
    value >>= 7;
  }

  if (bytes.isEmpty) {
    bytes.add(0);
  }

  return Uint8List.fromList(bytes);
}