search method

bool search(
  1. Searcher searcher, {
  2. bool skipLastMatch = false,
  3. Position? hitPosition,
  4. Position? startPosition,
  5. RelativePosition relativePosition = RelativePosition.onFirst,
})

Searches a pattern from a given position. searcher specifies the kind of pattern: regular expression, raw string... startPosition defines the position to start. If null the currentPosition is taken. hitPosition: OUT: the position of the hit. If result is false the value is not changed. If null the currentPosition is used. relativePosition defines the result position relative to the found pattern. skipLastMatch: if true the current position is moved below the last found match before the search is started. This is only meaningful if there was a previous call of search and the relativePosition of the previous call was RelativePosition.onFirst. This parameter makes it easy to find the same pattern multiple times. Note: currentPosition is always set, depending on relativePosition. Returns true if the pattern is found.

Implementation

bool search(
  Searcher searcher, {
  bool skipLastMatch = false,
  Position? hitPosition,
  Position? startPosition,
  RelativePosition relativePosition = RelativePosition.onFirst,
}) {
  var rc = false;
  startPosition ??= currentPosition;
  if (currentRegion.contains(startPosition)) {
    int? start;
    currentPosition.clone(startPosition);
    if (skipLastMatch) {
      currentPosition.addColumn(lastMatch.length());
    }

    /// Is the region exactly one line?
    if (currentPosition.line == currentRegion.end.line &&
        currentPosition.column > 0 &&
        currentRegion.end.line > 0) {
      final line = lines[currentPosition.line]
          .substring(currentPosition.column, currentRegion.end.column);
      lastMatch.position.line = currentPosition.line;
      rc = (start = searcher.next(line)) != null;
      if (rc) {
        currentPosition.addColumn(start!);
      }
    } else {
      var firstColumn = currentPosition.column;
      final lastFullLine = currentRegion.end.line - 1;
      var currentLine = currentPosition.line;
      while (currentLine <= lastFullLine) {
        lastMatch.position.line = currentLine;
        rc = (start = searcher.next(lines[currentLine], firstColumn)) != null;
        if (rc) {
          currentPosition.set(currentLine, start!);
          break;
        }
        firstColumn = 0;
        currentLine++;
      }
      if (!rc && currentRegion.end.column != 0) {
        rc = (start = searcher.next(lines[currentRegion.end.line]
                .substring(0, currentRegion.end.column))) !=
            null;
        if (rc) {
          currentPosition.set(currentRegion.end.line, start!);
        }
      }
    }
    if (rc) {
      hitPosition ??= currentPosition;
      hitPosition.clone(currentPosition);
      switch (relativePosition) {
        case RelativePosition.aboveFirst:
          currentPosition.backward(1);
          break;
        case RelativePosition.onFirst:
          break;
        case RelativePosition.onLast:
          currentPosition.addColumn(lastMatch.length() - 1);
          break;
        case RelativePosition.belowLast:
          currentPosition.addColumn(lastMatch.length());
          break;
      }
    }
  }
  return rc;
}