parse static method

EncoderInstruction parse(
  1. Uint8List bytes
)

Parse a single encoder instruction from bytes.

Returns the decoded instruction instance.

Throws ArgumentError if the bytes do not represent a valid instruction or if the buffer is too short.

Implementation

static EncoderInstruction parse(Uint8List bytes) {
  if (bytes.isEmpty) {
    throw ArgumentError(
      'Empty bytes cannot be parsed as an encoder instruction',
    );
  }

  final firstByte = bytes[0];

  // Insert with Name Reference: first bit = 1 (Section 4.3.2).
  if ((firstByte & 0x80) != 0) {
    return InsertWithNameReference._parse(bytes);
  }

  // Insert with Literal Name: first two bits = 01 (Section 4.3.3).
  if ((firstByte & 0xC0) == 0x40) {
    return InsertWithoutNameReference._parse(bytes);
  }

  // Set Dynamic Table Capacity: first three bits = 001 (Section 4.3.1).
  if ((firstByte & 0xE0) == 0x20) {
    return SetDynamicTableCapacity._parse(bytes);
  }

  // Duplicate: first three bits = 000 (Section 4.3.4).
  if ((firstByte & 0xE0) == 0x00) {
    return Duplicate._parse(bytes);
  }

  throw ArgumentError(
    'Unknown encoder instruction: 0x${firstByte.toRadixString(16).padLeft(2, '0')}',
  );
}