splitWords function

Iterable<String> splitWords(
  1. String input
)

Implementation

Iterable<String> splitWords(String input) sync* {
  final it = input.codeUnits.iterator;
  var buffer = <int>[];
  var canBreak = false;
  while (it.moveNext()) {
    if (isUppercase(it.current)) {
      if (canBreak) {
        yield String.fromCharCodes(buffer);
        buffer.clear();
        buffer.add(it.current);
        canBreak = false;
      } else {
        buffer.add(it.current);
        canBreak = !isUppercase(it.current);
      }
    } else if (RegExp('[^a-zA-Z0-9]')
        .hasMatch(String.fromCharCode(it.current))) {
      if (buffer.isNotEmpty) {
        yield String.fromCharCodes(buffer);
        buffer.clear();
        canBreak = false;
      }
    } else {
      canBreak = true;
      buffer.add(it.current);
    }
  }
  yield String.fromCharCodes(buffer);
}