groupIntoWords static method

List<String> groupIntoWords(
  1. String text
)

Groups a String into words based upon the defined regexps.

Implementation

static List<String> groupIntoWords(String text) {
  if (text.isEmpty) return [''];

  StringBuffer sb = StringBuffer();
  List<String> words = [];
  bool isAllCaps = !text.contains(RegExp('[a-z]'));

  for (int i = 0; i < text.length; i++) {
    String char = String.fromCharCode(text.codeUnitAt(i));
    String? nextChar = (i + 1 == text.length
        ? null
        : String.fromCharCode(text.codeUnitAt(i + 1)));

    if (_symbolRegex.hasMatch(char)) {
      continue;
    }

    sb.write(char);

    bool isEndOfWord = nextChar == null ||
        (_upperAlphaRegex.hasMatch(nextChar) && !isAllCaps) ||
        _symbolRegex.hasMatch(nextChar);

    if (isEndOfWord) {
      words.add(sb.toString());
      sb.clear();
    }
  }

  return words;
}