tailDifferent method

String tailDifferent(
  1. String other, {
  2. List<int>? splitIndexes,
  3. bool splitIndexesAlreadySorted = false,
})

Returns this String tail removing the head part that is equals to other.

  • If splitIndexes is provided the head String can only be split at this indexes.

Implementation

String tailDifferent(String other,
    {List<int>? splitIndexes, bool splitIndexesAlreadySorted = false}) {
  var head = headEqualsLength(other);

  if (head == 0) return this;
  if (head == length) return '';

  if (splitIndexes != null && splitIndexes.isNotEmpty) {
    if (!splitIndexesAlreadySorted) {
      splitIndexes = splitIndexes.toList();
      splitIndexes.sort();
    }

    var idx = splitIndexes.searchInsertSortedIndex(head);
    if (idx >= splitIndexes.length) idx = splitIndexes.lastIndex;
    var split = splitIndexes[idx];

    if (idx == 0) {
      if (head < split) {
        return this;
      } else {
        head = split;
      }
    } else if (idx == splitIndexes.lastIndex) {
      if (head < split) {
        head = splitIndexes[idx - 1];
      } else {
        head = split;
      }
    } else {
      if (head != split) {
        head = splitIndexes[idx - 1];
      }
    }
  }

  var sDiff = substring(head);
  return sDiff;
}