[bknr-cvs] r2039 - branches/xml-class-rework/thirdparty/iconv

bknr at bknr.net bknr at bknr.net
Tue Oct 24 09:56:56 UTC 2006


Author: hhubner
Date: 2006-10-24 05:56:56 -0400 (Tue, 24 Oct 2006)
New Revision: 2039

Modified:
   branches/xml-class-rework/thirdparty/iconv/iconv.lisp
Log:
Do not try to convert empty strings


Modified: branches/xml-class-rework/thirdparty/iconv/iconv.lisp
===================================================================
--- branches/xml-class-rework/thirdparty/iconv/iconv.lisp	2006-10-23 11:45:32 UTC (rev 2038)
+++ branches/xml-class-rework/thirdparty/iconv/iconv.lisp	2006-10-24 09:56:56 UTC (rev 2039)
@@ -58,41 +58,43 @@
 
 (defun iconv (from-code to-code from-string
 	      &optional error-p (error-value #.(char-code #\?)))
-  (with-iconv-cd (cd from-code to-code)
-    (let* ((from-len (length from-string))
-	   (to-len (* from-len 2))
-	   (inbuffer (cffi-uffi-compat:convert-to-foreign-string from-string))
-	   (outbuffer (cffi-uffi-compat:allocate-foreign-string to-len :unsigned t))
-	   (in-ptr (cffi-uffi-compat:allocate-foreign-object 'uchar-ptr))
-	   (out-ptr (cffi-uffi-compat:allocate-foreign-object 'uchar-ptr))
-	   (inbytesleft (cffi-uffi-compat:allocate-foreign-object :unsigned-int))
-	   (outbytesleft (cffi-uffi-compat:allocate-foreign-object :unsigned-int)))
-      (unwind-protect
-	   (progn
-	     (setf (cffi-uffi-compat:deref-pointer in-ptr 'uchar-ptr) inbuffer
-		   (cffi-uffi-compat:deref-pointer out-ptr 'uchar-ptr) outbuffer
-		   (cffi-uffi-compat:deref-pointer inbytesleft :unsigned-int) from-len
-		   (cffi-uffi-compat:deref-pointer outbytesleft :unsigned-int) to-len)
-	     (labels
-		 ((current ()
-		    (- from-len (cffi-uffi-compat:deref-pointer inbytesleft :unsigned-int)))
-		  (self ()
-		    (when (= (%iconv cd
-				     in-ptr inbytesleft
-				     out-ptr outbytesleft)
-			     #xffffffff)
-		      (if (find (get-errno) (list EILSEQ EINVAL) :test #'eql)
-			  (if error-p
-			      (error "invalid multibyte character (0x~X)." (cffi:mem-aref inbuffer :unsigned-char (current)))
-			      (progn
-				(setf (cffi:mem-aref inbuffer :unsigned-char (current)) error-value)
-				(self)))
-			  (error "unexpected iconv error ~A" (get-errno))))))
-	       (self))
-	     (cffi-uffi-compat:convert-from-foreign-string outbuffer :length (- to-len (cffi-uffi-compat:deref-pointer outbytesleft :unsigned-int))))
-	(cffi-uffi-compat:free-foreign-object outbytesleft)
-	(cffi-uffi-compat:free-foreign-object inbytesleft)
-	(cffi-uffi-compat:free-foreign-object out-ptr)
-	(cffi-uffi-compat:free-foreign-object in-ptr)
-	(cffi-uffi-compat:free-foreign-object outbuffer)
-	(cffi-uffi-compat:free-foreign-object inbuffer)))))
+  (if (equal from-string "")
+      ""
+      (with-iconv-cd (cd from-code to-code)
+	(let* ((from-len (length from-string))
+	       (to-len (* from-len 2))
+	       (inbuffer (cffi-uffi-compat:convert-to-foreign-string from-string))
+	       (outbuffer (cffi-uffi-compat:allocate-foreign-string to-len :unsigned t))
+	       (in-ptr (cffi-uffi-compat:allocate-foreign-object 'uchar-ptr))
+	       (out-ptr (cffi-uffi-compat:allocate-foreign-object 'uchar-ptr))
+	       (inbytesleft (cffi-uffi-compat:allocate-foreign-object :unsigned-int))
+	       (outbytesleft (cffi-uffi-compat:allocate-foreign-object :unsigned-int)))
+	  (unwind-protect
+	       (progn
+		 (setf (cffi-uffi-compat:deref-pointer in-ptr 'uchar-ptr) inbuffer
+		       (cffi-uffi-compat:deref-pointer out-ptr 'uchar-ptr) outbuffer
+		       (cffi-uffi-compat:deref-pointer inbytesleft :unsigned-int) from-len
+		       (cffi-uffi-compat:deref-pointer outbytesleft :unsigned-int) to-len)
+		 (labels
+		     ((current ()
+			(- from-len (cffi-uffi-compat:deref-pointer inbytesleft :unsigned-int)))
+		      (self ()
+			(when (= (%iconv cd
+					 in-ptr inbytesleft
+					 out-ptr outbytesleft)
+				 #xffffffff)
+			  (if (find (get-errno) (list EILSEQ EINVAL) :test #'eql)
+			      (if error-p
+				  (error "invalid multibyte character (0x~X)." (cffi:mem-aref inbuffer :unsigned-char (current)))
+				  (progn
+				    (setf (cffi:mem-aref inbuffer :unsigned-char (current)) error-value)
+				    (self)))
+			      (error "unexpected iconv error ~A" (get-errno))))))
+		   (self))
+		 (cffi-uffi-compat:convert-from-foreign-string outbuffer :length (- to-len (cffi-uffi-compat:deref-pointer outbytesleft :unsigned-int))))
+	    (cffi-uffi-compat:free-foreign-object outbytesleft)
+	    (cffi-uffi-compat:free-foreign-object inbytesleft)
+	    (cffi-uffi-compat:free-foreign-object out-ptr)
+	    (cffi-uffi-compat:free-foreign-object in-ptr)
+	    (cffi-uffi-compat:free-foreign-object outbuffer)
+	    (cffi-uffi-compat:free-foreign-object inbuffer))))))




More information about the Bknr-cvs mailing list