encodeVarint static method

Uint8List encodeVarint(
  1. int value
)

Encodes an integer as a varint (base-128, MSB-first)

Implementation

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

  if (value == 0) {
    return Uint8List.fromList([0]);
  }

  final bytes = <int>[];
  var remaining = value;

  while (remaining > 0) {
    bytes.insert(0, remaining & 0x7F);
    remaining >>= 7;
  }

  // Set continuation bits (MSB) on all bytes except the last
  for (var i = 0; i < bytes.length - 1; i++) {
    bytes[i] |= 0x80;
  }

  return Uint8List.fromList(bytes);
}