diff_main method

List<Diff> diff_main(
  1. String text1,
  2. String text2, [
  3. bool checklines = true,
  4. DateTime? deadline,
])

Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing. text1 is the old string to be diffed. text2 is the new string to be diffed. checklines is an optional speedup flag. If present and false, then don't run a line-level diff first to identify the changed areas. Defaults to true, which does a faster, slightly less optimal diff. deadline is an optional time when the diff should be complete by. Used internally for recursive calls. Users should set DiffTimeout instead. Returns a List of Diff objects.

Implementation

List<Diff> diff_main(String text1, String text2,
    [bool checklines = true, DateTime? deadline]) {
  // Set a deadline by which time the diff must be complete.
  if (deadline == null) {
    deadline = DateTime.now();
    if (Diff_Timeout <= 0) {
      // One year should be sufficient for 'infinity'.
      deadline = deadline.add(Duration(days: 365));
    } else {
      deadline = deadline
          .add(Duration(milliseconds: (Diff_Timeout * 1000).toInt()));
    }
  }

  // Check for equality (speedup).
  List<Diff> diffs;
  if (text1 == text2) {
    diffs = [];
    if (text1.isNotEmpty) {
      diffs.add(Diff(Operation.equal, text1));
    }
    return diffs;
  }

  // Trim off common prefix (speedup).
  int commonlength = diff_commonPrefix(text1, text2);
  String commonprefix = text1.substring(0, commonlength);
  text1 = text1.substring(commonlength);
  text2 = text2.substring(commonlength);

  // Trim off common suffix (speedup).
  commonlength = diff_commonSuffix(text1, text2);
  String commonsuffix = text1.substring(text1.length - commonlength);
  text1 = text1.substring(0, text1.length - commonlength);
  text2 = text2.substring(0, text2.length - commonlength);

  // Compute the diff on the middle block.
  diffs = _diff_compute(text1, text2, checklines, deadline);

  // Restore the prefix and suffix.
  if (commonprefix.isNotEmpty) {
    diffs.insert(0, Diff(Operation.equal, commonprefix));
  }
  if (commonsuffix.isNotEmpty) {
    diffs.add(Diff(Operation.equal, commonsuffix));
  }

  diff_cleanupMerge(diffs);
  return diffs;
}