writeVarUint method

void writeVarUint(
  1. int value
)

Write an integer as a list of bytes that contain an LEB128 unsigned integer. The size of the integer is decided automatically.

Implementation

void writeVarUint(int value) {
  int size = (value.toRadixString(2).length / 7.0).ceil();
  int index = 0;
  int i = 0;
  if (kIsWeb) {
    // Use a double to avoid 32-bit overflow on web platforms. JavaScript
    // bitwise operations truncate to 32-bit signed integers, but division
    // works correctly for integers up to 2^53.
    double remaining = value.toDouble();
    while (i < size) {
      int part = (remaining % 128).toInt(); // equivalent to value & 0x7f
      remaining =
          (remaining / 128).floorToDouble(); // equivalent to value >> 7
      _variableEncodeList[index++] = part;
      i += 1;
    }
  } else {
    while (i < size) {
      int part = value & 0x7f;
      //ignore: parameter_assignments
      value >>= 7;
      _variableEncodeList[index++] = part;
      i += 1;
    }
  }
  for (var i = 0; i < index - 1; i++) {
    _variableEncodeList[i] |= 0x80;
  }
  write(_variableEncodeList, index);
}