/[cmucl]/src/code/extfmts.lisp
ViewVC logotype

Contents of /src/code/extfmts.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2.4.3.2.18 - (hide annotations)
Thu Apr 30 18:52:43 2009 UTC (4 years, 11 months ago) by rtoy
Branch: unicode-utf16-extfmt-branch
CVS Tags: unicode-snapshot-2009-05
Changes since 1.2.4.3.2.17: +54 -20 lines
Update from Paul to make the external formats that use invert-tables
uses tries instead of a hash table.

code/extfmts.lisp:
o Change (unsigned-byte 31) to (unsigned-byte 21).  (Should probably
  add a codepoint deftype for this.)
o Use a trie instead of a hash-table for the invert-table stuff
o Fix a typo in a comment.

pcl/simple-streams/external-formats/iso8859-2.lisp:
pcl/simple-streams/external-formats/macroman.lisp:
o Use a trie
1 rtoy 1.1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: STREAM -*-
2     ;;;
3     ;;; **********************************************************************
4     ;;; This code was written by Paul Foley and has been placed in the public
5     ;;; domain.
6     ;;;
7     (ext:file-comment
8 rtoy 1.2.4.3.2.18 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/extfmts.lisp,v 1.2.4.3.2.18 2009/04/30 18:52:43 rtoy Exp $")
9 rtoy 1.1 ;;;
10     ;;; **********************************************************************
11     ;;;
12     ;;; Implementation of external-formats
13    
14     (in-package "STREAM")
15    
16     (export '(string-to-octets octets-to-string *default-external-format*
17 rtoy 1.2.4.1 string-encode string-decode))
18 rtoy 1.1
19     (defvar *default-external-format* :iso8859-1)
20    
21 rtoy 1.2.4.1 (defvar *external-formats* (make-hash-table :test 'equal))
22 rtoy 1.1 (defvar *external-format-aliases* (make-hash-table))
23    
24 rtoy 1.2.4.3.2.1 (defconstant +ef-cin+ 2)
25     (defconstant +ef-cout+ 3)
26     (defconstant +ef-sin+ 4)
27     (defconstant +ef-sout+ 5)
28     (defconstant +ef-os+ 6)
29     (defconstant +ef-so+ 7)
30     (defconstant +ef-en+ 8)
31     (defconstant +ef-de+ 9)
32     (defconstant +ef-max+ 10)
33 rtoy 1.2.4.1
34 rtoy 1.1 (define-condition external-format-not-implemented (error)
35     ()
36     (:report
37     (lambda (condition stream)
38     (declare (ignore condition))
39     (format stream "Attempting unimplemented external-format I/O."))))
40    
41     (defun %efni (a b c d)
42     (declare (ignore a b c d))
43     (error 'external-format-not-implemented))
44    
45 rtoy 1.2.4.3 (defstruct efx
46     (octets-to-code #'%efni :type function :read-only t)
47     (code-to-octets #'%efni :type function :read-only t)
48 rtoy 1.2.4.3.2.1 (cache nil :type (or null simple-vector))
49     (min 1 :type kernel:index :read-only t)
50     (max 1 :type kernel:index :read-only t))
51 rtoy 1.2.4.3
52 rtoy 1.1 (defstruct (external-format
53     (:conc-name ef-)
54     (:print-function %print-external-format)
55 rtoy 1.2.4.3 (:constructor make-external-format (name efx composingp
56     &optional slots slotd)))
57 rtoy 1.2.4.1 (name (ext:required-argument) :type (or keyword cons) :read-only t)
58 rtoy 1.2.4.3 (efx (ext:required-argument) :type efx :read-only t)
59 rtoy 1.2.4.1 (composingp (ext:required-argument) :type boolean :read-only t)
60 rtoy 1.1 (slots #() :type simple-vector :read-only t)
61 rtoy 1.2.4.3 (slotd nil :type list :read-only t))
62 rtoy 1.1
63     (defun %print-external-format (ef stream depth)
64     (declare (ignore depth))
65     (print-unreadable-object (ef stream :type t :identity t)
66     (princ (ef-name ef) stream)))
67    
68 rtoy 1.2.4.3.2.1 (defun %intern-ef (ef)
69 rtoy 1.2.4.1 (setf (gethash (ef-name ef) *external-formats*) ef))
70    
71 rtoy 1.2.4.3.2.1 (declaim (inline ef-octets-to-code ef-code-to-octets ef-cache
72     ef-min-octets ef-max-octets))
73 rtoy 1.2.4.3
74     (defun ef-octets-to-code (ef)
75     (efx-octets-to-code (ef-efx ef)))
76    
77     (defun ef-code-to-octets (ef)
78     (efx-code-to-octets (ef-efx ef)))
79    
80     (defun ef-cache (ef)
81     (efx-cache (ef-efx ef)))
82    
83 rtoy 1.2.4.3.2.1 (defun ef-min-octets (ef)
84     (efx-min (ef-efx ef)))
85    
86     (defun ef-max-octets (ef)
87     (efx-max (ef-efx ef)))
88    
89     (eval-when (:compile-toplevel :load-toplevel :execute)
90     (defun %merge-slots (old new)
91     (let* ((pos (length old))
92     (tmp (mapcar (lambda (x)
93     (let* ((name (if (consp x) (first x) x))
94     (init (if (consp x) (second x) nil))
95     (list (if (consp x) (nthcdr 2 x) nil))
96     (prev (assoc name old))
97     (posn (if prev (second prev) (1- (incf pos)))))
98     (list name posn init (getf list :type t))))
99     new)))
100     (delete-duplicates (stable-sort (append old tmp) #'< :key #'second)
101     :key #'second))))
102    
103     ;;; DEFINE-EXTERNAL-FORMAT -- Public
104     ;;;
105     ;;; name (&key min max size) (&rest slots) octets-to-code code-to-octets
106     ;;; Define a new external format. Min/Max/Size are the minimum and
107     ;;; maximum number of octets that make up a character (:size N is just
108     ;;; shorthand for :min N :max N). Slots is a list of slot descriptions
109     ;;; similar to defstruct.
110     ;;;
111     ;;; name (base) (&rest slots)
112     ;;; Define an external format based on a previously-defined external
113     ;;; format, Base. The slot names used in Slots must match those in Base.
114     ;;;
115 rtoy 1.2.4.3.2.10 ;;; octets-to-code (state input unput &rest vars)
116     ;;; Defines a form to be used by the external format to convert
117     ;;; octets to a code point. State is a form that can be used by the
118     ;;; body to access the state variable of the stream. Input is a
119     ;;; form that can be used to read one more octets from the input
120     ;;; strema. Similarly, Unput is a form to put back one octet to the
121     ;;; input stream. Vars is a list of vars that need to be defined
122     ;;; for any symbols used within the form.
123     ;;;
124     ;;; This should return two values: the code and the number of octets
125     ;;; read to form the code.
126     ;;;
127     ;;; code-to-octets (code state output &rest vars)
128     ;;; Defines a form to be used by the external format to convert a
129     ;;; code point to octets for output. Code is the code point to be
130     ;;; converted. State is a form to access the current value of the
131     ;;; stream's state variable. Output is a form that writes one octet
132     ;;; to the output stream.
133     ;;;
134 rtoy 1.2.4.3.2.12 ;;; Note: external-formats work on code-points, not
135 rtoy 1.2.4.3.2.10 ;;; characters, so that the entire 31 bit ISO-10646 range can be
136     ;;; used internally regardless of the size of a character recognized
137     ;;; by Lisp and external formats can be useful to people who want to
138     ;;; process characters outside the Lisp range (see
139     ;;; CODEPOINT-TO-OCTETS, OCTETS-TO-CODEPOINT)
140     ;;;
141 rtoy 1.2.4.3.2.1 (defmacro define-external-format (name (&rest args) (&rest slots)
142     &optional octets-to-code code-to-octets)
143     (when (and (oddp (length args)) (not (= (length args) 1)))
144     (warn "Nonsensical argument (~S) to DEFINE-EXTERNAL-FORMAT." args))
145 rtoy 1.2.4.3.2.8 (let* ((tmp (gensym))
146 rtoy 1.2.4.3.2.1 (min (if (evenp (length args))
147     (or (getf args :min) (getf args :size) 1)
148     1))
149     (max (if (evenp (length args))
150     (or (getf args :max) (getf args :size) 6)
151     6))
152     (base (if (= (length args) 1)
153     (find-external-format (first args))
154     nil))
155     (bslotd (if base (ef-slotd base) nil))
156     (slotd (%merge-slots bslotd slots))
157     (slotb (loop for slot in slotd
158     collect `(,(first slot)
159 rtoy 1.2.4.3.2.8 `(the ,',(fourth slot)
160     ;; IDENTITY is here to protect against SETF
161     (identity (svref %slots% ,',(second slot))))))))
162 rtoy 1.2.4.1 `(macrolet ((octets-to-code ((state input unput &rest vars) body)
163 rtoy 1.2.4.3.2.8 `(lambda (,state ,input ,unput)
164     (declare (ignorable ,state ,input ,unput)
165 rtoy 1.2.4.1 (optimize (ext:inhibit-warnings 3)))
166 rtoy 1.2.4.3.2.1 (let (,@',slotb
167     (,input `(the (or (unsigned-byte 8) null) ,,input))
168 rtoy 1.2.4.1 ,@(loop for var in vars collect `(,var (gensym))))
169     ,body)))
170     (code-to-octets ((code state output &rest vars) body)
171 rtoy 1.2.4.3.2.8 `(lambda (,',tmp ,state ,output)
172     (declare (ignorable ,state ,output)
173 rtoy 1.2.4.1 (optimize (ext:inhibit-warnings 3)))
174 rtoy 1.2.4.3.2.1 (let (,@',slotb
175     (,code ',code)
176 rtoy 1.2.4.1 ,@(loop for var in vars collect `(,var (gensym))))
177 rtoy 1.2.4.3.2.18 `(let ((,',code (the (unsigned-byte 21) ,,',tmp)))
178 rtoy 1.2.4.1 (declare (ignorable ,',code))
179     ,,body)))))
180 rtoy 1.2.4.3.2.1 (%intern-ef (make-external-format ,name
181     ,(if base
182     `(ef-efx (find-external-format ,(ef-name base)))
183     `(make-efx :octets-to-code ,octets-to-code
184     :code-to-octets ,code-to-octets
185     :cache (make-array +ef-max+
186     :initial-element nil)
187     :min ,(min min max) :max ,(max min max)))
188     nil
189 rtoy 1.2.4.3.2.18 (let* ,(loop for x in slotd
190     collect (list (first x) (third x)))
191     (vector ,@(mapcar #'first slotd)))
192 rtoy 1.2.4.3.2.1 ',slotd)))))
193 rtoy 1.2.4.1
194 rtoy 1.2.4.3.2.1 ;;; DEFINE-COMPOSING-EXTERNAL-FORMAT -- Public
195     ;;;
196     ;;; A composing-external-format differs from an (ordinary) external-format
197     ;;; in that it translates characters (really codepoints, of course) into
198     ;;; other characters, rather than translating between characters and binary
199     ;;; octets. They have to be composed with a non-composing external-format
200     ;;; to be of any use.
201     ;;;
202     (defmacro define-composing-external-format (name (&key min max size)
203     input output)
204 rtoy 1.2.4.3.2.8 (let ((tmp (gensym))
205 rtoy 1.2.4.3.2.1 (min (or min size 1))
206     (max (or max size 1)))
207 rtoy 1.2.4.1 `(macrolet ((input ((state input unput &rest vars) body)
208 rtoy 1.2.4.3.2.8 `(lambda (,state ,input ,unput)
209     (declare (ignorable ,state ,input ,unput)
210 rtoy 1.2.4.1 (optimize (ext:inhibit-warnings 3)))
211 rtoy 1.2.4.3.2.18 (let ((,input `(the (values (or (unsigned-byte 21) null)
212 rtoy 1.2.4.3.2.1 kernel:index)
213 rtoy 1.2.4.1 ,,input))
214     ,@(loop for var in vars collect `(,var (gensym))))
215     ,body)))
216     (output ((code state output &rest vars) body)
217 rtoy 1.2.4.3.2.8 `(lambda (,',tmp ,state ,output)
218     (declare (ignorable ,state ,output)
219 rtoy 1.2.4.1 (optimize (ext:inhibit-warnings 3)))
220     (let ((,code ',code)
221     ,@(loop for var in vars collect `(,var (gensym))))
222 rtoy 1.2.4.3.2.18 `(let ((,',code (the (unsigned-byte 21) ,,',tmp)))
223 rtoy 1.2.4.1 (declare (ignorable ,',code))
224     ,,body)))))
225 rtoy 1.2.4.3.2.1 (%intern-ef (make-external-format ,name
226     (make-efx :octets-to-code ,input
227     :code-to-octets ,output
228     :min ,(min min max) :max ,(max min max))
229     t
230     #() '())))))
231 rtoy 1.1
232     (defun load-external-format-aliases ()
233 rtoy 1.2.4.3.2.1 (let ((*package* (find-package "KEYWORD"))
234     (unix::*filename-encoding* :iso8859-1))
235 rtoy 1.2.4.2 (with-open-file (stm "ext-formats:aliases" :if-does-not-exist nil)
236 rtoy 1.1 (when stm
237 rtoy 1.2.4.3.2.1 (do ((alias (read stm nil stm) (read stm nil stm))
238     (value (read stm nil stm) (read stm nil stm)))
239     ((or (eq alias stm) (eq value stm))
240     (unless (eq alias stm)
241     (warn "External-format aliases file ends early.")))
242     (if (and (keywordp alias) (keywordp value))
243     (setf (gethash alias *external-format-aliases*) value)
244     (warn "Bad entry in external-format aliases file: ~S => ~S."
245     alias value)))))))
246 rtoy 1.1
247 rtoy 1.2.4.1 (defun %find-external-format (name)
248 rtoy 1.2.4.3.2.5 ;; avoid loading files, etc., early in the boot sequence
249     (when (or (eq name :iso8859-1)
250     (and (eq name :default) (eq *default-external-format* :iso8859-1)))
251     (return-from %find-external-format
252     (gethash :iso8859-1 *external-formats*)))
253    
254     (when (zerop (hash-table-count *external-format-aliases*))
255     (setf (gethash :latin1 *external-format-aliases*) :iso8859-1)
256     (setf (gethash :latin-1 *external-format-aliases*) :iso8859-1)
257     (setf (gethash :iso-8859-1 *external-format-aliases*) :iso8859-1)
258     (load-external-format-aliases))
259 rtoy 1.1
260     (do ((tmp (gethash name *external-format-aliases*)
261     (gethash tmp *external-format-aliases*))
262     (cnt 0 (1+ cnt)))
263     ((or (null tmp) (= cnt 50))
264     (unless (null tmp)
265     (error "External-format aliasing depth exceeded.")))
266     (setq name tmp))
267    
268     (or (gethash name *external-formats*)
269 rtoy 1.2 (and (let ((*package* (find-package "STREAM"))
270 rtoy 1.2.4.3.2.1 (lisp::*enable-package-locked-errors* nil)
271     (*default-external-format* :iso8859-1)
272 rtoy 1.2.4.3.2.9 (unix::*filename-encoding* :iso8859-1)
273     (s (open (format nil "ext-formats:~(~A~).lisp" name) :if-does-not-exist nil)))
274     (when s
275     (null (nth-value 1 (ext:compile-from-stream s)))))
276 rtoy 1.2.4.1 (gethash name *external-formats*))))
277    
278     (defun %composed-ef-name (a b)
279     (if (consp a) (append a (list b)) (list a b)))
280    
281 rtoy 1.2.4.3.2.1 (defun %compose-external-formats (a b)
282 rtoy 1.2.4.1 (when (ef-composingp a)
283     (error "~S is a Composing-External-Format." (ef-name a)))
284     (unless (ef-composingp b)
285     (error "~S is not a Composing-External-Format." (ef-name b)))
286 rtoy 1.2.4.3 (make-external-format
287     (%composed-ef-name (ef-name a) (ef-name b))
288     (make-efx
289 rtoy 1.2.4.3.2.8 :octets-to-code (lambda (state input unput)
290     (funcall (ef-octets-to-code b) state
291     (funcall (ef-octets-to-code a)
292     state input unput)
293 rtoy 1.2.4.3 unput))
294 rtoy 1.2.4.3.2.8 :code-to-octets (lambda (code state output)
295     (funcall (ef-code-to-octets b) code state
296 rtoy 1.2.4.3 `(lambda (x)
297     ,(funcall (ef-code-to-octets a)
298     'x state output))))
299 rtoy 1.2.4.3.2.1 :cache (make-array +ef-max+ :initial-element nil)
300     :min (* (ef-min-octets a) (ef-min-octets b))
301     :max (* (ef-max-octets a) (ef-max-octets b)))
302 rtoy 1.2.4.3 nil #() '()))
303 rtoy 1.2.4.1
304     (defun find-external-format (name &optional (error-p t))
305     (when (external-format-p name)
306     (return-from find-external-format name))
307    
308     (or (if (consp name) (every #'keywordp name) (keywordp name))
309     (error "~S is not a valid external format name." name))
310 rtoy 1.1
311 rtoy 1.2.4.1 (when (eq name :default)
312     (setq name *default-external-format*))
313    
314     (when (and (consp name) (not (cdr name)))
315     (setq name (car name)))
316    
317 rtoy 1.2.4.3.2.5 (flet ((not-found ()
318     (when (equal *default-external-format* name)
319     (setq *default-external-format* :iso8859-1))
320     (if error-p (error "External format ~S not found." name) nil)))
321     (if (consp name)
322     (let ((efs (mapcar #'%find-external-format name)))
323     (if (member nil efs)
324     (not-found)
325     (let ((name (reduce #'%composed-ef-name (mapcar #'ef-name efs))))
326     (or (gethash name *external-formats*)
327     (%intern-ef (reduce #'%compose-external-formats efs))))))
328     (or (%find-external-format name) (not-found)))))
329 rtoy 1.1
330 rtoy 1.2.4.3.2.1 (defun flush-external-formats ()
331     (maphash (lambda (name ef)
332     (declare (ignore name))
333     (fill (ef-cache ef) nil))
334     *external-formats*))
335    
336 rtoy 1.2.4.3.2.5 (defvar *.table-inverse.* (make-hash-table :test 'eq :size 7))
337    
338     (defun invert-table (table)
339     (declare (type (or (simple-array (unsigned-byte 31) *)
340     (simple-array (unsigned-byte 16) *))
341     table)
342     (optimize (speed 3) (space 0) (safety 0) (debug 0)
343     (ext:inhibit-warnings 3)))
344     (or (gethash table *.table-inverse.*)
345 rtoy 1.2.4.3.2.18 (let* ((mbits (if (= (array-total-size table) 128) 7 8))
346     (lbits (cond ((> (array-total-size table) 256) 3)
347     ((< (array-total-size table) 100) 6)
348     (t 5)))
349     (hvec (make-array (1+ (ash #x110000 (- 0 mbits lbits)))
350     :element-type '(unsigned-byte 16)
351     :initial-element #xFFFF))
352     (mvec (make-array 0 :element-type '(unsigned-byte 16)))
353     (lvec (make-array 0 :element-type '(unsigned-byte 16)))
354 rtoy 1.2.4.3.2.5 (width (array-dimension table 0))
355     (power (1- (array-rank table)))
356 rtoy 1.2.4.3.2.18 (base (if (= width 94) 1 0))
357     hx mx lx)
358     (assert (and (< power 2) (<= width 256)))
359 rtoy 1.2.4.3.2.5 (dotimes (i (array-total-size table))
360     (declare (type (integer 0 (#.array-dimension-limit)) i))
361     (let ((tmp i) (val (row-major-aref table i)) (z 0))
362     (declare (type (integer 0 (#.array-dimension-limit)) tmp)
363 rtoy 1.2.4.3.2.18 (type (unsigned-byte 16) z))
364     (unless (= val #xFFFE)
365     (when (plusp power)
366 rtoy 1.2.4.3.2.5 (multiple-value-bind (x y) (floor tmp width)
367     (setq tmp x)
368     (setq z (logior z (ash (the (integer 0 255) (+ y base))
369     (the (integer 0 24)
370 rtoy 1.2.4.3.2.18 (* 8 power)))))))
371     (setq hx (ash val (- 0 mbits lbits)))
372     (when (= (aref hvec hx) #xFFFF)
373     (setf (aref hvec hx) (length mvec))
374     (let ((tmp (make-array (+ (length mvec) (ash 1 mbits))
375     :element-type '(unsigned-byte 16)
376     :initial-element #xFFFF)))
377     (replace tmp mvec)
378     (setq mvec tmp)))
379     (setq mx (logand (ash val (- lbits)) (lognot (ash -1 mbits))))
380     (when (= (aref mvec (+ hx mx)) #xFFFF)
381     (setf (aref mvec (+ hx mx)) (length lvec))
382     (let ((tmp (make-array (+ (length lvec) (ash 1 lbits))
383     :element-type '(unsigned-byte 16)
384     :initial-element #xFFFF)))
385     (replace tmp lvec)
386     (setq lvec tmp)))
387     (setq lx (logand val (lognot (ash -1 lbits))))
388     (setf (aref lvec (+ (aref mvec (+ hx mx)) lx))
389     (logior z (+ tmp base))))))
390     (setf (gethash table *.table-inverse.*)
391     (lisp::make-ntrie16 :split (logior (ash (1- mbits) 4) (1- lbits))
392     :hvec hvec :mvec mvec :lvec lvec)))))
393    
394     (declaim (inline get-inverse))
395     (defun get-inverse (ntrie code)
396     (declare (type lisp::ntrie16 ntrie) (type (integer 0 #x10FFFF) code))
397     (let ((n (lisp::qref ntrie code)))
398     (and n (let ((m (aref (lisp::ntrie16-lvec ntrie) n)))
399     (if (= m #xFFFF) nil m)))))
400 rtoy 1.2.4.3.2.5
401    
402 rtoy 1.1 (define-condition void-external-format (error)
403     ()
404     (:report
405     (lambda (condition stream)
406     (declare (ignore condition))
407     (format stream "Attempting I/O through void external-format."))))
408    
409 rtoy 1.2.4.3.2.1 (define-external-format :void (:size 0) ()
410 rtoy 1.1 (octets-to-code (state input unput)
411 rtoy 1.2.4.1 `(error 'void-external-format))
412 rtoy 1.1 (code-to-octets (code state output)
413 rtoy 1.2.4.1 `(error 'void-external-format)))
414 rtoy 1.1
415 rtoy 1.2.4.3.2.1 (define-external-format :iso8859-1 (:size 1) ()
416 rtoy 1.1 (octets-to-code (state input unput)
417 rtoy 1.2.4.1 `(values ,input 1))
418 rtoy 1.1 (code-to-octets (code state output)
419 rtoy 1.2.4.1 `(,output (if (> ,code 255) #x3F ,code))))
420 rtoy 1.1
421 rtoy 1.2.4.3.2.1 ;;; OCTETS-TO-CODEPOINT, CODEPOINT-TO-OCTETS -- Semi-Public
422     ;;;
423     ;;; Normally you'd want to use OCTETS-TO-CHAR and CHAR-TO-OCTETS instead of
424     ;;; these, but that limits you to Lisp's idea of a character - either Latin-1
425     ;;; in 8 bit Lisp images, or the Unicode BMP in 16 bit images. If you want
426     ;;; to read or write texts containing characters not supported by your Lisp,
427     ;;; these macros can be used instead.
428 rtoy 1.1 (defmacro octets-to-codepoint (external-format state count input unput)
429 rtoy 1.2.4.3.2.8 (let ((tmp1 (gensym)) (tmp2 (gensym))
430     (ef (find-external-format external-format)))
431     `(multiple-value-bind (,tmp1 ,tmp2)
432     ,(funcall (ef-octets-to-code ef) state input unput)
433     (setf ,count (the kernel:index ,tmp2))
434 rtoy 1.2.4.3.2.18 (the (or (unsigned-byte 21) null) ,tmp1))))
435 rtoy 1.1
436     (defmacro codepoint-to-octets (external-format code state output)
437 rtoy 1.2.4.3.2.8 (let ((ef (find-external-format external-format)))
438     (funcall (ef-code-to-octets ef) code state output)))
439 rtoy 1.2.4.1
440    
441    
442     (defvar *ef-base* +ef-max+)
443     (defvar *ef-extensions* '())
444    
445     (defun ensure-cache (ef id reqd)
446     (let ((base (or (getf *ef-extensions* id)
447     (setf (getf *ef-extensions* id)
448     (prog1 *ef-base* (incf *ef-base* reqd))))))
449     (when (< (length (ef-cache ef)) (+ base reqd))
450 rtoy 1.2.4.3 (setf (efx-cache (ef-efx ef))
451 rtoy 1.2.4.1 (adjust-array (ef-cache ef) (+ base reqd) :initial-element nil)))
452     base))
453    
454 rtoy 1.2.4.3.2.1 ;;; DEF-EF-MACRO -- Public
455     ;;;
456     ;;;
457 rtoy 1.2.4.1 (defmacro def-ef-macro (name (ef id reqd idx) body)
458 rtoy 1.2.4.3.2.8 (let ((tmp1 (gensym))
459     (tmp2 (gensym))
460     (%name (intern (format nil "%~A" name) (symbol-package name))))
461     `(progn
462     (defun ,%name (,ef)
463     (let* ((,tmp1 (find-external-format ,ef))
464     (,tmp2 ,(if (eq id 'lisp::lisp)
465     idx
466     `(+ (ensure-cache ,tmp1 ',id ,reqd) ,idx))))
467     (funcall (or (aref (ef-cache ,tmp1) ,tmp2)
468     (setf (aref (ef-cache ,tmp1) ,tmp2)
469     (let ((*compile-print* nil)
470     ;; Set default format when we compile so we
471 rtoy 1.2.4.3.2.18 ;; can see compiler messages. If we don't,
472 rtoy 1.2.4.3.2.8 ;; we run into a problem that we might be
473     ;; changing the default format while we're
474     ;; compiling, and we don't know how to output
475     ;; the compiler messages.
476     (*default-external-format* :iso8859-1))
477     (compile nil `(lambda (%slots%)
478     (declare (ignorable %slots%))
479     ,,body)))))
480     (ef-slots ,tmp1))))
481     (declaim (inline ,name))
482     (defun ,name (,tmp1)
483     (let ((,tmp2 (load-time-value (cons nil nil))))
484     (when (eq ,tmp1 :default)
485     (setq ,tmp1 *default-external-format*))
486     (if (eq ,tmp1 (car ,tmp2))
487     (cdr ,tmp2)
488     (setf (car ,tmp2) ,tmp1
489     (cdr ,tmp2) (,%name ,tmp1))))))))
490 rtoy 1.1
491    
492    
493 rtoy 1.2.4.3.2.1 ;;; OCTETS-TO-CHAR, CHAR-TO-OCTETS -- Public
494     ;;;
495     ;;; Read and write one character through an external-format
496     ;;;
497 rtoy 1.1 (defmacro octets-to-char (external-format state count input unput)
498 rtoy 1.2.4.3.2.17 (let ((s (gensym "STATE-"))
499     (code (gensym "CODE-")))
500 rtoy 1.2.4.3.2.15 `(let ((,s ,state))
501     (when (null ,s)
502     ;; Need our own state variable to hold our state and the
503     ;; state for the external format.
504     (setq ,s (setf ,state (cons nil nil))))
505     (if (car ,s)
506     ;; Return the trailing surrgate. Must set count to 0 to
507     ;; tell the stream code we didn't consume any octets!
508     (prog1 (the character (car ,s))
509     (setf (car ,s) nil)
510     (setf ,count 0))
511 rtoy 1.2.4.3.2.17 (let ((,code (octets-to-codepoint ,external-format
512 rtoy 1.2.4.3.2.15 (cdr ,s) ,count ,input ,unput)))
513 rtoy 1.2.4.3.2.17 (declare (type (unsigned-byte 31) ,code))
514     (cond ((or (<= #xD800 ,code #xDFFF)
515     (> ,code #x10FFFF))
516 rtoy 1.2.4.3.2.15 #-(and unicode (not unicode-bootstrap)) #\?
517     #+(and unicode (not unicode-bootstrap)) #\U+FFFD)
518     #+unicode
519 rtoy 1.2.4.3.2.17 ((> ,code #xFFFF)
520 rtoy 1.2.4.3.2.15 (multiple-value-bind (hi lo)
521 rtoy 1.2.4.3.2.17 (lisp::surrogates ,code)
522 rtoy 1.2.4.3.2.15 (setf (car ,state) lo)
523     hi))
524 rtoy 1.2.4.3.2.17 (t (code-char ,code))))))))
525 rtoy 1.2.4.3.2.15
526     ;; This doesn't handle surrogate code units correctly. It just
527     ;; outputs the surrogate value to the external format. External
528     ;; formats almost never allow surrogate code points (except UTF-16).
529 rtoy 1.1 (defmacro char-to-octets (external-format char state output)
530     `(codepoint-to-octets ,external-format (char-code ,char) ,state ,output))
531    
532 rtoy 1.2.4.3.2.1
533 rtoy 1.2.4.1 (def-ef-macro ef-string-to-octets (extfmt lisp::lisp +ef-max+ +ef-so+)
534 rtoy 1.2.4.3.2.16 `(lambda (string start end buffer &aux (ptr 0) (state nil) (code 0) (c 0) widep)
535 rtoy 1.2.4.1 (declare #|(optimize (speed 3) (safety 0) (space 0) (debug 0))|#
536     (type simple-string string)
537 rtoy 1.2.4.3.2.1 (type kernel:index start end ptr)
538 rtoy 1.2.4.1 (type (simple-array (unsigned-byte 8) (*)) buffer)
539 rtoy 1.2.4.3.2.16 (type (integer 0 #x10ffff) code c)
540     (type (or null fixnum) widep)
541 rtoy 1.2.4.1 (ignorable state))
542 rtoy 1.2.4.3.2.16 (loop with i of-type kernel:index = start
543 rtoy 1.2.4.3.2.14 while (< i end)
544     do
545 rtoy 1.2.4.3.2.16 (multiple-value-bind (c widep)
546 rtoy 1.2.4.3.2.14 (lisp::codepoint string i end)
547 rtoy 1.2.4.3.2.16 (incf i (if widep 2 1))
548     (codepoint-to-octets ,extfmt c state
549 rtoy 1.2.4.3.2.14 (lambda (b)
550     (when (= ptr (length buffer))
551     (setq buffer (adjust-array buffer (* 2 ptr))))
552     (setf (aref buffer (1- (incf ptr))) b)))))))
553 rtoy 1.2.4.1
554 rtoy 1.2 (defun string-to-octets (string &key (start 0) end (external-format :default)
555     (buffer nil bufferp))
556 rtoy 1.1 (declare (type string string)
557 rtoy 1.2.4.3.2.1 (type kernel:index start)
558     (type (or kernel:index null) end)
559 rtoy 1.2.4.1 (type (or (simple-array (unsigned-byte 8) (*)) null) buffer))
560 rtoy 1.2.4.3.2.1 (let* ((buffer (or buffer (make-array (length string)
561 rtoy 1.2.4.1 :element-type '(unsigned-byte 8)))))
562 rtoy 1.2.4.3.2.1 (multiple-value-bind (buffer ptr)
563     (lisp::with-array-data ((string string) (start start) (end end))
564 rtoy 1.2.4.3.2.8 (funcall (ef-string-to-octets external-format)
565 rtoy 1.2.4.3.2.1 string start end buffer))
566     (values (if bufferp buffer (lisp::shrink-vector buffer ptr)) ptr))))
567 rtoy 1.2.4.1
568     (def-ef-macro ef-octets-to-string (extfmt lisp::lisp +ef-max+ +ef-os+)
569 rtoy 1.2.4.3.2.11 `(lambda (octets ptr end string &aux (pos -1) (count 0) (state nil) (code 0))
570 rtoy 1.2.4.1 (declare #|(optimize (speed 3) (safety 0) (space 0) (debug 0))|#
571     (type (simple-array (unsigned-byte 8) (*)) octets)
572 rtoy 1.2.4.3.2.1 (type kernel:index end count)
573 rtoy 1.2.4.1 (type (integer -1 (#.array-dimension-limit)) ptr pos)
574     (type simple-string string)
575 rtoy 1.2.4.3.2.11 (type (integer 0 #x10ffff) code)
576 rtoy 1.2.4.1 (ignorable state))
577     (loop until (>= ptr end)
578     do (when (= pos (length string))
579     (setq string (adjust-array string (* 2 pos))))
580 rtoy 1.2.4.3.2.11 (setf code
581     (octets-to-codepoint ,extfmt state count
582 rtoy 1.2.4.3.2.8 (aref octets (incf ptr)) ;;@@ EOF??
583     (lambda (n) (decf ptr n))))
584 rtoy 1.2.4.3.2.11 ;; Convert codepoint to UTF-16 surrogate pairs if needed
585 rtoy 1.2.4.3.2.14 (multiple-value-bind (high low)
586     (surrogates code)
587     (setf (aref string (incf pos)) high)
588     (when low
589     (setf (aref string (incf pos)) low)))
590 rtoy 1.2.4.3 finally (return (values string (1+ pos))))))
591 rtoy 1.1
592 rtoy 1.2 (defun octets-to-string (octets &key (start 0) end (external-format :default)
593     (string nil stringp))
594 rtoy 1.1 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
595 rtoy 1.2.4.3.2.1 (type kernel:index start)
596     (type (or kernel:index null) end)
597 rtoy 1.2.4.1 (type (or simple-string null) string))
598     (multiple-value-bind (string pos)
599 rtoy 1.2.4.3.2.8 (funcall (ef-octets-to-string external-format)
600 rtoy 1.2.4.1 octets (1- start) (1- (or end (length octets)))
601     (or string (make-string (length octets))))
602 rtoy 1.2 (values (if stringp string (lisp::shrink-vector string pos)) pos)))
603 rtoy 1.1
604    
605    
606 rtoy 1.2.4.1 (def-ef-macro ef-encode (extfmt lisp::lisp +ef-max+ +ef-en+)
607     `(lambda (string start end result &aux (ptr 0) (state nil))
608     (declare #|(optimize (speed 3) (safety 0) (space 0) (debug 0))|#
609     (type simple-string string)
610 rtoy 1.2.4.3.2.1 (type kernel:index start end ptr)
611 rtoy 1.2.4.1 (type simple-base-string result)
612     (ignorable state))
613     (dotimes (i (- end start) (values result ptr))
614 rtoy 1.2.4.3.2.1 (declare (type kernel:index i))
615 rtoy 1.2.4.3.2.8 (char-to-octets ,extfmt (schar string (+ start i)) state
616     (lambda (b)
617     (when (= ptr (length result))
618     (setq result (adjust-array result (* 2 ptr))))
619     (setf (aref result (1- (incf ptr)))
620     (code-char b)))))))
621 rtoy 1.2.4.1
622     (defun string-encode (string external-format &optional (start 0) end)
623 rtoy 1.2.4.3.2.8 (when (zerop (length string))
624     (return-from string-encode string))
625 rtoy 1.2.4.1 (multiple-value-bind (result ptr)
626     (lisp::with-array-data ((string string) (start start) (end end))
627 rtoy 1.2.4.3.2.8 (funcall (ef-encode external-format) string start end
628 rtoy 1.2.4.1 (make-string (length string) :element-type 'base-char)))
629     (lisp::shrink-vector result ptr)))
630    
631     (def-ef-macro ef-decode (extfmt lisp::lisp +ef-max+ +ef-de+)
632     `(lambda (string ptr end result &aux (pos -1) (count 0) (state nil))
633     (declare #|(optimize (speed 3) (safety 0) (space 0) (debug 0))|#
634     (type simple-string string)
635 rtoy 1.2.4.3.2.1 (type kernel:index end count)
636 rtoy 1.2.4.1 (type (integer -1 (#.array-dimension-limit)) ptr pos)
637     (type simple-string result)
638     (ignorable state))
639     (loop until (>= ptr end)
640     ;; increasing size of result shouldn't ever be necessary, unless
641     ;; someone implements an encoding smaller than the source string...
642     do (setf (schar result (incf pos))
643 rtoy 1.2.4.3.2.8 (octets-to-char ,extfmt state count
644     ;; note the need to return NIL for EOF
645     (if (= (1+ ptr) (length string))
646     nil
647     (char-code (char string (incf ptr))))
648     (lambda (n) (decf ptr n))))
649 rtoy 1.2.4.3 finally (return (values result (1+ pos))))))
650 rtoy 1.2.4.1
651     (defun string-decode (string external-format &optional (start 0) end)
652 rtoy 1.2.4.3.2.8 (when (zerop (length string))
653     (return-from string-decode string))
654 rtoy 1.2.4.1 (multiple-value-bind (result pos)
655     (lisp::with-array-data ((string string) (start start) (end end))
656 rtoy 1.2.4.3.2.8 (funcall (ef-decode external-format)
657 rtoy 1.2.4.1 string (1- start) (1- end) (make-string (length string))))
658 rtoy 1.2.4.3 (lisp::shrink-vector result pos)))

  ViewVC Help
Powered by ViewVC 1.1.5