[cmucl-imp] string-to-octets

Raymond Toy toy.raymond at gmail.com
Sun Oct 30 04:28:21 CET 2011


On 10/29/11 4:18 AM, Helmut Eller wrote:
> I'm wondering how STRING-TO-OCTETS can be used with a fixed sized byte
> buffer.  For example we want to write a long string to a byte stream
> using a fixed size byte buffer.  STRING-TO-OCTETS seems to return the
> buffer and the number of bytes written.  E.g.
>
> (let ((buffer (make-array 20 :element-type '(unsigned-byte 8)))
>       (string (make-string 100  :initial-element #\a)))
>   (stream:string-to-octets string :external-format :utf32 :buffer buffer))
>
>
Is this better:

(let ((buffer (make-array 19 :element-type '(unsigned-byte 8)))
           (string (make-string 100  :initial-element #\u+3b2)))
       (multiple-value-bind (b p i last)
           (stream:string-to-octets string :external-format :utf8
:buffer buffer)
         (values b p i last)))

#(206 178 206 178 206 178 206 178 206 178 206 178 206 178 206 178 206
178 206)
19
9
18

9 is the number of characters converted, 18 is the index+1 in the buffer
where the last valid octet was placed.  The last octet is the first
octect of the 2-octet utf8 encoding for #\u+3b2.

If no buffer is specified, the entire string is converted and the buffer
is returned along with the number of octets generated.  (Kind of
redundant now.)

Ray


More information about the cmucl-imp mailing list