toString method
Emmulate GNU diff's format.
Header: @@ -382,8 +481,9 @@
Indicies are printed as 1-based, not 0-based. Returns the GNU diff string.
Implementation
String toString() {
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 = new StringBuffer('@@ -$coords1 +$coords2 @@\n');
// Escape the body of the patch with %xx notation.
for (Diff aDiff in this.diffs) {
switch (aDiff.operation) {
case DIFF_INSERT:
text.write('+');
break;
case DIFF_DELETE:
text.write('-');
break;
case DIFF_EQUAL:
text.write(' ');
break;
}
text
..write(Uri.encodeFull(aDiff.text))
..write('\n');
}
return text.toString().replaceAll('%20', ' ');
}