+
+#+scl
+;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
+;; so have to iteratively copy from sap
+(defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
+ (declare (type system:system-area-pointer sap))
+ (locally
+ (declare (optimize (speed 3) (safety 0)))
+ (let ((null-terminated-length
+ (when null-terminated-p
+ (loop
+ for offset of-type fixnum upfrom 0
+ until (zerop (system:sap-ref-8 sap offset))
+ finally (return offset)))))
+ (if length
+ (if (and null-terminated-length
+ (> (the fixnum length) (the fixnum null-terminated-length)))
+ (setq length null-terminated-length))
+ (setq length null-terminated-length)))
+ (let ((result (make-string length)))
+ (dotimes (i length)
+ (declare (type fixnum i))
+ (setf (char result i) (code-char (system:sap-ref-8 sap i))))
+ result)))
+
+#+(and sbcl (not sb-unicode))
+(defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
+ (declare (type sb-sys:system-area-pointer sap)
+ (type (or null fixnum) length))
+ (locally
+ (declare (optimize (speed 3) (safety 0)))
+ (let ((null-terminated-length
+ (when null-terminated-p
+ (loop
+ for offset of-type fixnum upfrom 0
+ until (zerop (sb-sys:sap-ref-8 sap offset))
+ finally (return offset)))))
+ (if length
+ (if (and null-terminated-length
+ (> (the fixnum length) (the fixnum null-terminated-length)))
+ (setq length null-terminated-length))
+ (setq length null-terminated-length)))
+ (let ((result (make-string length)))
+ (funcall *system-copy-fn* sap 0 result +system-copy-offset+
+ (* length +system-copy-multiplier+))
+ result)))
+
+#+(and sbcl sb-unicode)
+(defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
+ (declare (type sb-sys:system-area-pointer sap)
+ (type (or null fixnum) length))
+ (locally
+ (declare (optimize (speed 3) (safety 0)))
+ (cond
+ (null-terminated-p
+ (let ((casted (sb-alien:cast (sb-alien:sap-alien sap (* char))
+ #+sb-unicode sb-alien:utf8-string
+ #-sb-unicode sb-alien:c-string)))
+ (if length
+ (copy-seq (subseq casted 0 length))
+ (copy-seq casted))))
+ (t
+ (let ((result (make-string length)))
+ ;; this will not work in sb-unicode
+ (funcall *system-copy-fn* sap 0 result +system-copy-offset+
+ (* length +system-copy-multiplier+))
+ result)))))
+
+
+(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 len)
+ (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
+ (type char-ptr-def s))
+ (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)))))
+
+#+(and allegro ics)
+(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 (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))))))