Encoding functions

Encoding functions convert between text and bytes using a named character encoding, and handle the encoded-word form used in email headers.

These functions matter when a system doesn't speak UTF-8, such as a mainframe export in Shift_JIS, a legacy CSV in Windows-1252, or an email header carrying an accented name. A wrong encoding produces the garbled text that shows up as é where an é should be.

ENCODING ISN'T ESCAPING

Encoding is about which bytes represent a character. Escaping is about which characters are safe inside a format. An accented name sent to a CSV needs both, and neither substitutes for the other.

FEATURE AVAILABILITY

WEL is currently available to select customers. Contact your Customer Success Representative to confirm whether it is available in your workspace.

Text to bytes and back

The following functions convert between text and bytes with a specified character encoding:

encode_string

Converts a string to Binary using a named encoding.

Use encode_string instead of Binary() when text can contain non-ASCII characters. Binary() raises E008 for non-ASCII input instead of selecting an encoding.

text
encode_string(text, encoding)
ParameterDescription
textThe string to encode.
encodingAn encoding name, such as UTF-8 or Shift_JIS.
Encode an accented string as UTF-8

The following example encodes an accented string as UTF-8 bytes:

Formula

text
encode_string('café', 'UTF-8')

Output

text
0x"636166C3A9"

The é occupies two bytes, C3A9, so the 4-code-point string café encodes to 5 bytes. length counts code points, grapheme_length counts user-perceived characters, and byte_length counts bytes. The three can differ for anything outside ASCII.

decode_string

Converts Binary to a string using a named encoding.

text
decode_string(binary, encoding, options)
ParameterDescription
binaryThe bytes to decode.
encodingThe encoding the bytes are in.
optionsOptional map to control how invalid sequences are handled. invalid: 'replace' substitutes a replacement character, or 'skip' drops them.

An invalid sequence raises an error by default. Silently replacing bytes instead would turn a data problem into a corrupted record that nobody notices.

Decode UTF-8 bytes back to a string

The following example decodes UTF-8 bytes back to the original string:

Formula

text
decode_string(encode_string('café', 'UTF-8'), 'UTF-8')

Output

text
café

decode_string_by_content_type

Decodes bytes using the charset named in an HTTP Content-Type header, rather than one you supply.

Use it when handling an HTTP response whose encoding you don't control.

text
decode_string_by_content_type(binary, content_type, options)
ParameterDescription
binaryThe bytes to decode.
content_typeThe Content-Type header value.
optionsOptional map specifying a fallback encoding to use when the header names none.

Check before converting

The following functions test whether text or bytes support a specified encoding:

ascii_string?

Returns true if the string contains only ASCII characters.

text
ascii_string?(text)
ParameterDescription
textThe string to test.
A plain string is ASCII

The following example tests a plain ASCII string:

Formula

text
ascii_string?('plain')

Output

text
true
An accented string isn't ASCII

The following example tests a string containing an accented character:

Formula

text
ascii_string?('café')

Output

text
false

convertible_to_encoding?

Returns true if the given encoding can represent the string without losing anything.

Check convertibility before sending text to a system with a restricted character set. It's better to reject a record here than to deliver one with characters silently replaced.

text
convertible_to_encoding?(text, encoding)
ParameterDescription
textThe string to test.
encodingThe target encoding.
Test whether an accented string fits ASCII

The following example tests whether an accented string can convert to ASCII without loss:

Formula

text
convertible_to_encoding?('café', 'ASCII')

Output

text
false

valid_string?

Returns true if the bytes are valid in the given encoding and can be decoded.

text
valid_string?(binary, encoding)
ParameterDescription
binaryThe bytes to test.
encodingThe encoding to test against.
Valid UTF-8 bytes

The following example tests bytes that are valid UTF-8:

Formula

text
valid_string?(Binary('AB'), 'UTF-8')

Output

text
true

encodings

Lists the supported encoding names. Pass true to include synonyms.

text
encodings(include_synonyms)
ParameterDescription
include_synonymsOptional Boolean.

Email headers

An email header can only carry ASCII. RFC 2047 wraps anything outside that range in encoded-words that look like =?UTF-8?B?...?=.

encode_mime_header

Encodes text as RFC 2047 encoded-words for an email header.

text
encode_mime_header(text, encoding, mode)
ParameterDescription
textThe header text to encode.
encodingThe character encoding to use.
modeOptional encoding mode.
Encode an accented name for a header

The following example encodes an accented string as an RFC 2047 encoded-word:

Formula

text
encode_mime_header('café', 'UTF-8')

Output

text
=?UTF-8?B?Y2Fmw6k=?=

decode_mime_header

Decodes RFC 2047 encoded-words in an email header back to text.

text
decode_mime_header(text)
ParameterDescription
textThe header value to decode.
Decode an RFC 2047 encoded-word

The following example decodes an RFC 2047 encoded-word back to text:

Formula

text
decode_mime_header('=?UTF-8?B?SGVsbG8=?=')

Output

text
Hello

Use case: Prepare a name for a legacy ASCII-only system

The destination exclusively accepts ASCII. Use convertible_to_encoding? to check each name before sending, and route the ones that won't fit rather than delivering corrupted text:

Input

json
{
  "names": ["Kalani Park", "Noam Gupta", "José Álvarez"]
}

Formula

text
let parts = _.names >> partition_by(n ~> convertible_to_encoding?(n, 'ASCII'))
do {
  send: parts >> at(0),
  needs_review: parts >> at(1)
}

Output

json
{
  "send": ["Kalani Park", "Noam Gupta"],
  "needs_review": ["José Álvarez"]
}

Check convertibility before transmission so a person can correct the record instead of receiving corrupted text such as Jos? lvarez.

Last updated: