Version 1.8.2: Test suite and more functions for foreign string encoding debian-1.8.2-1 v1.8.2
authorKevin Rosenberg <kevin@rosenberg.net>
Mon, 8 Feb 2010 04:56:42 +0000 (21:56 -0700)
committerKevin Rosenberg <kevin@rosenberg.net>
Mon, 8 Feb 2010 04:56:42 +0000 (21:56 -0700)
ChangeLog
debian/changelog
src/i18n.lisp
src/package.lisp
src/strings.lisp
tests/.gitignore
tests/foreign-loader.lisp
tests/foreign-var.lisp
tests/i18n.lisp [new file with mode: 0644]
tests/union.lisp
uffi-tests.asd

index eba11e6f37ae24f9da7060ef787a07c4bf5a9a7b..6da74a2beac67542f3715fe23ff506c70bde26fa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2010-02-07 Kevin Rosenberg <kevin@rosenberg.net>
+       * Version 1.8.2
+       * src/i18n.lisp: Rename function to 
+       foreign-encoded-octet-count. Fix errors.
+       * tests/i18n.lisp: New file. i18n tests fine
+       on AllegroCL 8/16 bits, SBCL unicode/non-unicode,
+       CCL, and Lispworks 6
+       * src/strings.lisp: Fix an error with decoding
+       strings on CCL.
+
 2010-02-06 Kevin Rosenberg <kevin@rosenberg.net>
        * src/i18n.lisp: Add new function string-to-octets
 
index 9e5a2add655cd64390ab779648f4975acfe3835a..83371568197e7000a960e550d1b7ade48b387c5c 100644 (file)
@@ -1,3 +1,9 @@
+cl-uffi (1.8.2-1) unstable; urgency=low
+
+  * New upstream
+
+ -- Kevin M. Rosenberg <kmr@debian.org>  Sun, 07 Feb 2010 21:17:13 -0700
+
 cl-uffi (1.8.1-1) unstable; urgency=low
 
   * New upstream
index 08c16ad0b54eb962721764deaa4eea9c4a34feaa..1f2bb1ad9a33286fd402304d0a0f8357e7b81458 100644 (file)
@@ -55,42 +55,105 @@ encoding.")
   (mapcar 'car *foreign-encoding-mapping*)
   "List of normalized names of external formats support by underlying implementation.")
 
