Next: , Previous: lisp-string-to-foreign, Up: Strings


with-foreign-string, with-foreign-strings

Syntax

— Macro: with-foreign-string (var-or-vars string &rest args) &body body

— Macro: with-foreign-strings (bindings) &body body

var-or-vars ::= var | (var &optional octet-size-var) bindings ::= {(var-or-vars string &rest args)}*

Arguments and Values

var, byte-size-var
A symbol.
string
A Lisp string.
body
A list of forms to be executed.

Description

The with-foreign-string macro will bind var to a newly allocated foreign string containing string. Args is passed to the underlying foreign-string-alloc call.

If octet-size-var is provided, it will be bound the length of foreign string in octets including the null terminator.

Examples

  CFFI> (with-foreign-string (foo "12345")
          (foreign-funcall "strlen" :pointer foo :int))
  => 5
   
  CFFI> (let ((array (coerce #(84 117 114 97 110 103 97)
                             '(array (unsigned-byte 8)))))
          (with-foreign-string (foreign-string array)
            (foreign-string-to-lisp foreign-string)))
  => "Turanga"

See Also

foreign-string-alloc
with-foreign-pointer-as-string