endTable method

int endTable()

End the current table and return its offset.

Implementation

int endTable() {
  assert(_inVTable);
  // Prepare for writing the VTable.
  _prepare(_sizeofInt32, 1);
  final tableTail = _tail;
  // Prepare the size of the current table.
  final currentVTable = _currentVTable!;
  currentVTable.tableSize = tableTail - _currentTableEndTail;
  // Prepare the VTable to use for the current table.
  int? vTableTail;
  {
    currentVTable.computeFieldOffsets(tableTail);

    // Try to find an existing compatible VTable.
    if (deduplicateTables) {
      // Search backward - more likely to have recently used one
      for (var i = _vTables.length - 1; i >= 0; i--) {
        final vt2Offset = _vTables[i];
        final vt2Start = _buf.lengthInBytes - vt2Offset;
        final vt2Size = _buf.getUint16(vt2Start, Endian.little);

        if (currentVTable._vTableSize == vt2Size &&
            currentVTable._offsetsMatch(vt2Start, _buf)) {
          vTableTail = vt2Offset;
          break;
        }
      }
    }

    // Write a new VTable.
    if (vTableTail == null) {
      _prepare(_sizeofUint16, _currentVTable!.numOfUint16);
      vTableTail = _tail;
      currentVTable.tail = vTableTail;
      currentVTable.output(_buf, _buf.lengthInBytes - _tail);
      if (deduplicateTables) _vTables.add(currentVTable.tail);
    }
  }
  // Set the VTable offset.
  _setInt32AtTail(tableTail, vTableTail - tableTail);
  // Done with this table.
  _currentVTable = null;
  return tableTail;
}