~williewillus/racket-rfc8949

a87c782a7fcf9429a3127f05c330cf9c03d52805 — Vincent Lee 3 years ago 5a83fd3
Support writing vectors and sequences
2 files changed, 17 insertions(+), 2 deletions(-)

M decode.rkt
M encode.rkt
M decode.rkt => decode.rkt +1 -1
@@ 180,7 180,7 @@
  (unless (bytes? data)
    (error "Bignum payload must be bytes"))
  (for/fold ([acc 0])
            ([(chunk idx) (in-indexed data)])
            ([(chunk idx) (in-indexed (in-bytes data))])
    (define shift (* 8 (- (sub1 (bytes-length data))
                          idx)))
    (bitwise-ior acc (arithmetic-shift chunk shift))))

M encode.rkt => encode.rkt +16 -1
@@ 72,6 72,20 @@
  (when (> len u64-max)
    (write-break out)))

(define (cbor-write-vector vec out)
  (define len (vector-length vec))
  (write-argument out 4 len)
  (for ([v (in-vector vec)])
    (cbor-write v out))
  (when (> len u64-max)
    (write-break out)))

(define (cbor-write-seq seq out)
  (write-byte (combine 4 31) out)
  (for ([v seq])
    (cbor-write v out))
  (write-break out))

(define (cbor-write-map m out)
  (define len (length m))
  (write-argument out 5 len)


@@ 116,8 130,9 @@
    [(bytes? v) (cbor-write-bytes v out)]
    [(string? v) (cbor-write-string v out)]
    [(list? v) (cbor-write-list v out)]
    [(vector? v) (cbor-write-vector v out)]
    [(hash? v) (cbor-write-map v out)]
    [(sequence? v) (cbor-write-seq v out)]
    [(tagged-value?) (cbor-write-tag v out)] ; todo handle interpreted values
    [(simple-value? v) (cbor-write-simple-value v out)]
    [else (error "Don't know how to write" v)]))
; todo support writing vectors as lists, sequences as indefinite lists