transform method
Transforms input
into an instance of U
using the provided locale
.
Implementation
@override
String transform(String input, String locale) {
assert(length >= 0);
if (length == 0) {
return truncationIndicator;
}
final isTruncatedAtStart = truncateAt == TruncateAt.start;
final wordExpression = isTruncatedAtStart
? _wordWithTrailingWhitespaceExpression
: _wordWithLeadingWhitespaceExpression;
final wordMatches = wordExpression.allMatches(input).toList();
final wordMatchIndexes = Iterable<int>.generate(
wordMatches.length,
(i) => isTruncatedAtStart ? wordMatches.length - 1 - i : i,
);
final buffer = <int>[];
var wordCount = 0;
for (final i in wordMatchIndexes) {
final wordMatch = wordMatches[i];
final word = isTruncatedAtStart ? wordMatch.group(1) : wordMatch.group(2);
final wordWithOptionalWhitespace = wordMatch.group(0);
if (isTruncatedAtStart && word != null && wordCount == length) {
buffer.insertAll(0, truncationIndicator.runes);
break;
} else if (!isTruncatedAtStart && word != null && wordCount == length) {
buffer.addAll(truncationIndicator.runes);
break;
}
if (wordWithOptionalWhitespace != null) {
if (isTruncatedAtStart) {
buffer.insertAll(0, wordWithOptionalWhitespace.runes);
} else {
buffer.addAll(wordWithOptionalWhitespace.runes);
}
if (word != null) {
++wordCount;
}
}
}
final result = String.fromCharCodes(buffer);
return result;
}