Choosing a Text Decoding Error Policy

Invalid encoded input can be rejected, be replaced with a visible marker, or be handled under another explicitly documented rule.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Choose by purpose
  2. Preserve the distinction from editable text
  3. Compare what each policy preserves on a broken byte
  4. Choose behavior from the consequence of losing original bytes

Choose by purpose

For a data import that will be edited and re-exported, rejecting an incomplete sequence preserves evidence that reading failed. A display tool may use a replacement marker so surrounding readable text remains visible. UTF-8 sequence boundaries explain one failure source.

Preserve the distinction from editable text

With replacement enabled, a malformed sequence and an original replacement character may look identical afterward. Keep raw bytes or a separate diagnostic when the difference matters. Function contracts define which information the result promises.[1]

Compare what each policy preserves on a broken byte

In UTF-8, the byte sequence 41 FF 42 contains valid A and B separated by an invalid byte. Strict decoding can report the location of the error. Replacement can return A, a replacement character and B. Ignoring it returns AB and conceals the missing information. See byte-offset diagnostics.

Bytes: 41 FF 42
Strict: error at invalid byte
Replace: A�B
Ignore: AB

Choose behavior from the consequence of losing original bytes

A preview may need a readable replacement, while a signed text record or import field needs reliable preservation or an explicit failure. Retain the raw input when the exact source is part of an audit. Test invalid data before and after valid records.

References

  1. IETF, RFC 3629: UTF-8, a Transformation Format of ISO 10646 ↩