words function
Separate a String into a List of words.
Implementation
List<String> words(String original) {
if (original.isEmpty ||
listSubtractAll(original.codeUnits, kWhitespaceCodes).isEmpty) {
return [];
}
if (listIntersect(kWhitespaceCodes, original.codeUnits).isEmpty) {
return [original];
}
List<String> result = [];
String currentWord = '';
for (int i = 0; i < original.length; i++) {
String currentChar = original.substring(i, i + 1);
int currentCode = original.substring(i, i + 1).codeUnits.first;
if (!kWhitespaceCodes.contains(currentCode)) {
currentWord += currentChar;
} else {
if (currentWord.isNotEmpty) {
result.add(currentWord);
currentWord = '';
}
continue;
}
}
//add last word
if (currentWord.isNotEmpty) {
result.add(currentWord);
}
return result;
}