+
+#+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)))
+
+#+sbcl
+(defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
+ (declare (type sb-sys: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 (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)))
+ (sb-kernel:copy-from-system-area sap 0
+ result (* sb-vm:vector-data-offset
+ sb-vm:n-word-bits)
+ (* length sb-vm:n-byte-bits))
+ result)))
+
+
+(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)
+ (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)
+ (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)
+ (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))