fastDiff function

DiffResult fastDiff(
  1. String oldText,
  2. String newText,
  3. int cursorPosition
)

Performs a fast diff operation on two input strings based on provided cursorPosition.

Implementation

DiffResult fastDiff(String oldText, String newText, int cursorPosition) {
  var delta = newText.length - oldText.length;
  var limit = math.max(0, cursorPosition - delta);
  var end = oldText.length;
  while (end > limit && oldText[end - 1] == newText[end + delta - 1]) {
    end -= 1;
  }
  var start = 0;
  var startLimit = cursorPosition - math.max(0, delta);
  while (start < startLimit && oldText[start] == newText[start]) {
    start += 1;
  }
  final deleted = (start < end) ? oldText.substring(start, end) : '';
  final inserted = newText.substring(start, end + delta);
  return DiffResult(start, deleted, inserted);
}