Skip to content

introduce ext:string-to-octets, ext:octets-to-string, ext:string-size-in-octets, ext:vector-size-in-chars

Both babel and flexi-streams have functions that transcode strings to octets and vice versa using the specified encoding. We can do that in a more performant manner by incorporating sequence streams with encoders and decoders, i.e

(defun transcode-string (string)
  ;; one character may take up to four octets
  (let* ((array (make-array (* 4 (length string)) :element-type '(unsigned-byte 8)
                                                  :fill-pointer 0))
         (stream (ext:make-sequence-output-stream array :external-format :utf-8)))
    (format stream string)
    ;; terminating 0
    (vector-push-extend 0 array)
    (si:make-foreign-data-from-array array)))



(defun call-printf (string)
  (let ((str (transcode-string string)))
    (ffi:c-inline (str) (:pointer-void) :void "printf(#0);")))


(call-printf (format nil "HELLO涅~%"))

Example written for #790 (closed).

Edited by Daniel Kochmański