diff_levenshtein method

int diff_levenshtein(
  1. List<Diff> diffs
)

Compute the Levenshtein distance; the number of inserted, deleted or substituted characters. diffs is a List of Diff objects. Returns the number of changes.

Implementation

int diff_levenshtein(List<Diff> diffs) {
  int levenshtein = 0;
  int insertions = 0;
  int deletions = 0;
  for (Diff aDiff in diffs) {
    switch (aDiff.operation) {
      case Operation.insert:
        insertions += aDiff.text.length;
        break;
      case Operation.delete:
        deletions += aDiff.text.length;
        break;
      case Operation.equal:
        // A deletion and an insertion is one substitution.
        levenshtein += max<int>(insertions, deletions);
        insertions = 0;
        deletions = 0;
        break;
    }
  }
  levenshtein += max<int>(insertions, deletions);
  return levenshtein;
}