count method

int count(
  1. Searcher searcher, {
  2. int maximalHitsPerLine = 1,
  3. int? maximalHits,
  4. bool saveCurrentPosition = true,
})

Count the hits of a pattern from the current position to the region end. searcher specifies the kind of search: regular expression, raw string... If regExp is not null this expression is searched. If pattern is not null this string converted to a regular expression. maximalHitsPerLine: if this count of hits is reached search is continued in the next line. maximalHits: if this count is reached the search stops. Returns the number of hits.

Implementation

int count(Searcher searcher,
    {int maximalHitsPerLine = 1,
    int? maximalHits,
    bool saveCurrentPosition = true}) {
  var rc = 0;
  var skipLast = false;
  Position? position;
  if (maximalHitsPerLine <= 0) {
    maximalHitsPerLine = int52MaxValue;
  }
  if (saveCurrentPosition) {
    position = Position(0, 0, this);
    position.clone(currentPosition);
  }
  var lastLine = currentPosition.line - 1;
  var hitsInLine = 0;
  while (search(searcher, skipLastMatch: skipLast)) {
    skipLast = true;
    if (++rc >= (maximalHits ?? int52MaxValue)) {
      break;
    }
    if (currentPosition.line != lastLine) {
      hitsInLine = 1;
      lastLine = currentPosition.line;
    } else {
      hitsInLine++;
    }
    if (hitsInLine >= maximalHitsPerLine) {
      currentPosition.nextLine();
      skipLast = false;
    }
  }
  if (saveCurrentPosition) {
    currentPosition.clone(position!);
  }
  return rc;
}