[Openmcl-devel] byte-strings
Ben
ben at medianstrip.net
Fri Aug 20 12:57:37 PDT 2004
hi,
i'm writing an application which wants to treat strings as byte arrays
for the purposes of FFI. more specifically, i'd like to be able to
quickly copy the contents of strings in and out of foreign character
arrays. i don't really care about what the character representation
is, i'm happy treating everyting as bits.
under CMUCL and Franz i can directly pass strings to memcpy, which
does the trick. this works even with 16-bit strings in Franz,
provided i pass in the correct byte-length. under Lispworks there is
a function "fli:replace-foreign-array" which i'm hoping will do the
trick for me.
of course there is nothing stopping me from doing (using UFFI)
(defun copy-buf (str buf len src-offset 0 buf-offset 0))
(declare (optimize (speed 3) (safety 0))
(type string str)
(type array-char buf)
(type fixnum len src-offset buf-offset)
(dynamic-extent str buf len))
(typecase str
(simple-string
(loop for i fixnum from 0 below len
do
(setf (deref-array buf '(:array :char) (+ i buf-offset))
(char-code (schar str (+ i src-offset))))))
(string
(loop for i fixnum from 0 below len
do
(setf (deref-array buf '(:array :char) (+ i buf-offset))
(char-code (char str (+ i src-offset))))))))
but i find memcpy is much, much faster. is there any way to get at
the underlying bits of a string? e.g. is "with-cstr" using memcpy,
and can i pass it a buffer to stuff the string into?
thanks in advance, B
More information about the Openmcl-devel
mailing list