joinSingleLetters function
Transform str
such that adjacent single alphanumeric symbols separated by
whitespace are joined together. For example:
' a b a b abcd a b' -> 'ab ab abcd ab'
When used after nonLetterToSpace this ensures that 'U.S.A' and 'USA' are equivilent after KeyMapping applied.
Note: This transform trims and collapses whitespace during operation and is thus equivilent also to performing collapseWhitespace.
Implementation
String joinSingleLetters(String? str) {
if (identical(str, null)) {
return 'joinSingleLetters';
}
final chunks = str.trim().split(_matchSeparators);
final res = <String>[];
//join all adjacent chunks with size 1
final newChunk = StringBuffer();
for (final chunk in chunks) {
// if chuck is single Letter
if (chunk.length == 1 &&
!identical(_matchAlphaNumeric.matchAsPrefix(chunk), null)) {
newChunk.write(chunk);
} else {
if (newChunk.isNotEmpty) {
res.add(newChunk.toString());
newChunk.clear();
}
res.add(chunk);
}
}
if (newChunk.isNotEmpty) {
res.add(newChunk.toString());
}
return res.join(' ');
}