readCodePoint method

int readCodePoint()

Consumes a single Unicode code unit and returns it.

This works like readChar, except that it automatically handles UTF-16 surrogate pairs. Specifically, if the next two code units form a surrogate pair, consumes them both and returns the corresponding Unicode code point.

If next two characters are not a surrogate pair, the next code unit is returned as-is, even if it's an unpaired surrogate.

Implementation

int readCodePoint() {
  final first = readChar();
  if (!isHighSurrogate(first)) return first;

  final next = peekChar();
  if (next == null || !isLowSurrogate(next)) return first;

  readChar();
  return decodeSurrogatePair(first, next);
}