[gambit-list] Output Vector Ports

Marc Feeley feeley at iro.umontreal.ca
Tue Mar 31 20:04:19 EDT 2009


On 31-Mar-09, at 3:46 PM, vasil wrote:

> with-output-to-u8vector CREATES a new u8-vector, it is not  
> destructive!
> so do like this:
>
>> (let ((s (make-u8vector 3 0)))
>   (set! s (with-output-to-u8vector s (lambda ()
>      (write-u8 65)
>      (write-u8 45)
>      (force-output))))
>   s)
> #u8(0 0 0 65 45)
>

Indeed a new vector will be created and the "initial vector" passed to  
with-output-to-u8vector is simply the prefix of the new vector.

You may wonder why this API was chosen.  Well:

1) Internally a vector must be created and initialized, so it can save  
time to allow an initializing vector when the prefix is known.

2) This parameter can be used to specify other options, such as the  
character encoding.  For example:

 > (map (lambda (x)
          (cons x
                (with-output-to-u8vector
                 (list char-encoding: x)
                 (lambda () (write-char (integer->char 255))))))
        '(ISO-8859-1 UTF-8 UTF-16BE UTF-16LE UTF-16 UTF UCS-2 UCS-4))
((ISO-8859-1 . #u8(255))
  (UTF-8 . #u8(195 191))
  (UTF-16BE . #u8(0 255))
  (UTF-16LE . #u8(255 0))
  (UTF-16 . #u8(255 254 255 0))
  (UTF . #u8(239 187 191 195 191))
  (UCS-2 . #u8(255 254 255 0))
  (UCS-4 . #u8(255 254 0 0 255 0 0 0)))

Marc




More information about the Gambit-list mailing list