unicodeToList
Splits a string into Unicode code points, correctly handling surrogate pairs.
Lecture
Dart Strings are sequences of UTF-16 code units, not
characters. Calling s.split('') splits by code unit — fine
for plain ASCII/Latin text, but wrong for anything outside the Basic
Multilingual Plane, like most emoji: those are represented as a
surrogate pair (two code units), so a naive split tears
a single emoji into two broken halves.
unicodeToList fixes that by iterating the string's
.runes — Unicode code points — and turning each one back
into its own single-character String. The result is the list
of code points, matching FxTS's unicodeToArray, which splits
by code point rather than UTF-16 unit. Code points are not always what a
person sees as one character: flags, ZWJ emoji sequences and combining
marks are still split into several pieces, because grapheme clusters are
not handled.
The FxTS name works too: unicodeToArray is an alias of
unicodeToList, which takes the Dart spelling because it
returns a List.
Demo 1 · Naive split vs. unicodeToList
Demo 2 · Reversing and counting correctly
Building on unicodeToList, a naive-split reverse would
mangle the emoji; this one doesn't:
Try it yourself
Exercise: count how many code points are in this string (it contains an emoji).
fxSplit — split on a plain char iterable ·
reverse — reverse an Iterable, same surrogate-pair care applies to strings ·
countBy — used above to tally characters ·
fxIdentity — used above as the counting key