chooseMatch function

_Match chooseMatch(
  1. List<int> source,
  2. List<int> sourcePositions,
  3. List<int> target,
  4. int targetPos,
)

Implementation

_Match chooseMatch(List<int> source, List<int> sourcePositions,
    List<int> target, int targetPos) {
  int i, len, spos, tpos;
  int? rvLength;
  int? rvOffset;

  for (i = 0; i < sourcePositions.length; i++) {
    len = 0;
    spos = sourcePositions[i];
    tpos = targetPos;
    if (rvLength != null && rvOffset != null && spos < (rvOffset + rvLength)) {
      // this offset is contained in a previous match
      continue;
    }

    while (source[spos++] == target[tpos]) {
      len++;
      tpos++;
    }

    if (rvLength == null || rvOffset == null) {
      rvLength = len;
      rvOffset = sourcePositions[i];
    } else if (rvLength < len) {
      rvLength = len;
      rvOffset = sourcePositions[i];
    }
    if (rvLength > (source.length / 5).floor()) {
      // don't try to find a match that is bigger than one fifth of
      // the source buffer
      break;
    }
  }

  // Fimxe; Return rv
  return _Match(rvOffset, rvLength);
}