toPortableString method

String toPortableString()

Encodes a complete storage image, without execution or external state.

Implementation

String toPortableString() {
  if (keyWidth <= 0 || entryCount <= 0 || entries.length != entryCount) {
    throw StateError('A portable associative image must be complete.');
  }
  final result =
      StringBuffer('RHCL_ASSOC_STORAGE_V1 $keyWidth $entryCount\n');
  for (var index = 0; index < entryCount; index++) {
    final entry = entries[index];
    if (entry == null ||
        entry.key.width != keyWidth ||
        entry.valid.width != 1) {
      throw StateError('Invalid associative image entry.');
    }
    final key = MemoryValueCodec.binary(entry.key);
    final valid = MemoryValueCodec.binary(entry.valid);
    if (key.contains('z') || valid.contains('z')) {
      throw StateError('Stored associative images cannot contain Z.');
    }
    result.writeln('$valid $key');
  }
  return result.toString();
}