retrieveNextPair method

ExpandedPair? retrieveNextPair(
  1. BitArray row,
  2. List<ExpandedPair> previousPairs,
  3. int rowNumber
)

Implementation

ExpandedPair? retrieveNextPair(
  BitArray row,
  List<ExpandedPair> previousPairs,
  int rowNumber,
) {
  bool isOddPattern = previousPairs.length % 2 == 0;
  if (_startFromEven) {
    isOddPattern = !isOddPattern;
  }

  FinderPattern? pattern;
  DataCharacter? leftChar;

  bool keepFinding = true;
  int forcedOffset = -1;
  do {
    _findNextPair(row, previousPairs, forcedOffset);
    pattern = _parseFoundFinderPattern(
      row,
      rowNumber,
      isOddPattern,
      previousPairs,
    );
    if (pattern == null) {
      // probable false positive, keep looking
      forcedOffset = _getNextSecondBar(row, _startEnd[0]);
    } else {
      try {
        leftChar = decodeDataCharacter(row, pattern, isOddPattern, true);
        keepFinding = false;
      } on NotFoundException catch (_) {
        // probable false positive, keep looking
        forcedOffset = _getNextSecondBar(row, _startEnd[0]);
      }
    }
  } while (keepFinding);

  // When stacked symbol is split over multiple rows, there's no way to guess if this pair can be last or not.
  // bool mayBeLast = checkPairSequence(previousPairs, pattern);

  if (previousPairs.isNotEmpty &&
      previousPairs[previousPairs.length - 1].mustBeLast) {
    throw NotFoundException.instance;
  }

  DataCharacter? rightChar;
  try {
    rightChar = decodeDataCharacter(row, pattern!, isOddPattern, false);
  } on NotFoundException catch (_) {
    rightChar = null;
  }
  return ExpandedPair(leftChar, rightChar, pattern);
}