firstGraphemeCluster function

({String first, String rest}) firstGraphemeCluster(
  1. String s
)

Gets the first grapheme cluster from a string.

Returns a record with the first grapheme and the remaining string. For simplicity, this treats each rune as a grapheme cluster. A proper implementation would use Unicode grapheme cluster breaking rules.

Implementation

({String first, String rest}) firstGraphemeCluster(String s) {
  if (s.isEmpty) {
    return (first: '', rest: '');
  }

  final it = uni.graphemes(s).iterator;
  if (!it.moveNext()) return (first: '', rest: '');
  final first = it.current;
  final rest = s.substring(first.length);
  return (first: first, rest: rest);
}