indexAtWord method
Returns the index where word
starts (or ends, if start
is false
).
Note, the first word is word 1, and so on.
Implementation
int indexAtWord(int? word, {bool start = true}) {
if (word == null || word <= 0) return 0;
var wordCount = 0;
var isInWord = false;
var i = 0;
final utf16Chars = codeUnits;
for (final utf16Char in utf16Chars) {
final isNonWordChar = isNonWordCharacter(utf16Char);
if (isInWord) {
if (isNonWordChar) {
isInWord = false;
if (word == wordCount && !start) return i;
}
} else if (!isNonWordChar) {
wordCount++;
if (word == wordCount && start) return i;
isInWord = true;
}
i++;
}
return i;
}