diff<LeftType, RightType> static method

List<ItemResult<LeftType?, RightType?>> diff<LeftType, RightType>(
  1. List<LeftType> leftItems,
  2. List<RightType> rightItems
)

Method comparing two objects by converting them into strings These strings are compared line by line and

ItemResult contains diff state and compared items

Implementation

static List<ItemResult<LeftType?, RightType?>> diff<LeftType, RightType>(
    List<LeftType> leftItems, List<RightType> rightItems) {
  final commonSeq = _longestCommonSubstring(
      leftItems.map((e) => e.toString().split('')).toList(growable: false),
      rightItems.map((e) => e.toString().split('')).toList(growable: false));
  final startBefore = commonSeq.startString1;
  final startAfter = commonSeq.startString2;
  if (commonSeq.length == 0) {
    final Iterable<ItemResult<LeftType, RightType?>> result =
        leftItems.map((each) {
      return ItemResult<LeftType, RightType?>(
        each,
        null,
        state: LineDiffState.negative,
      );
    });
    return [
      ...result,
      ...rightItems.map((each) {
        return ItemResult<LeftType?, RightType>(null, each,
            state: LineDiffState.positive);
      }).toList(growable: false)
    ];
  }

  final beforeLeft = leftItems.sublist(0, startBefore);
  final afterLeft = rightItems.sublist(0, startAfter);
  final equal2 =
      leftItems.sublist(startBefore!, startBefore + commonSeq.length!);
  final List<ItemResult<LeftType, RightType>> equal =
      <ItemResult<LeftType, RightType>>[];

  final _sublist =
      rightItems.sublist(startAfter!, startAfter + commonSeq.length!);
  for (var i = 0; i < _sublist.length; ++i) {
    final each = _sublist[i];
    equal.add(ItemResult<LeftType, RightType>(
      equal2[i],
      each,
      state: LineDiffState.neutral,
    ));
  }

  final beforeRight = leftItems.sublist(startBefore + commonSeq.length!);
  final afterRight = rightItems.sublist(startAfter + commonSeq.length!);
  return [
    ...diff(beforeLeft, afterLeft),
    ...equal,
    ...diff(beforeRight, afterRight)
  ];
}