moveWordBackwardFromReader function

int moveWordBackwardFromReader(
  1. int length,
  2. int offset, {
  3. required GraphemePredicate isWord,
  4. required GraphemeReader graphemeAt,
})

Implementation

int moveWordBackwardFromReader(
  int length,
  int offset, {
  required GraphemePredicate isWord,
  required GraphemeReader graphemeAt,
}) {
  if (offset <= 0 || length <= 0) {
    return 0;
  }

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