1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: non-ASCII character support
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2010
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
12 ;;;; *************************************************************************
16 #-(or (and lispworks unicode) (and sbcl sb-unicode)
17 (and allegro ics) (and clisp i18n)
18 (and openmcl openmcl-unicode-strings))
19 (pushnew 'no-i18n cl:*features*)
21 (defvar *default-foreign-encoding*
23 "Normalized name of default external character format to use
24 for foreign string conversions. nil means use implementation default
27 (defvar *foreign-encoding-mapping*
28 #+(and lispworks unicode)
29 '((:ascii . :ascii) (:latin-1 . :latin-1) (:ucs-2 . :unicode)
30 (:utf-8 . :utf-8) (:jis . :jis) (:sjis . :sjis) (:gbk . :gbk))
31 #+(and sbcl sb-unicode)
32 '((:ascii . :ascii) (:latin-1 . :latin-1) (:utf-8 . :utf-8)
33 (:ucs-2 . :ucs-2) (:sjis . :sjis) (:gbk . :gbk))
35 '((:ascii . :ascii) (:latin-1 . :latin1) (:utf-8 . :utf-8)
36 (:sjis . :shift-jis) (:euc-jp . :euc) (:gbk . :gb2313)
39 '((:ascii . charset:ascii) (:ucs-2 . charset:ucs-2)
40 (:utf-8 . charset:utf-8) (:latin-1 . charset:iso-8859-1)
41 (:jis . charset:jis_x0201) (:jis . charset:shift-jis)
42 (:gbk . charset:gbk) (:euc-jp . charset:euc-jp))
43 #+(and openmcl openmcl-unicode-strings)
44 '((:ascii . :ascii) (:latin-1 . :iso-8859-1) (:utf-8 . :utf-8)
46 #+nil (:euc-jp . :euc-jp)
48 #-(or (and lispworks unicode) (and sbcl sb-unicode)
49 (and allegro ics) (and clisp unicode)
50 (and openmcl openmcl-unicode-strings))
52 "Mapping between normalized external format name and implementation name.")
54 (defvar *foreign-encodings*
55 (mapcar 'car *foreign-encoding-mapping*)
56 "List of normalized names of external formats support by underlying implementation.")
58 (defun lookup-foreign-encoding (normalized)
59 (cdr (assoc normalized *foreign-encoding-mapping* :test 'eql)))
61 (defmacro string-to-octets (str &key encoding null-terminate)
62 (declare (ignorable encoding))
63 #-(or allegro lispworks openmcl sbcl)
64 (map-into (make-array (length str) :element-type '(unsigned-byte 8))
68 (let ((fe (gensym "FE-"))
72 `(let* ((,fe (or ,encoding *default-foreign-encoding*))
73 (,ife (when ,fe (lookup-foreign-encoding ,fe)))
75 (,nt ,null-terminate))
78 (excl:string-to-octets ,s :external-format ,ife :null-terminate ,nt)
79 (excl:string-to-octets ,s :null-terminate ,nt)))))
81 #+(or lispworks openmcl)
82 ;; simply reading each char-code from the LENGTH of string handles multibyte characters
83 ;; just fine in testing LW 6.0 and CCL 1.4
84 (let ((len (gensym "LEN-"))
85 (out (gensym "OUT-")))
86 `(let ((,len (length ,str)))
89 (let ((,out (map-into (make-array (1+ ,len) :element-type '(unsigned-byte 8))
91 (setf (char ,out ,len) 0)
93 (map-into (make-array len :element-type '(unsigned-byte 8))
97 (let ((fe (gensym "FE-"))
101 `(let* ((,fe (or ,encoding *default-foreign-encoding*))
102 (,ife (when ,fe (lookup-foreign-encoding ,fe)))
104 (,nt ,null-terminate))
106 (sb-ext:string-to-octets ,s :external-format ,ife :null-terminate ,nt)
107 (sb-ext:string-to-octets ,s :null-terminate ,nt))))
111 (defmacro octets-to-string (octets &key encoding)
112 "Converts a vector of octets to a Lisp string."
113 (declare (ignorable encoding))
114 #-(or allegro lispworks openmcl sbcl)
115 (let ((out (gensym "OUT-"))
116 (code (gensym "CODE-")))
117 `(with-output-to-string (,out)
118 (loop for ,code across ,octets
119 do (write-char (code-char ,code) ,out))))
122 (let ((fe (gensym "FE-"))
123 (ife (gensym "IFE-"))
124 (oct (gensym "OCTETS-")))
125 `(let* ((,fe (or ,encoding *default-foreign-encoding*))
126 (,ife (when ,fe (lookup-foreign-encoding ,fe)))
130 (excl:octets-to-string ,oct :external-format ,ife)
131 (excl:octets-to-string ,oct)))))
133 #+(or lispworks openmcl)
134 ;; With LW 6.0 and CCL 1.4, writing multibyte character just one octet at a time tests fine
135 (let ((out (gensym "OUT-"))
136 (code (gensym "CODE-")))
137 `(with-output-to-string (,out)
138 (loop for ,code across ,octets
139 do (write-char (code-char ,code) ,out))))
142 (let ((fe (gensym "FE-"))
143 (ife (gensym "IFE-"))
144 (oct (gensym "OCTETS-")))
145 `(let* ((,fe (or ,encoding *default-foreign-encoding*))
146 (,ife (when ,fe (lookup-foreign-encoding ,fe)))
149 (sb-ext:octets-to-string ,oct :external-format ,ife)
150 (sb-ext:octets-to-string ,oct))))
154 (defun foreign-encoded-octet-count (str &key encoding)
155 "Returns the octets required to represent the string when passed to a ~
157 (declare (ignorable encoding))
158 ;; AllegroCL 8-bit, CCL, and Lispworks give correct value without converting
159 ;; to external-format. AllegroCL 16-bit, SBCL, and CLISP requires conversion
160 ;; with external-format
162 #+(or (and allegro ics) (and sbcl sb-unicode) (and clisp i18n))
163 (length (string-to-octets str :encoding
164 (or encoding *default-foreign-encoding*)))
166 #-(or (and allegro ics) (and sbcl sb-unicode) (and clisp i18n))