(define (bytevector-builder)
(let ((builder
- (make-sequence-builder (lambda () (allocate-bytevector 16))
+ (make-sequence-builder allocate-bytevector
bytevector-length
+ bytevector-u8-ref
bytevector-u8-set!
+ 16
bytevector-builder:finish-build)))
(lambda (#!optional object)
(cond ((default-object? object) ((builder 'build)))
\f
;;;; Builder for vector-like sequences
-(define (make-sequence-builder make-buffer sequence-length sequence-set!
- finish-build)
+(define (make-sequence-builder make-sequence sequence-length sequence-ref
+ sequence-set! buffer-length finish-build)
;; This is optimized to minimize copying, so it wastes some space.
(let ((buffers)
(buffer)
(define (reset!)
(set! buffers '())
- (set! buffer (make-buffer))
+ (set! buffer (make-sequence buffer-length))
(set! index 0)
unspecific)
(define (new-buffer!)
(set! buffers (cons (cons buffer index) buffers))
- (set! buffer (make-buffer))
+ (set! buffer (make-sequence buffer-length))
(set! index 0)
unspecific)
((not (pair? buffers)) (fix:+ n index))))
(define (append-element! element)
- (if (not (fix:< index (sequence-length buffer)))
+ (if (not (fix:< index buffer-length))
(new-buffer!))
(sequence-set! buffer index element)
(set! index (fix:+ index 1))
unspecific)
(define (append-sequence! sequence)
- (if (fix:> index 0)
- (new-buffer!))
- (set! buffers (cons (cons sequence (sequence-length sequence)) buffers))
- unspecific)
+ (let ((length (sequence-length sequence)))
+ (if (fix:<= length buffer-length)
+ (do ((i 0 (fix:+ i 1)))
+ ((not (fix:< i length)))
+ (append-element! (sequence-ref sequence i)))
+ (begin
+ (if (fix:> index 0)
+ (new-buffer!))
+ (set! buffers (cons (cons sequence length) buffers))
+ unspecific))))
(define (build)
(finish-build (reverse (cons (cons buffer index) buffers))))
(define (string-builder)
(let ((builder
- (make-sequence-builder (lambda () (full-string-allocate 16))
+ (make-sequence-builder full-string-allocate
string-length
+ string-ref
string-set!
+ 16
string-builder:finish-build)))
(lambda (#!optional object)
(cond ((default-object? object) ((builder 'build)))