Welcome to the CHICKEN Scheme pasting service

iterrogo: bytevector append and copy! added by alexcharlton on Wed Sep 17 17:03:16 2014

;; These depend on foreign and srfi-4
;; The use of memcpy makes them an order of magnitude faster than those found in r7rs, IIRC

(define (bytevector-copy! to at from
                          #!optional (start 0) (end (u8vector-length from)))
  (when (or (< start 0) (>= start end) (> end (u8vector-length from)))
    (error 'bytevector-copy "Bad from range" (list start end)))
  (when (or (< at 0) (> at (u8vector-length to)))
    (error 'bytevector-copy "Bad at" at))
  (when (< (- (u8vector-length to) at)
           (- end start))
    (error 'bytevector-copy
           "Bytevector being copied into is not long enough to hold range" to))
  ((foreign-lambda* void ((u8vector to) (u8vector from) (size_t len)
                          (size_t to_off) (size_t from_off))
     "memcpy(&to[to_off], &from[from_off], len);")
   to from (- end start) at start))

(define (bytevector-append bv . bvs)
  (when (and (null? bvs) (not (list? bv)))
    (error 'bytevector-append "When applied to a single element, a list is expected"
           bv))
  (let* ((bvs (if (null? bvs)
                  bv
                  (cons bv bvs)))
         (length (fold (lambda (bv n)
                         (+ (u8vector-length bv) n))
                       0 bvs))
         (result (make-u8vector length)))
    (let loop ((vectors bvs) (i 0))
      (if (null? vectors)
          result
          (begin
            (let* ((v (car vectors))
                   (len (u8vector-length v)))
              ((foreign-lambda* void ((u8vector to) (u8vector from) (size_t len)
                                      (size_t off))
                 "memcpy(&to[off], from, len);")
               result v len i)
              (loop (cdr vectors) (+ i len))))))))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Name of the bot in #chicken:
Visually impaired? Let me spell it for you (wav file) download WAV