findMalformedUtf8SequenceOffset

function findMalformedUtf8SequenceOffset(bytes, offset?): number;

Finds the first malformed UTF-8 sequence in the given bytes, from the given offset.

This rejects everything the Unicode standard rejects: unexpected continuation bytes, overlong encodings, encoded surrogates, code points above U+10FFFF and truncated sequences.

Parameters

ParameterTypeDescription
bytes| ReadonlyUint8Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike>The byte array to validate.
offset?numberThe offset from which to start validating (defaults to 0).

Returns

number

The offset at which the first malformed sequence starts, or -1 if the bytes are well-formed.

Example

findMalformedUtf8SequenceOffset(new Uint8Array([0xe8, 0xaa, 0x9e])); // -1 ("語")
findMalformedUtf8SequenceOffset(new Uint8Array([0x61, 0xc0, 0x80])); // 1 (overlong)

On this page