codePointLength function

int codePointLength(
  1. String str
)

The number of characters (Unicode code points) of the string. Unlike String.length, a character outside of the Basic Multilingual Plane, like most emoji, counts once.

Implementation

int codePointLength(String str) {
  final length = str.length;
  var count = length;
  for (var i = 0; i < length - 1; i++) {
    final char = str.codeUnitAt(i);
    if (char >= 0xD800 &&
        char <= 0xDBFF &&
        (str.codeUnitAt(i + 1) & 0xFC00) == 0xDC00) {
      count--;
      i++;
    }
  }
  return count;
}