toString method

  1. @override
String toString(
  1. {String indent = '',
  2. bool hex = false}
)
override

Show bytes description if defined.

Implementation

@override
String toString({String indent = '', bool hex = false}) {
  var s = StringBuffer();

  for (var e in _data) {
    if (e is BytesEmitter) {
      s.write(e.toString(indent: '  ', hex: hex));
    } else if (e is List<int>) {
      if (hex) {
        var h = e.map((e) => e.toHex()).join(' ');
        s.write('[$h]\n');
      } else {
        s.write('$e\n');
      }
    } else if (e is int) {
      if (hex) {
        var h = e.toHex();
        s.write('[$h] ');
      } else {
        s.write('[$e] ');
      }
    } else {
      throw StateError("Can't handle type: $e");
    }
  }

  var lines = s.toString().split('\n').map((l) => '$indent$l');

  var allLines = lines
      .join('\n')
      .replaceAllMapped(_regexpNs, (m) {
        var indent = m.group(1)!;
        var ns = m.group(2)!;
        ns = ns.replaceAll(RegExp(r'[^\da-f]+'), ' ').trim();
        return '$indent[$ns]';
      })
      .replaceAll(_regexpLineTail, '\n')
      .trimRight();

  final description = this.description;
  if (description != null && description.isNotEmpty) {
    return '$indent## $description:\n$allLines\n';
  } else {
    return '$allLines\n';
  }
}