Documentation

Init.Data.String.Lossy

@[inline]

Decodes a sequence of characters from their UTF-8 representation, replacing invalid byte sequences with � (U+FFFD, the Unicode replacement character). Whenever decoding fails at some byte, that byte and all immediately following continuation bytes are replaced by a single �.

Examples:

  • String.fromUTF8Lossy [0x4C, 0xE2, 0x88, 0x83, 0x4E].toByteArray = "L∃N"
  • String.fromUTF8Lossy [0x61, 0xFF, 0x62].toByteArray = "a�b", since 0xFF never occurs in UTF-8.
  • String.fromUTF8Lossy [0x61, 0xE2, 0x88, 0x62].toByteArray = "a�b", since the three-byte sequence starting with 0xE2 is truncated.
  • String.fromUTF8Lossy [0x61, 0xED, 0xA0, 0x80, 0x62].toByteArray = "a�b", since 0xED 0xA0 0x80 would encode the surrogate code point U+D800, which is not a valid Char.
  • String.fromUTF8Lossy [0x61, 0x80, 0x80, 0x80, 0x62].toByteArray = "a�b", since a run of stray continuation bytes is replaced by a single �.
  • String.fromUTF8Lossy [0xFF, 0xFF].toByteArray = "��"
Equations
Instances For