13 Encoding Functions

The encoding/ library converts between Bytes and strings, numbers or collections with a named codec. The codec is always given; there is no default.

Codec encoding/encode encoding/decode
hex Bytes to lowercase hexadecimal text Hexadecimal text of either case to Bytes
utf8 A string to its UTF-8 bytes UTF-8 bytes to a string
i64le, i64be An integer to its 8 bytes 8 bytes to an integer
f64le, f64be A number to the 8 bytes of its float 8 bytes to a float
bencode Integers, strings, Bytes, lists and maps to Bytes Bytes to integers, Bytes, lists and maps

A number codec names its byte order: le writes the least significant byte first (little-endian) and be the most significant byte first (big-endian).

Options are supplied as the final argument, after the value. An option that the codec does not define is an error. null is not a valid value for a codec.

13.1. encoding/encode

Function

(encoding/encode codec value) → string
bytes
(encoding/encode codec value options) → string
bytes

Encodes value with codec. hex takes Bytes and returns two lowercase hexadecimal digits for each byte. utf8 takes a string and returns its bytes. No codec defines an option for encoding.

DUST
(encoding/encode "hex" #b64 "F8gHWw==")
(encoding/encode "hex" #b64 "")
(encoding/encode "hex" #b64 "AP8=" {})
(encoding/encode "utf8" "hi")
(encoding/encode "utf8" "")
(encoding/encode "utf8" "hé")
(encoding/encode "utf8" ~"a,(+ 1 2)")
(map (functions/partial :encoding/encode "hex") [#b64 "AA==" #b64 "/w=="])
(encoding/encode "hex" null)
(encoding/encode "hex" "text")
(encoding/encode "utf8" null)
(encoding/encode "utf8" #b64 "aGk=")
(encoding/encode "hex" #b64 "AA==" {errors "replace"})
(encoding/encode "utf8" "hi" {errors "replace"})
(encoding/encode "hex" #b64 "AA==" "opts")
(encoding/encode "base32" #b64 "AA==")
(encoding/encode null #b64 "AA==")
(encoding/encode "hex")
=
DUST Result
"17c8075b"
""
"00ff"
#b64 "aGk="
#b64 ""
#b64 "aMOp"
#b64 "YTM="
["00" "ff"]
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected a string"}
#err {<mismatch type>
      message "expected a string"}
#err {<invalid value>
      message "unsupported codec option"}
#err {<invalid value>
      message "unsupported codec option"}
#err {<mismatch type>
      message "codec options must be a map"}
#err {<invalid value>
      message "unknown codec"}
#err {<mismatch type>
      message "expected a string"}
#err {<arity mismatch>
      message "wrong number of arguments"}

i64le and i64be take an integer and return its 8 bytes in two's complement. f64le and f64be take a float and return the 8 bytes of its IEEE 754 binary64 form. They also take an integer, as the float equal to it; an integer beyond 253 in magnitude, which a float might not hold exactly, is an error. To make a list of numbers into Bytes, encode each number and join the results with bytes/concat.

DUST
(encoding/encode "f64le" 1.5)
(encoding/encode "f64be" 1.5)
(encoding/encode "f64le" 1)
(encoding/encode "f64be" -0.0)
(encoding/encode "i64le" 1)
(encoding/encode "i64be" 258)
(encoding/encode "i64le" -9223372036854775808)
(encoding/encode "i64be" 9223372036854775807)
(bytes/concat (encoding/encode "i64be" 1) (encoding/encode "f64be" 2.5))
(map (functions/partial :encoding/encode "i64be") [0 -1])
(encoding/encode "i64le" 1 {})
(encoding/encode "f64le" 9007199254740993)
(encoding/encode "i64le" 1.5)
(encoding/encode "f64le" "1.5")
(encoding/encode "f64le" null)
(encoding/encode "i64le" 1 {errors "replace"})
(encoding/encode "f64" 1.5)
(encoding/encode "f32le" 1.5)
=
DUST Result
#b64 "AAAAAAAA+D8="
#b64 "P/gAAAAAAAA="
#b64 "AAAAAAAA8D8="
#b64 "gAAAAAAAAAA="
#b64 "AQAAAAAAAAA="
#b64 "AAAAAAAAAQI="
#b64 "AAAAAAAAAIA="
#b64 "f/////////8="
#b64 "AAAAAAAAAAFABAAAAAAAAA=="
[#b64 "AAAAAAAAAAA=" #b64 "//////////8="]
#b64 "AQAAAAAAAAA="
#err {<invalid value>
      message "integer is not exact as a float"}
#err {<mismatch type>
      message "codec requires an integer"}
#err {<mismatch type>
      message "codec requires a number"}
#err {<mismatch type>
      message "codec requires a number"}
