nextWordRangeFromReader function

({int end, int start})? nextWordRangeFromReader(
  1. int length,
  2. int offset, {
  3. required GraphemePredicate isWord,
  4. required GraphemeReader graphemeAt,
})

Implementation

({int start, int end})? nextWordRangeFromReader(
  int length,
  int offset, {
  required GraphemePredicate isWord,
  required GraphemeReader graphemeAt,
}) {
  if (length <= 0) {
    return null;
  }

  var position = offset.clamp(0, length);
  while (position < length) {
    final grapheme = graphemeAt(position);
    if (grapheme != null && isWord(grapheme)) {
      break;
    }
    position++;
  }
  if (position >= length) {
    return null;
  }

  var end = position;
  while (end < length) {
    final grapheme = graphemeAt(end);
    if (grapheme == null || !isWord(grapheme)) {
      break;
    }
    end++;
  }
  return (start: position, end: end);
}