unicodeToList
Splits a string into user-perceived characters, 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 characters a person actually sees when reading the string, matching
FxTS's unicodeToList, which splits by code point rather than
UTF-16 unit.
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 user-perceived characters are in this string (it contains an emoji).