skipField function

int skipField(
  1. List<int> bytes,
  2. int offset,
  3. int wireType
)

Skip an unknown field (field number not in descriptor).

Returns the number of bytes consumed (not counting the tag, which the caller has already read).

Wire type mapping:

  • 0 (VARINT): read and discard a varint
  • 1 (I64): skip 8 bytes
  • 2 (LEN): read length varint, skip that many bytes
  • 5 (I32): skip 4 bytes

Throws FormatException for unsupported wire types (3, 4 are deprecated group start/end).

Implementation

int skipField(List<int> bytes, int offset, int wireType) {
  switch (wireType) {
    case 0:
      // VARINT: read until continuation bit is clear.
      final result = decodeVarint(bytes, offset);
      return result['bytesRead']!;
    case 1:
      // I64: always 8 bytes.
      return 8;
    case 2:
      // LEN: varint length prefix + that many bytes.
      final result = decodeVarint(bytes, offset);
      final length = result['value']!;
      final varintSize = result['bytesRead']!;
      return varintSize + length;
    case 5:
      // I32: always 4 bytes.
      return 4;
    default:
      throw FormatException(
        'Unknown or unsupported wire type $wireType at offset $offset',
      );
  }
}