Module

Data.ByteString.Encoding

Package
purescript-bytestrings
Repository
f-f/purescript-bytestrings

Conversions between String and ByteString using different encodings

#fromString Source

fromString :: Encoding -> String -> Maybe ByteString

Encode a string to bytes under the given encoding, failing on unrepresentable inputs. Use fromStringLossy for a lenient decoder that doesn't fail. Per encoding, the input is rejected when:

  • UTF8: it contains an unpaired surrogate
  • UTF16LE: never fails
  • Latin1: a UTF-16 code unit is above 0xFF
  • Hex: the length is odd, or a character is not a hex digit
  • Base64: anything but a canonically padded RFC 4648 encoding, e.g. a length not a multiple of four, a character outside the standard and URL-safe alphabets, = other than as final padding (at most two), or nonzero unused bits in the final data character.

#fromStringLossy Source

fromStringLossy :: Encoding -> String -> ByteString

Encode a string to bytes under the given encoding, lossily. Substitutions per encoding:

  • UTF8: substitutes U+FFFD for an unpaired surrogate
  • UTF16LE: exact
  • Latin1: masks each code unit to its low byte
  • Hex: stops at the first non-hex character or a trailing lone digit
  • Base64: skips any character outside the alphabet (whitespace, =, anything else)

#toString Source

toString :: Encoding -> ByteString -> Maybe String

Decode bytes to a string under the given encoding, returning Nothing when a strict UTF decoder rejects the input (invalid UTF-8, or an odd-length UTF-16 buffer)

#toStringLossy Source

toStringLossy :: Encoding -> ByteString -> String

Decode bytes to a string under the given encoding, total in every case. Per encoding:

  • UTF8: substitutes U+FFFD for invalid bytes
  • UTF16LE: drops a trailing odd byte and decodes the even prefix
  • Latin1, Hex, Base64: never fail