X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=src%2Fstrings.lisp;h=f935e1bd479db0b479068a6e812432dbfebe44d9;hb=0dbc9821c1d1f665a914585dcef97199e8334bf3;hp=250df2e67734ba18bcaa27b274e606977ea411c4;hpb=ebedde4e67b858b1f65c5eb4dc7bc45978ed1e40;p=uffi.git diff --git a/src/strings.lisp b/src/strings.lisp index 250df2e..f935e1b 100644 --- a/src/strings.lisp +++ b/src/strings.lisp @@ -156,16 +156,17 @@ that LW/CMU automatically converts strings from c-calls." `(if (zerop ,obj) nil (if (eq ,locale :none) - (fast-native-to-string ,obj) - (excl:native-to-string - ,obj - ,@(when length (list :length length)) - :truncate (not ,null-terminated-p)))) + (fast-native-to-string ,obj ,length) + (values + (excl:native-to-string + ,obj + ,@(when length (list :length length)) + :truncate (not ,null-terminated-p))))) #+lispworks `(if (fli:null-pointer-p ,obj) nil (if (eq ,locale :none) - (fast-native-to-string ,obj) + (fast-native-to-string ,obj ,length) (fli:convert-from-foreign-string ,obj ,@(when length (list :length length)) @@ -188,25 +189,33 @@ that LW/CMU automatically converts strings from c-calls." #+mcl `(if (ccl:%null-ptr-p ,obj) nil - (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil))) + #+(and mcl (not openmcl)) (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)) + #+openmcl (let ((str (ccl:%get-cstring ,obj))) + ,(if length '(subseq str 0 length) 'str))) ) (defmacro allocate-foreign-string (size &key (unsigned t)) - #+(or cmu scl) + #+ignore (let ((array-def (gensym))) `(let ((,array-def (list 'alien:array 'c-call:char ,size))) (eval `(alien:cast (alien:make-alien ,,array-def) ,(if ,unsigned '(* (alien:unsigned 8)) '(* (alien:signed 8))))))) + + #+(or cmu scl) + `(alien:make-alien ,(if unsigned + '(alien:unsigned 8) + '(alien:signed 8)) + ,size) + #+sbcl - (let ((array-def (gensym))) - `(let ((,array-def (list 'sb-alien:array 'char ,size))) - (eval `(sb-alien:cast (sb-alien:make-alien ,,array-def) - ,(if ,unsigned - '(* (sb-alien:unsigned 8)) - '(* (sb-alien:signed 8))))))) + `(sb-alien:make-alien ,(if unsigned + '(sb-alien:unsigned 8) + '(sb-alien:signed 8)) + ,size) + #+lispworks `(fli:allocate-foreign-object :type ,(if unsigned @@ -223,6 +232,15 @@ that LW/CMU automatically converts strings from c-calls." `(new-ptr ,size) ) +(defun foreign-string-length (foreign-string) + #+allegro `(ff:foreign-strlen ,foreign-string) + #-allegro + `(loop with size = 0 + until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null) + do (incf size) + finally return size)) + + (defmacro with-foreign-string ((foreign-string lisp-string) &body body) (let ((result (gensym))) `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string)) @@ -303,53 +321,30 @@ that LW/CMU automatically converts strings from c-calls." (* length sb-vm:n-byte-bits)) result))) - -(def-function "strlen" - ((str (* :unsigned-char))) - :returning :unsigned-int) +(eval-when (:compile-toplevel :load-toplevel :execute) + (def-function "strlen" + ((str (* :unsigned-char))) + :returning :unsigned-int)) (def-type char-ptr-def (* :unsigned-char)) #+(or lispworks (and allegro (not ics))) -(defun fast-native-to-string (s) +(defun fast-native-to-string (s len) (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0)) (type char-ptr-def s)) - (let* ((len (strlen s)) - (str (make-string len))) + (let* ((len (or len (strlen s))) + (str (make-string len))) (declare (fixnum len) (type (simple-array (signed-byte 8) (*)) str)) (dotimes (i len str) - (setf (aref str i) (uffi:deref-array s '(:array :char) i))))) + (setf (aref str i) + (uffi:deref-array s '(:array :char) i))))) #+(and allegro ics) -(defun fast-native-to-string (s) +(defun fast-native-to-string (s len) (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0)) (type char-ptr-def s)) - (let* ((len (strlen s)) - (str (make-string len))) - (declare (fixnum len)) - (dotimes (i len str) - (setf (schar str i) (ensure-char-character - (uffi:deref-array s '(:array :char) i)))))) - -#+ignore -(defun fast-native-to-string (s) - (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0) (debug 0)) - (type char-ptr-def s)) - (let* ((len (strlen s)) - (len4 (the fixnum (ash (the fixnum len) -2))) - (end4 (the fixnum (ash (the fixnum len4) 2))) - (remainder (the fixnum (- len end4))) - (str (make-string len)) - (str4 str)) - (declare (type fixnum len len4 end4 remainder) - (type (simple-array (signed-byte 32) (*)) str4) - (type (simple-array (signed-byte 8) (*)) str)) - (dotimes (i len4) - (declare (fixnum i)) - (setf (aref str4 i) (uffi:deref-array s '(:array :int) i))) - (dotimes (i remainder) - (declare (fixnum i)) - (setf (aref str end4) (uffi:deref-array s '(:array :char) end4)) - (incf end4)) - str)) + (let* ((len (or len (strlen s))) + (str (make-string len))) + (dotimes (i len str) + (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))