toString method

  1. @override
String toString()
override

Generates a string representation of a TokenSet.

This is intended to allow TokenSets to be used as keys in objects, largely to aid the construction and minimisation of a TokenSet. As such it is not designed to be a human friendly representation of the TokenSet.

@returns {string}

Implementation

@override
String toString() {
  // NOTE: Using Object.keys here as this.edges is very likely
  // to enter 'hash-mode' with many keys being added
  //
  // avoiding a for-in loop here as it leads to the function
  // being de-optimised (at least in V8). From some simple
  // benchmarks the performance is comparable, but allowing
  // V8 to optimize may mean easy performance wins in the future.

  if (cachedString != null) {
    return cachedString!;
  }

  String str = isFinal ? '1' : '0';
  List<String> labels = edges.keys.toList()..sort((a, b) => a.compareTo(b));
  int len = labels.length;

  for (var i = 0; i < len; i++) {
    var label = labels[i];
    TokenSet node = edges[label]!;

    str = '$str$label${node.id}';
  }

  return str;
}