compareString static method

({String character, int index, bool isAddText}) compareString(
  1. String oldText,
  2. String newText
)

Implementation

static ({String character, int index, bool isAddText}) compareString(String oldText, String newText) {
  final isAddText = newText.length > oldText.length;
  final longerText = isAddText ? newText : oldText;
  final shorterText = isAddText ? oldText : newText;

  int diffIndex = -1;
  for (int i = 0; i < shorterText.length; i++) {
    if (longerText[i] != shorterText[i]) {
      diffIndex = i;
      break;
    }
  }

  if (diffIndex == -1) {
    diffIndex = shorterText.length;
  }

  String characters = longerText.substring(diffIndex, longerText.length - shorterText.length + diffIndex);

  return (
    character: characters,
    index: diffIndex,
    isAddText: isAddText,
  );
}