toGnuString method

String toGnuString()

Emulate GNU diff's format. Header: @@ -382,8 +481,9 @@ Indices are printed as 1-based, not 0-based. Returns the GNU diff string.

Implementation

String toGnuString() {
  String? coords1, coords2;
  if (this.length1 == 0) {
    coords1 = '${this.start1},0';
  } else if (this.length1 == 1) {
    coords1 = (this.start1 + 1).toString();
  } else {
    coords1 = '${this.start1 + 1},${this.length1}';
  }
  if (this.length2 == 0) {
    coords2 = '${this.start2},0';
  } else if (this.length2 == 1) {
    coords2 = (this.start2 + 1).toString();
  } else {
    coords2 = '${this.start2 + 1},${this.length2}';
  }
  final text = StringBuffer('@@ -$coords1 +$coords2 @@\n');
  // Escape the body of the patch with %xx notation.
  for (final aDiff in this.diffs) {
    switch (aDiff.operation) {
      case Operation.insert:
        text.write('+');
        break;
      case Operation.delete:
        text.write('-');
        break;
      case Operation.equal:
        text.write(' ');
        break;
    }
    text.write(Uri.encodeFull(aDiff.text));
    text.write('\n');
  }
  return text.toString().replaceAll('%20', ' ');
}