-(defun implementation-foreign-encoding (normalized)
+(defun lookup-foreign-encoding (normalized)
   (cdr (assoc normalized *foreign-encoding-mapping* :test 'eql)))
 
-(defun foreign-encoded-string-octets (str &key foreign-encoding)
-  "Returns the octets required to represent the string when passed to a ~
-foreign function."
-  ;; AllegroCL, CCL, and Lispworks give correct value without converting
-  ;; to external-format. CLISP, like SBCL, requires conversion with external-
-  ;; format
-  (length #+(and sbcl sb-unicode)
-          (sb-ext:string-to-octets
-           str
-           :external-format (or foreign-encoding
-                                *default-foreign-encoding*
-                                :utf-8))
-          #-(and sbcl sb-unicode) str))
-
-(defun string-to-octets (str &key foreign-encoding)
+(defmacro string-to-octets (str &key (encoding *default-foreign-encoding*))
   "Converts a Lisp string to a vector of octets."
   #-(or allegro lispworks openmcl sbcl)
-  (declare (ignore foreign-encoding))
+  (declare (ignore encoding))
   #-(or allegro lispworks openmcl sbcl)
-  (map-into (make-array len :element-type '(unsigned-byte 8))
+  (map-into (make-array (length str) :element-type '(unsigned-byte 8))
             #'char-code str)
 
   #+allegro
-  (excl:string-to-native str :external-format foreign-encoding :null-terminate nil)
+  (let ((fe (gensym "FE-"))
+        (ife (gensym "IFE-"))
+        (s (gensym "STR-")))
+    `(let* ((,fe ,encoding)
+            (,ife (when ,fe (lookup-foreign-encoding ,fe)))
+            (,s ,str))
+       (values
+        (if ,ife
+            (excl:string-to-octets ,s :external-format ,ife :null-terminate nil)
+            (excl:string-to-octets ,s :null-terminate nil)))))
 
   #+(or lispworks openmcl)
   ;; simply reading each char-code from the LENGTH of string handles multibyte characters
   ;; just fine in testing LW 6.0 and CCL 1.4
-  (map-into (make-array len :element-type '(unsigned-byte 8))
+  (map-into (make-array (length str) :element-type '(unsigned-byte 8))
             #'char-code str)
 
   #+sbcl
-  (sb-ext:string-to-native str :external-format foreign-encoding)
+  (let ((fe (gensym "FE-"))
+        (ife (gensym "IFE-"))
+        (s (gensym "STR-")))
+    `(let* ((,fe ,encoding)
+            (,ife (when ,fe (lookup-foreign-encoding ,fe)))
+            (,s ,str))
+       (if ,ife
+           (sb-ext:string-to-octets ,s :external-format ,ife)
+           (sb-ext:string-to-octets ,s))))
 
 )
 
+(defmacro octets-to-string (octets &key (encoding *default-foreign-encoding*))
+  "Converts a vector of octets to a Lisp string."
+  #-(or allegro lispworks openmcl sbcl)
+  (declare (ignore encoding))
+  #-(or allegro lispworks openmcl sbcl)
+  (let ((out (gensym "OUT-"))
+        (code (gensym "CODE-")))
+    `(with-output-to-string (,out)
+       (loop for ,code across ,octets
+          do (write-char (code-char ,code) ,out))))
+
+  #+allegro
+  (let ((fe (gensym "FE-"))
+        (ife (gensym "IFE-"))
+        (oct (gensym "OCTETS-")))
+    `(let* ((,fe ,encoding)
+            (,ife (when ,fe (lookup-foreign-encoding ,fe)))
+            (,oct ,octets))
+       (values
+        (if ,ife
+            (excl:octets-to-string ,oct :external-format ,ife)
+            (excl:octets-to-string ,oct)))))
+
+  #+(or lispworks openmcl)
+  ;; With LW 6.0 and CCL 1.4, writing multibyte character just one octet at a time tests fine
+  (let ((out (gensym "OUT-"))
+        (code (gensym "CODE-")))
+    `(with-output-to-string (,out)
+       (loop for ,code across ,octets
+          do (write-char (code-char ,code) ,out))))
+
+  #+sbcl
+  (let ((fe (gensym "FE-"))
+        (ife (gensym "IFE-"))
+        (oct (gensym "OCTETS-")))
+    `(let* ((,fe ,encoding)
+            (,ife (when ,fe (lookup-foreign-encoding ,fe)))
+            (,oct ,octets))
+       (if ,ife
+           (sb-ext:octets-to-string ,oct :external-format ,ife)
+           (sb-ext:octets-to-string ,oct))))
+
+)
+
+(defun foreign-encoded-octet-count (str &key (encoding *default-foreign-encoding*))
+  "Returns the octets required to represent the string when passed to a ~
+foreign function."
+  ;; AllegroCL 8-bit, CCL, and Lispworks give correct value without converting
+  ;; to external-format. AllegroCL 16-bit, SBCL, and CLISP requires conversion
+  ;; with external-format
+
+  #+(or (and allegro ics) (and sbcl sb-unicode) (and clisp i18n))
+  (length (string-to-octets str :encoding encoding))
+
+  #-(or (and allegro ics) (and sbcl sb-unicode) (and clisp i18n))
+  (declare (ignore encoding))
+  #-(or (and allegro ics) (and sbcl sb-unicode) (and clisp i18n))
+  (length str)
+
+)
index 318e68a279e5c54a30fb8e95bfa25f8d5d447bc6..905809369314d2482ecf85851b64a5b59292b0df 100644 (file)
@@ -85,8 +85,7 @@
    #:no-i18n
    #:*default-foreign-encoding*
    #:*foreign-encodings*
-   #:foreign-encoded-string-octets
+   #:foreign-encoded-octet-count
    #:string-to-octets
+   #:octets-to-string
    ))
-
-
index 209116428cce7e905c1210de6dc72675ab7c014e..eedc1b6e241b717df37af94ac2c94608c21ecbad 100644 (file)
@@ -208,9 +208,9 @@ that LW/CMU automatically converts strings from c-calls."
 (defmacro convert-to-foreign-string (obj &optional foreign-encoding)
   #+allegro
   (let ((stored (gensym "STR-"))
-        (ef (gensym "EF-"))
-        (nef (gensym "NEF-")))
-    `(let ((,stored ,obj)
+        (fe (gensym "FE-"))
+        (ife (gensym "IFE-")))
+    `(let* ((,stored ,obj)
             (,fe (or foreign-encoding *default-foreign-encoding*))
             (,ife (when ,fe
                     (implementation-foreign-encoding ,fe))))
@@ -240,7 +240,7 @@ that LW/CMU automatically converts strings from c-calls."
 
   #+(or cmu scl sbcl digitool openmcl)
   `(%convert-to-foreign-string ,obj (implementation-foreign-encoding
-                                     (or ,foreign-encoding *default-foreign-encoding)))
+                                     (or ,foreign-encoding *default-foreign-encoding*)))
 )
 
 
@@ -332,8 +332,7 @@ that LW/CMU automatically converts strings from c-calls."
   (declare (ignore null-terminated-p))
   #+(or openmcl digitool)
   (let ((stored-obj (gensym "STR-"))
-        (fe (gensym "FE-"))
-        (ife (gensym "IFE-")))
+        (fe (gensym "FE-")))
     `(let ((,stored-obj ,obj))
        (if (ccl:%null-ptr-p ,stored-obj)
            nil
index 2008a30aebcbdbdf5af6b5e98eacfa61af79c9bd..e911e2f5f2ca682afc94d07927a54e5fb792e743 100644 (file)
@@ -4,3 +4,4 @@ c-test-fns.dylib
 z.dylib
 .bin
 uffi-c-test.so
+uffi-c-test.dylib
index 017fbedee1ac3d081dcfbba83f2d5f61736df384..2053273ed39492e63e2a398ce0cd1644671ee6e3 100644 (file)
@@ -26,6 +26,8 @@
           "z"
           (list (pathname-directory *load-pathname*)
                 "/usr/local/lib/" #+(or 64bit x86-64) "/usr/lib64/"
+                "/usr/lib32/"
+                "/opt/local/lib/"
                 "/usr/lib/" "/zlib/"))
          :module "zlib"
          :supporting-libraries '("c"))
index 3a19d75e29e155a978e38068076372ffe93483d4..fda7c10d86b62c8e76bdbebe9cf9692c78545789 100644 (file)
@@ -61,7 +61,7 @@
                      (fvar-struct-double))
   t)
 
-(deftest fvarst.6
+(deftest :fvarst.6
     (let ((orig *fvar-addend*))
       (incf *fvar-addend* 3)
       (prog1
@@ -69,7 +69,7 @@
         (setf *fvar-addend* orig)))
   6)
 
-(deftest fvarst.7
+(deftest :fvarst.7
     (let ((orig *fvar-addend*))
       (incf *fvar-addend* 3)
       (prog1
@@ -77,7 +77,7 @@
         (setf *fvar-addend* orig)))
   48)
 
-(deftest fvarst.8
+(deftest :fvarst.8
     (let ((orig (uffi:get-slot-value *fvar-struct* 'fvar-struct 'i)))
       (decf (uffi:get-slot-value *fvar-struct* 'fvar-struct 'i) 10)
       (prog1
diff --git a/tests/i18n.lisp b/tests/i18n.lisp
new file mode 100644 (file)
index 0000000..911e41f
--- /dev/null
@@ -0,0 +1,79 @@
+;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; Name:          i18n.lisp
+;;;; Purpose:       UFFI test file of i18n functions
+;;;; Author:        Kevin M. Rosenberg
+;;;; Date Started:  Feb 2010
+;;;;
+;;;; This file, part of UFFI, is Copyright (c) 2010 by Kevin M. Rosenberg
+;;;;
+;;;; *************************************************************************
+
+(in-package #:uffi-tests)
+
+(deftest :i18n/sto/1
+    (uffi:string-to-octets "")
+  #())
+
+(deftest :i18n/sto/2
+    (uffi:string-to-octets "A")
+  #(65))
+
+(deftest :i18n/sto/3
+    (uffi:string-to-octets "abc")
+  #(97 98 99))
+
+;; Below is UTF-8 encoded, 27 octets / 20 lisp characters
+(deftest :i18n/sto/4
+    (uffi:string-to-octets "Iñtërnâtiônàlizætiøn" :encoding :utf-8)
+  #(73 195 177 116 195 171 114 110 195 162 116 105 195 180 110 195 160 108 105 122 195 166 116 105 195 184 110))
+
+(deftest :i18n/sto/5
+    (length (uffi:string-to-octets "Iñtërnâtiônàlizætiøn" :encoding :utf-8))
+  27)
+
+(deftest :i18n/feoc/1
+    (uffi:foreign-encoded-octet-count "")
+  0)
+
+(deftest :i18n/feoc/2
+    (uffi:foreign-encoded-octet-count "A")
+  1)
+
+(deftest :i18n/feoc/3
+    (uffi:foreign-encoded-octet-count "abc")
+  3)
+
+(deftest :i18n/feoc/4
+    (uffi:foreign-encoded-octet-count "Iñtërnâtiônàlizætiøn"
+                                      :encoding :utf-8)
+  27)
+
+
+(deftest :i18n/ots/1
+    (let ((octets '()))
+      (uffi:octets-to-string (make-array (list (length octets)) :element-type '(unsigned-byte 8)
+                                         :initial-contents octets)))
+  "")
+
+(deftest :i18n/ots/2
+    (let ((octets '(65)))
+      (uffi:octets-to-string (make-array (list (length octets)) :element-type '(unsigned-byte 8)
+                                         :initial-contents octets)))
+  "A")
+
+(deftest :i18n/ots/3
+    (let ((octets '(97 98 99)))
+      (uffi:octets-to-string (make-array (list (length octets)) :element-type '(unsigned-byte 8)
+                                         :initial-contents octets)))
+  "abc")
+
+(deftest :i18n/ots/4
+    (let ((octets '(73 195 177 116 195 171 114 110 195 162 116 105 195 180
+                    110 195 160 108 105 122 195 166 116 105 195 184 110)))
+      (uffi:octets-to-string (make-array (list (length octets)) :element-type '(unsigned-byte 8)
+                                         :initial-contents octets)
+                             :encoding :utf-8))
+  "Iñtërnâtiônàlizætiøn")
index 1c7104afed656025de423c809ddc5f866e4201f0..f1f6b781e854af90fbc1178cbfe75d7d425164c8 100644 (file)
 (deftest :union.3 (plusp (uffi:get-slot-value *u* 'tunion1 'uint)) t)
 
 
+#-openmcl
 (uffi:def-union foo-u
     (bar :pointer-self))
 
+#-openmcl
 (uffi:def-foreign-type foo-u-ptr (* foo-u))
 
 ;; tests that compilation worked
+#-openmcl
 (deftest :unions.4
   (with-foreign-object (p 'foo-u)
     t)
   t)
 
+#-openmcl
 (deftest :unions.5
     (progn
       (uffi:def-foreign-type foo-union (:union foo-u))
index 06577e2f0175a5f66a755a6a476d7f5c4ee653ba..ded16f72e102337e503cad106ad75956bf104f85 100644 (file)
@@ -86,6 +86,7 @@
               (:file "compress" :depends-on ("foreign-loader"))
               (:file "casts" :depends-on ("foreign-loader"))
               (:file "foreign-var" :depends-on ("foreign-loader"))
+              (:file "i18n" :depends-on ("package"))
               ))))
 
 (defmethod perform ((o test-op) (c (eql (find-system :uffi-tests))))