assertIsWellFormedUtf8Bytes

function assertIsWellFormedUtf8Bytes(bytes, offset?): void;

Asserts that the given bytes, from the given offset, form well-formed UTF-8.

TextDecoder silently replaces malformed sequences with the replacement character (U+FFFD); this function throws instead. See findMalformedUtf8SequenceOffset for what counts as malformed.

Parameters

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

Returns

void

Throws

If a malformed sequence is found, reporting its offset.

Example

assertIsWellFormedUtf8Bytes(new Uint8Array([0xe8, 0xaa, 0x9e])); // Passes ("語")
assertIsWellFormedUtf8Bytes(new Uint8Array([0xc0, 0x80]));       // Throws error (overlong)

On this page