diff_toDelta method

String diff_toDelta(
  1. List<Diff> diffs
)

Crush the diff into an encoded string which describes the operations required to transform text1 into text2. E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation. diffs is a List of Diff objects. Returns the delta text.

Implementation

String diff_toDelta(List<Diff> diffs) {
  final text = StringBuffer();
  for (Diff aDiff in diffs) {
    switch (aDiff.operation) {
      case Operation.insert:
        text.write('+');
        text.write(Uri.encodeFull(aDiff.text));
        text.write('\t');
        break;
      case Operation.delete:
        text.write('-');
        text.write(aDiff.text.length);
        text.write('\t');
        break;
      case Operation.equal:
        text.write('=');
        text.write(aDiff.text.length);
        text.write('\t');
        break;
    }
  }
  String delta = text.toString();
  if (delta.isNotEmpty) {
    // Strip off trailing tab character.
    delta = delta.substring(0, delta.length - 1);
  }
  return delta.replaceAll('%20', ' ');
}