utf8LengthStrict function

int utf8LengthStrict(
  1. String s
)

The exact number of UTF-8 bytes s encodes to, or โˆ’1 when it holds an unpaired surrogate and therefore cannot be encoded at all.

The strict twin of utf8Length, and what the encoder sizes a fixlen_word with: it settles both questions โ€” how many bytes and is this encodable โ€” in one pass over the code units, so a string field can be written straight into the caller's output buffer with no transcode buffer in between (CORELIB_PLAN ยง6.6: the codec allocates no payload storage). utf8Length keeps its lenient answer for callers that only want a size.

It reads through String.codeUnitAt rather than codeUnits, which would allocate a view object per call.

Implementation

int utf8LengthStrict(String s) => utf8LengthStrictUnits(s.codeUnits);