You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/restructure/buffer.rkt

41 lines
1.2 KiB
Racket

#lang restructure/racket
(require "number.rkt" "utils.rkt" "stream.rkt")
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee
|#
(define-subclass RestructureBase (Buffer [_length #xffff])
(define/override (decode stream [parent #f])
(define len (resolveLength _length stream parent))
(send stream readBuffer len))
(define/override (size [val #f] [parent #f])
(when val (unless (bytes? val)
(raise-argument-error 'Buffer:size "bytes" val)))
(if val
(bytes-length val)
(resolveLength _length val parent)))
(define/override (encode stream buf [parent #f])
(when (Number? _length)
(send _length encode stream (bytes-length buf)))
(send stream writeBuffer buf)))
(define (bytes->Buffer bstr)
(define b (+Buffer (bytes-length bstr)))
(send b decode (+DecodeStream bstr))
b)
#;(test-module
(require "stream.rkt")
(define stream (+DecodeStream #"\2BCDEF"))
(define S (+String uint8 'utf8))
(check-equal? (send S decode stream) "BC")
(define os (+EncodeStream))
(send S encode os "Mike")
(check-equal? (send os dump) #"\4Mike")
(check-equal? (send (+String) size "foobar") 6))