computeCharDiff function

List<CharDiff> computeCharDiff(
  1. String oldStr,
  2. String newStr
)

Compute a character-level diff between oldStr and newStr.

Implementation

List<CharDiff> computeCharDiff(String oldStr, String newStr) {
  final oldChars = oldStr.split('');
  final newChars = newStr.split('');
  final ops = shortestEditScript(oldChars, newChars);

  final result = <CharDiff>[];
  var oi = 0, ni = 0;
  for (final op in ops) {
    switch (op) {
      case EditOp.equal:
        result.add(CharDiff(char: oldChars[oi], type: DiffType.context));
        oi++;
        ni++;
      case EditOp.delete:
        result.add(CharDiff(char: oldChars[oi], type: DiffType.remove));
        oi++;
      case EditOp.insert:
        result.add(CharDiff(char: newChars[ni], type: DiffType.add));
        ni++;
    }
  }
  return result;
}