awwx.ws

binary1

Binary data

http://awwx.ws/binary1.arc:

(extend type (x) (scheme.bytes? x)
  'binary)

(extend ac-literal (x) (errsafe:isa x 'binary)
  scheme-t)

(extend coerce (x totype . args) (is totype 'binary)
  (case (type x)
    binary x
    string (scheme.string->bytes/utf-8 x)
           (err "Can't coerce" x type)))

(def binary (x)
  (coerce x 'binary))

(extend coerce (x totype . args) (isa x 'binary)
  (case totype
    binary x
    string (scheme.bytes->string/utf-8 x)
           (err "Can't coerce" x type)))

(extend len (x) (isa x 'binary)
  (scheme.bytes-length x))

This hack adds PLT Scheme’s byte strings as a “binary” type in Arc.

I suspect that having a separate data type to store binary data is going in the wrong direction for Arc, but I needed to get the byte length of a Unicode encoded string to generate a Content-Length header and this was a quick and lazy way to do that.

Prerequisites

This hack depends on arc3.1 and scheme0.

License

Same as Arc.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com