r10140: add sb-unicode patch
[uffi.git] / src / strings.lisp
index dc42fa3f58fa26a462a2f6c0afba2fcc11ca73da..b8965b151068cbcee667cb54060abe1648431205 100644 (file)
@@ -157,10 +157,11 @@ that LW/CMU automatically converts strings from c-calls."
        nil
      (if (eq ,locale :none)
         (fast-native-to-string ,obj ,length)
-       (excl:native-to-string
-       ,obj 
-       ,@(when length (list :length length))
-       :truncate (not ,null-terminated-p))))
+       (values
+       (excl:native-to-string
+        ,obj 
+        ,@(when length (list :length length))
+        :truncate (not ,null-terminated-p)))))
   #+lispworks
   `(if (fli:null-pointer-p ,obj)
        nil
@@ -188,7 +189,10 @@ 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 ,@(if length
+                   `((ccl:%str-from-ptr ,obj ,length))
+                   `((ccl:%get-cstring ,obj))))
   )
 
 
@@ -295,7 +299,7 @@ that LW/CMU automatically converts strings from c-calls."
        (setf (char result i) (code-char (system:sap-ref-8 sap i))))
       result)))
 
-#+sbcl
+#+(and sbcl (not sb-unicode))
 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
   (declare (type sb-sys:system-area-pointer sap))
   (locally
@@ -318,6 +322,27 @@ that LW/CMU automatically converts strings from c-calls."
                                    (* length sb-vm:n-byte-bits))
       result)))
 
+#+(and sbcl sb-unicode)
+(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)))
+      (cond
+        (null-terminated-p
+        (let ((casted (sb-alien:cast
+                       (sb-alien:sap-alien sap (* char)) 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
+           (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)))))
+
 (eval-when (:compile-toplevel :load-toplevel :execute)
    (def-function "strlen"
      ((str (* :unsigned-char)))
@@ -344,4 +369,4 @@ that LW/CMU automatically converts strings from c-calls."
   (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 :byte) i))))))
+        (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))