nextCharacter static method

  1. @visibleForTesting
int nextCharacter(
  1. int index,
  2. String string, [
  3. bool includeWhitespace = true
])

Returns the index into the string of the next character boundary after the given index.

The character boundary is determined by the characters package, so surrogate pairs and extended grapheme clusters are considered.

The index must be between 0 and string.length, inclusive. If given string.length, string.length is returned.

Setting includeWhitespace to false will only return the index of non-space characters.

Implementation

@visibleForTesting
static int nextCharacter(int index, String string,
    [bool includeWhitespace = true]) {
  assert(index >= 0 && index <= string.length);
  if (index == string.length) {
    return string.length;
  }

  int count = 0;
  final Characters remaining =
      string.characters.skipWhile((String currentString) {
    if (count <= index) {
      count += currentString.length;
      return true;
    }
    if (includeWhitespace) {
      return false;
    }
    return TextLayoutMetrics.isWhitespace(currentString.codeUnitAt(0));
  });
  return string.length - remaining.toString().length;
}