indexOf method

  1. @override
Future<int> indexOf(
  1. int element, [
  2. int start = 0,
  3. int? end
])
override

Returns the index of element if it is found in the range of start inclusive to end exclusive. If element isn't found, or if start == end, then -1 is returned.

The scan terminates at either end or the end of the buffer, whichever comes first. The maximum number of bytes scanned is start - end.

Implementation

@override
Future<int> indexOf(int element, [int start = 0, int? end]) async {
  checkArgument(start >= 0 && (end == null || start < end));
  checkState(!_closed, 'closed');
  while (end == null || start < end) {
    final result = _buffer.indexOf(element, start, end);
    if (result != -1) return result;

    // The byte wasn't in the buffer. Give up if we've already reached our target size or if the
    // underlying stream is exhausted.
    final lastBufferLength = _buffer._length;
    if ((end != null && lastBufferLength >= end) ||
        await _source.read(_buffer, kBlockSize) == 0) {
      return -1;
    }

    // Continue the search from where we left off.
    start = max(start, lastBufferLength);
  }
  return -1;
}