12 Bytes Functions

The bytes/ library builds, searches, and combines binary values.

12.1. Bytes values

A Bytes value is a sequence of bytes of any length. In DUST, RON, and EDN it uses the #b64 prefix with its bytes in base64 text. TRON stores the bytes directly as a bin value.

#b64 "F8gHWw=="
{"#b64": "F8gHWw=="}

The payload is canonical padded standard base64 (RFC 4648 section 4): a multiple of four characters from A-Z, a-z, 0-9, +, and /, with = only as the last one or two characters, and with the unused bits of the last character zero. Each byte sequence thus has exactly one spelling. The empty value is #b64 "". Any other payload produces the parse error invalid tag at the tag.

DUST
#b64 "F8gHWw=="
→
RON
#b64 F8gHWw==
=
DUST Result
#b64 "F8gHWw=="
DUST
{a #b64 "aGk="
 l [#b64 "" #b64 "AQID"]}
→
RON
a #b64 aGk=
l [#b64 "" #b64 AQID]
=
DUST Result
{a #b64 "aGk="
 l [#b64 "" #b64 "AQID"]}
DUST
#b64 "aGk"
=
DUST Result
#err <invalid tag>
DUST
#b64 "a==="
=
DUST Result
#err <invalid tag>
DUST
#b64 "aG-_"
=
DUST Result
#err <invalid tag>
DUST
#b64 "aGl="
=
DUST Result
#err <invalid tag>
DUST
#b64 1
=
DUST Result
#err <invalid tag>

Two Bytes values are equal when they have the same bytes. A Bytes value never equals a string, and comparing the two is a type mismatch. Bytes order by their byte values from the first byte, and a prefix sorts before a longer value.

DUST
(= #b64 "aGk=" #b64 "aGk=")
(= #b64 "aGk=" #b64 "aGo=")
(= #b64 "" #b64 "")
(= #b64 "" (bytes/slice #b64 "aGk=" 1 1))
(= [#b64 "aGk="] [#b64 "aGk="])
(= {a #b64 "aGk="} {a #b64 "aGo="})
(comparisons/not= #b64 "aGk=" #b64 "aGk=")
(= #b64 "aGk=" "aGk=")
=
DUST Result
true
false
true
true
true
false
false
#err {<mismatch type>
      message "equality requires matching types"}
DUST
(compare #b64 "AA==" #b64 "AQ==")
(compare #b64 "AQ==" #b64 "AA==")
(compare #b64 "AA==" #b64 "AA==")
(compare #b64 "" #b64 "AA==")
(compare #b64 "AA==" #b64 "AAA=")
(compare #b64 "/w==" #b64 "AAA=")
(< #b64 "fw==" #b64 "gA==")
(<= #b64 "gA==" #b64 "gA==")
(> #b64 "AA==" #b64 "")
(sort [#b64 "/w==" #b64 "AAA=" #b64 "" #b64 "AA=="])
(distinct [#b64 "aGk=" #b64 "aGk=" #b64 ""])
(compare #b64 "AA==" "AA==")
=
DUST Result
-1
1
0
-1
-1
1
true
true
true
[#b64 "" #b64 "AA==" #b64 "AAA=" #b64 "/w=="]
[#b64 "aGk=" #b64 ""]
#err {<mismatch type>
      message "ordering requires matching types"}

Core functions read a Bytes value as its byte values, the integers 0 to 255: count and empty? use the number of bytes, get selects one byte, and the functions that read a list or a pointer, such as map, reduce, first, and lists/filter, read the bytes in order. Their results are lists, because a function can return a value that is not a byte; bytes/of makes a list of byte values into Bytes again. Functions that change a list in place do not accept Bytes.

An empty result is #b64 "". null means that a value is absent, such as a search that finds nothing. The bitwise operators and the codecs reject null.

12.2. bytes/of

Function

(bytes/of values) → bytes

Returns Bytes with one byte for each integer in the list values. null is an empty list. Each element must be an integer from 0 to 255; any other element is an error, and no value is truncated. An element is a byte value, not a number to store: (bytes/of [1.5]) is an error. The encoding library writes a number as its 8 bytes.

DUST
(bytes/of [104 105])
(bytes/of [])
(bytes/of null)
(bytes/of [0 255])
(bytes/of (map (fn [x] (+ x 1)) #b64 "AAEC"))
(bytes/of [256])
(bytes/of [-1])
(bytes/of [1.5])
(bytes/of ["a"])
(bytes/of [null])
(bytes/of #b64 "aGk=")
(bytes/of "hi")
=
DUST Result
#b64 "aGk="
#b64 ""
#b64 ""
#b64 "AP8="
#b64 "AQID"
#err {<invalid value>
      message "byte values must be 0 to 255"}
#err {<invalid value>
      message "byte values must be 0 to 255"}
#err {<mismatch type>
      message "byte values must be integers"}
#err {<mismatch type>
      message "byte values must be integers"}
#err {<mismatch type>
      message "byte values must be integers"}
#err {<mismatch type>
      message "expected a list or null"}
#err {<mismatch type>
      message "expected a list or null"}

12.3. bytes/concat

Function

(bytes/concat buffers*) → bytes

Returns the bytes of every argument joined in order. Without arguments it returns #b64 "". A null argument adds no bytes.

DUST
(bytes/concat)
(bytes/concat #b64 "aGk=")
(bytes/concat #b64 "aGk=" #b64 "IQ==")
(bytes/concat #b64 "aGk=" null #b64 "" #b64 "IQ==")
(bytes/concat null)
(bytes/concat null null)
(= (bytes/concat) #b64 "")
(bytes/concat #b64 "aGk=" "hi")
(bytes/concat [104 105])
=
DUST Result
#b64 ""
#b64 "aGk="
#b64 "aGkh"
#b64 "aGkh"
#b64 ""
#b64 ""
true
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}

12.4. bytes/slice

Function

(bytes/slice b start) → bytes
(bytes/slice b start end) → bytes

Returns the bytes from index start up to, but not including, index end. end defaults to the byte count. The bounds must be integers with 0 ≤ start ≤ end ≤ (count b). Any other bounds produce #err <bounds index>; a slice never returns fewer bytes than its bounds name.

DUST
(bytes/slice #b64 "F8gHWw==" 1 3)
(bytes/slice #b64 "F8gHWw==" 1)
(bytes/slice #b64 "F8gHWw==" 0)
(bytes/slice #b64 "F8gHWw==" 0 4)
(bytes/slice #b64 "F8gHWw==" 4)
(bytes/slice #b64 "F8gHWw==" 2 2)
(bytes/slice #b64 "" 0 0)
(bytes/slice #b64 "F8gHWw==" 1 99)
(bytes/slice #b64 "F8gHWw==" 5)
(bytes/slice #b64 "F8gHWw==" 3 1)
(bytes/slice #b64 "F8gHWw==" -1 2)
(bytes/slice #b64 "F8gHWw==" 0 9223372036854775807)
(bytes/slice #b64 "F8gHWw==" 1.0 2)
(bytes/slice #b64 "F8gHWw==" 0 null)
(bytes/slice null 0)
(bytes/slice "text" 0 1)
=
DUST Result
#b64 "yAc="
#b64 "yAdb"
#b64 "F8gHWw=="
#b64 "F8gHWw=="
#b64 ""
#b64 ""
#b64 ""
#err {<bounds index>
      message "slice bound outside bytes"}
#err {<bounds index>
      message "slice bound outside bytes"}
#err {<bounds index>
      message "slice start after its end"}
#err {<bounds index>
      message "slice bound outside bytes"}
#err {<bounds index>
      message "slice bound outside bytes"}
#err {<mismatch type>
      message "slice bounds must be integers"}
#err {<mismatch type>
      message "slice bounds must be integers"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}

12.5. bytes/index-of

Function

(bytes/index-of b needle) → integer
null
(bytes/index-of b needle start) → integer
null

Returns the index of the first occurrence of the Bytes needle in b at or after start, or null if there is none. start defaults to zero and is limited to the range from zero to the byte count, as in strings/index-of. An empty needle is found at the limited start.

DUST
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==")
(bytes/index-of #b64 "F8gHWw==" #b64 "yAc=")
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" 2)
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" 3)
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" -5)
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" 99)
(bytes/index-of #b64 "F8gHWw==" #b64 "/w==")
(bytes/index-of #b64 "YWFh" #b64 "YWE=" 1)
(bytes/index-of #b64 "F8gHWw==" #b64 "")
(bytes/index-of #b64 "F8gHWw==" #b64 "" 3)
(bytes/index-of #b64 "F8gHWw==" #b64 "" 99)
(bytes/index-of #b64 "F8gHWw==" #b64 "" -9)
(bytes/index-of #b64 "" #b64 "")
(bytes/index-of #b64 "" #b64 "AA==")
(bytes/index-of #b64 "AAAB" #b64 "AAE=")
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" 9223372036854775807)
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" 1.0)
(bytes/index-of #b64 "F8gHWw==" #b64 "Bw==" null)
(bytes/index-of #b64 "F8gHWw==" 7)
(bytes/index-of null #b64 "Bw==")
(bytes/index-of #b64 "F8gHWw==" null)
=
DUST Result
2
1
2
null
2
null
null
1
0
3
4
0
0
null
1
null
#err {<mismatch type>
      message "search start must be an integer"}
#err {<mismatch type>
      message "search start must be an integer"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}

12.6. bytes/and

Function

(bytes/and a b more*) → bytes

Returns the bitwise AND of two or more Bytes values of equal length. Values of different lengths are an error.

DUST
(bytes/and #b64 "/w==" #b64 "Dw==")
(bytes/and #b64 "/w8=" #b64 "D/8=" #b64 "AwM=")
(bytes/and #b64 "" #b64 "")
(bytes/and #b64 "/w==" #b64 "Dw8=")
(bytes/and #b64 "/w==")
(bytes/and #b64 "/w==" null)
(bytes/and null #b64 "/w==")
(bytes/and #b64 "/w==" 15)
=
DUST Result
#b64 "Dw=="
#b64 "AwM="
#b64 ""
#err {<invalid value>
      message "bytes/and requires bytes of equal length"}
#err {<arity mismatch>
      message "wrong number of arguments"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}

12.7. bytes/or

Function

(bytes/or a b more*) → bytes

Returns the bitwise OR of two or more Bytes values of equal length. Values of different lengths are an error.

DUST
(bytes/or #b64 "8A==" #b64 "Dw==")
(bytes/or #b64 "AQA=" #b64 "AAI=" #b64 "BAA=")
(bytes/or #b64 "" #b64 "")
(bytes/or #b64 "/w==" #b64 "Dw8=")
(bytes/or #b64 "/w==")
(bytes/or #b64 "/w==" null)
=
DUST Result
#b64 "/w=="
#b64 "BQI="
#b64 ""
#err {<invalid value>
      message "bytes/or requires bytes of equal length"}
#err {<arity mismatch>
      message "wrong number of arguments"}
#err {<mismatch type>
      message "expected bytes"}

12.8. bytes/xor

Function

(bytes/xor a b more*) → bytes

Returns the bitwise exclusive OR of two or more Bytes values of equal length. Values of different lengths are an error.

DUST
(bytes/xor #b64 "/w==" #b64 "Dw==")
(bytes/xor #b64 "F8gHWw==" #b64 "F8gHWw==")
(bytes/xor #b64 "AQI=" #b64 "AwQ=" #b64 "BQY=")
(= #b64 "F8gHWw==" (bytes/xor (bytes/xor #b64 "F8gHWw==" #b64 "qlWqVQ==") #b64 "qlWqVQ=="))
(bytes/xor #b64 "" #b64 "")
(bytes/xor #b64 "/w==" #b64 "Dw8=")
(bytes/xor #b64 "/w==")
(bytes/xor null #b64 "/w==")
=
DUST Result
#b64 "8A=="
#b64 "AAAAAA=="
#b64 "BwA="
true
#b64 ""
#err {<invalid value>
      message "bytes/xor requires bytes of equal length"}
#err {<arity mismatch>
      message "wrong number of arguments"}
#err {<mismatch type>
      message "expected bytes"}

12.9. bytes/not

Function

(bytes/not b) → bytes

Returns b with every bit inverted.

DUST
(bytes/not #b64 "AP8=")
(bytes/not #b64 "")
(= #b64 "F8gHWw==" (bytes/not (bytes/not #b64 "F8gHWw==")))
(bytes/not null)
(bytes/not "AP8=")
(bytes/not #b64 "AA==" #b64 "AA==")
=
DUST Result
#b64 "/wA="
#b64 ""
true
#err {<mismatch type>
      message "expected bytes"}
#err {<mismatch type>
      message "expected bytes"}
#err {<arity mismatch>
      message "wrong number of arguments"}

12.10. bytes/shift-left

Function

(bytes/shift-left b n) → bytes

Returns b shifted left by n bits as one number, with the first byte most significant. The byte count stays the same. Bits shifted past the first byte are lost, and the vacated bits are zero. n must be a nonnegative integer. A shift of the bit length or more returns all zero bytes, and a shift of zero returns equal Bytes.

DUST
(bytes/shift-left #b64 "AQI=" 4)
(bytes/shift-left #b64 "AQI=" 8)
(bytes/shift-left #b64 "AQI=" 9)
(bytes/shift-left #b64 "AQI=" 15)
(bytes/shift-left #b64 "AQI=" 16)
(bytes/shift-left #b64 "AQI=" 9223372036854775807)
(bytes/shift-left #b64 "AQI=" 0)
(bytes/shift-left #b64 "" 3)
(bytes/shift-left #b64 "gA==" 1)
(bytes/shift-left #b64 "AQI=" -1)
(bytes/shift-left #b64 "AQI=" 1.0)
(bytes/shift-left #b64 "AQI=" null)
(bytes/shift-left null 1)
=
DUST Result
#b64 "ECA="
#b64 "AgA="
#b64 "BAA="
#b64 "AAA="
#b64 "AAA="
#b64 "AAA="
#b64 "AQI="
#b64 ""
#b64 "AA=="
#err {<invalid value>
      message "shift count must be nonnegative"}
#err {<mismatch type>
      message "shift count must be an integer"}
#err {<mismatch type>
      message "shift count must be an integer"}
#err {<mismatch type>
      message "expected bytes"}

12.11. bytes/shift-right

Function

(bytes/shift-right b n) → bytes

Returns b shifted right by n bits as one number, with the first byte most significant. The shift is logical: the vacated bits are zero. The byte count stays the same, and bits shifted past the last byte are lost. n must be a nonnegative integer. A shift of the bit length or more returns all zero bytes.

DUST
(bytes/shift-right #b64 "AQI=" 4)
(bytes/shift-right #b64 "AQI=" 8)
(bytes/shift-right #b64 "gAA=" 15)
(bytes/shift-right #b64 "gAA=" 16)
(bytes/shift-right #b64 "AQI=" 9223372036854775807)
(bytes/shift-right #b64 "AQI=" 0)
(bytes/shift-right #b64 "" 3)
(bytes/shift-right #b64 "AQ==" 1)
(= #b64 "AAI=" (bytes/shift-right (bytes/shift-left #b64 "AQI=" 8) 8))
(bytes/shift-right #b64 "AQI=" -1)
(bytes/shift-right null 1)
=
DUST Result
#b64 "ABA="
#b64 "AAE="
#b64 "AAE="
#b64 "AAA="
#b64 "AAA="
#b64 "AQI="
#b64 ""
#b64 "AA=="
true
#err {<invalid value>
      message "shift count must be nonnegative"}
#err {<mismatch type>
      message "expected bytes"}