#err {<invalid value>
      message "unsupported codec option"}
#err {<invalid value>
      message "unknown codec"}
#err {<invalid value>
      message "unknown codec"}

13.2. encoding/decode

Function

(encoding/decode codec value) → value
(encoding/decode codec value options) → value

Reverses encoding/encode for codec. hex takes a string of hexadecimal digit pairs of either case and returns Bytes; an odd length or any other character is an error. utf8 takes Bytes and returns a string.

UTF-8 decoding is strict: bytes that are not well-formed UTF-8 are an error. With the option {errors "replace"}, each byte of a malformed sequence becomes U+FFFD instead. {errors "strict"} names the default.

DUST
(encoding/decode "hex" "17c8075b")
(encoding/decode "hex" "17C8075B")
(encoding/decode "hex" "")
(encoding/decode "utf8" #b64 "aGk=")
(encoding/decode "utf8" #b64 "")
(encoding/decode "utf8" #b64 "aMOp")
(encoding/decode "utf8" #b64 "aGk=" {errors "strict"})
(encoding/decode "utf8" #b64 "/2hp" {errors "replace"})
(encoding/decode "utf8" #b64 "aP/+aQ==" {errors "replace"})
(encoding/decode "utf8" #b64 "4oI=" {errors "replace"})
(encoding/decode "utf8" #b64 "aGk=" {})
(map (fn [value] (encoding/decode "utf8" value {errors "replace"})) [#b64 "aGk=" #b64 "/w=="])
(= #b64 "F8gHWw==" (encoding/decode "hex" (encoding/encode "hex" #b64 "F8gHWw==")))
(= "hé" (encoding/decode "utf8" (encoding/encode "utf8" "hé")))
(encoding/decode "hex" "17c")
(encoding/decode "hex" "zz")
(encoding/decode "hex" "0x17")
(encoding/decode "hex" #b64 "AA==")
(encoding/decode "hex" null)
(encoding/decode "utf8" #b64 "/2hp")
(encoding/decode "utf8" #b64 "/2hp" {errors "strict"})
(encoding/decode "utf8" #b64 "7aCA")
(encoding/decode "utf8" #b64 "wIA=")
(encoding/decode "utf8" "hi")
(encoding/decode "utf8" null)
(encoding/decode "utf8" #b64 "aGk=" {errors "ignore"})
(encoding/decode "utf8" #b64 "aGk=" {errors 1})
(encoding/decode "utf8" #b64 "aGk=" {mode "x"})
(encoding/decode "hex" "00" {errors "replace"})
(encoding/decode "latin1" #b64 "aGk=")
=
DUST Result
#b64 "F8gHWw=="
#b64 "F8gHWw=="
#b64 ""
"hi"
""
"hé"
"hi"
"�hi"
"h��i"
"��"
"hi"
["hi" "�"]
true
true
#err {<invalid value>
      message "hex text must have an even length"}
#err {<invalid value>
      message "invalid hex text"}
#err {<invalid value>
      message "invalid hex text"}
#err {<mismatch type>
      message "expected a string"}
#err {<mismatch type>
      message "expected a string"}
#err {<invalid value>
      message "invalid UTF-8"}
#err {<invalid value>
      message "invalid UTF-8"}
#err {<invalid value>
      message "invalid UTF-8"}
#err {<invalid value>
      message "invalid UTF-8"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<invalid value>
      message "errors must be strict or replace"}
#err {<mismatch type>
      message "expected a string"}
#err {<invalid value>
      message "unsupported codec option"}
#err {<invalid value>
      message "unsupported codec option"}
#err {<invalid value>
      message "unknown codec"}

The number codecs take Bytes of exactly 8 bytes; any other count is an error. i64le and i64be return an integer. f64le and f64be return a float; bytes that spell an infinity or a NaN are an error, because DUST floats are finite.

DUST
(encoding/decode "f64le" #b64 "AAAAAAAA+D8=")
(encoding/decode "f64be" #b64 "P/gAAAAAAAA=")
(encoding/decode "f64le" (encoding/encode "f64le" 2.0))
(encoding/decode "i64be" #b64 "AAAAAAAAAQI=")
(encoding/decode "i64le" #b64 "AQIAAAAAAAA=")
(encoding/decode "i64le" (encoding/encode "i64le" -9223372036854775808))
(encoding/decode "i64be" #b64 "//////////8=")
(= 12345.678 (encoding/decode "f64be" (encoding/encode "f64be" 12345.678)))
(encoding/decode "f64le" #b64 "AAAAAAAA8H8=")
(encoding/decode "f64le" #b64 "AAAAAAAA+H8=")
(encoding/decode "i64le" #b64 "AQI=")
(encoding/decode "f64be" #b64 "")
(encoding/decode "f64le" "AAAAAAAA+D8=")
(encoding/decode "i64le" null)
(encoding/decode "i64le" 258)
=
DUST Result
1.5
1.5
2.0
258
513
-9223372036854775808
-1
true
#err {<float overflow>
      message "floating-point overflow"}
#err {<float overflow>
      message "floating-point overflow"}
#err {<invalid value>
      message "codec requires 8 bytes"}
#err {<invalid value>
      message "codec requires 8 bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}

13.3. Bencode

encoding/encode "bencode" accepts signed i64 integers, text, Bytes, lists and maps. Text is encoded as UTF-8. Dictionaries have text keys and are written in raw byte order. Floats, booleans, null, functions and cyclic collections cannot be represented. Decoding returns DUST values directly, without an intermediate Document. Byte-string values remain Bytes by default; <utf8> decodes them as strict UTF-8 text. Dictionary keys must always be valid UTF-8.

Decoding rejects truncated values, duplicate keys, noncanonical integer/length spellings, integers outside i64, and trailing bytes. Unsorted dictionaries are accepted for protocol interoperability. Add <canonical> to require sorted keys; combine it with text decoding as <canonical utf8>. Collections nest to any depth, and allocations count toward the memory budget.

(def message {op "eval" code "(+ 1 2 3)"})
(def wire (encoding/encode "bencode" message))
(tests/equal "d4:code9:(+ 1 2 3)2:op4:evale" (encoding/decode "utf8" wire))
(tests/equal message (encoding/decode "bencode" wire <utf8>))
(tests/equal #b64 "AP8=" (encoding/decode "bencode" #b64 "MjoA/w=="))
(io/stdout/println "Bencode checks passed")
Signature Result
(encoding/bencode/encode value [options]) Same as encoding/encode "bencode"; no encoding options
(encoding/bencode/decode bytes [options]) One decoded value; options utf8 and canonical
(encoding/bencode/read reader [options]) One value, or null at clean EOF
(encoding/bencode/read-frame reader [options]) One complete nREPL dictionary as its original Bytes
(encoding/bencode/write writer value [options]) Encode and write a value; return the byte count

A reader is a buffered reader or a callback accepting a byte count and returning up to that many Bytes, null at EOF, or an error. Reads stop at the value's boundary, so the next call can read another message. read-frame retains the original encoding and accepts arbitrary-size wire integers. It builds no value, so a frame can nest to any depth that memory allows. Converting such a frame to a DUST value still requires integers to fit i64.

Messages have no size limit of their own. The one bound comes from Bytes: one bencode string, and one frame from read-frame, becomes one Bytes value, and a Bytes value holds at most 2,147,483,392 bytes, the longest string LuaJIT represents. A string whose declared length passes that bound fails before its bytes are read. A frame fails as soon as the bytes it needs pass the bound: before a string whose declared length would take it past, or at any later byte, including the closing e.

A writer is a stream or a callback accepting Bytes and returning the positive number written. Partial writes are completed automatically. Writing does not close the stream. Stream options contain timeout; reads also accept utf8 and canonical. The timeout applies to each underlying I/O call. Callbacks control their own I/O deadlines. After a malformed, truncated or timed-out stream read, close the connection; decoding cannot resume a partial value.