0c2b5f7568ce277026bd3a6ee096936ec63dc05c
[sbcl.git] / src / code / target-char.lisp
1 ;;;; character functions
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!IMPL")
13
14 ;;; We compile some trivial character operations via inline expansion.
15 #!-sb-fluid
16 (declaim (inline standard-char-p graphic-char-p alpha-char-p
17                  upper-case-p lower-case-p both-case-p alphanumericp
18                  char-int))
19 (declaim (maybe-inline digit-char-p digit-weight))
20
21 (deftype char-code ()
22   `(integer 0 (,char-code-limit)))
23
24 #!+sb-unicode
25 (progn
26  (defvar *unicode-character-name-database*)
27  (defvar *unicode-character-name-huffman-tree*))
28
29 (macrolet
30     ((frob ()
31        (flet ((file (name type)
32                 (merge-pathnames (make-pathname
33                                   :directory
34                                   '(:relative :up :up "output")
35                                   :name name :type type)
36                                  sb!xc:*compile-file-truename*))
37               (read-ub8-vector (pathname)
38                 (with-open-file (stream pathname
39                                         :direction :input
40                                         :element-type '(unsigned-byte 8))
41                   (let* ((length (file-length stream))
42                          (array (make-array
43                                  length :element-type '(unsigned-byte 8))))
44                     (read-sequence array stream)
45                     array))))
46          (let ((character-database (read-ub8-vector (file "ucd" "dat")))
47                (decompositions (read-ub8-vector (file "decomp" "dat")))
48                (long-decompositions (read-ub8-vector (file "ldecomp" "dat"))))
49            `(progn
50               (declaim (type (simple-array (unsigned-byte 8) (*)) **character-database** **character-decompositions** **character-long-decompositions**))
51               (defglobal **character-database** ,character-database)
52               (defglobal **character-decompositions** ,decompositions)
53               (defglobal **character-long-decompositions** ,long-decompositions)
54               (defun !character-database-cold-init ()
55                 (setf **character-database** ,character-database))
56               ,(with-open-file (stream (file "ucd-names" "lisp-expr")
57                                        :direction :input
58                                        :element-type 'character)
59                                (let ((names (make-hash-table)))
60                                  #!+sb-unicode
61                                  (loop
62                                        for code-point = (read stream nil nil)
63                                        for char-name = (string-upcase (read stream nil nil))
64                                        while code-point
65                                        do (setf (gethash code-point names) char-name))
66                                  (let ((tree
67                                         #!+sb-unicode
68                                          (make-huffman-tree
69                                           (let (list)
70                                             (maphash (lambda (code name)
71                                                        (declare (ignore code))
72                                                        (push name list))
73                                                      names)
74                                             list)))
75                                        (code->name
76                                         (make-array (hash-table-count names)
77                                                     :fill-pointer 0))
78                                        (name->code nil))
79                                    (maphash (lambda (code name)
80                                               (vector-push
81                                                (cons code (huffman-encode name tree))
82                                                code->name))
83                                             names)
84                                    (setf name->code
85                                          (sort (copy-seq code->name) #'< :key #'cdr))
86                                    (setf code->name
87                                          (sort (copy-seq name->code) #'< :key #'car))
88                                    (setf names nil)
89                                    `(defun !character-name-database-cold-init ()
90                                       #!+sb-unicode
91                                       (setq *unicode-character-name-database*
92                                             (cons ',code->name ',name->code)
93                                             *unicode-character-name-huffman-tree* ',tree))))))))))
94   (frob))
95 #+sb-xc-host (!character-name-database-cold-init)
96
97 (defparameter *base-char-name-alist*
98   ;; Note: The *** markers here indicate character names which are
99   ;; required by the ANSI specification of #'CHAR-NAME. For the others,
100   ;; we prefer the ASCII standard name.
101   '((#x00 "Nul" "Null" "^@")
102     (#x01 "Soh" "^a")
103     (#x02 "Stx" "^b")
104     (#x03 "Etx" "^c")
105     (#x04 "Eot" "^d")
106     (#x05 "Enq" "^e")
107     (#x06 "Ack" "^f")
108     (#x07 "Bel" "Bell" "^g")
109     (#x08 "Backspace" "^h" "Bs") ; *** See Note above
110     (#x09 "Tab" "^i" "Ht") ; *** See Note above
111     (#x0A "Newline" "Linefeed" "^j" "Lf" "Nl") ; *** See Note above
112     (#x0B "Vt" "^k")
113     (#x0C "Page" "^l" "Form" "Formfeed" "Ff" "Np") ; *** See Note above
114     (#x0D "Return" "^m" "Cr") ; *** See Note above
115     (#x0E "So" "^n")
116     (#x0F "Si" "^o")
117     (#x10 "Dle" "^p")
118     (#x11 "Dc1" "^q")
119     (#x12 "Dc2" "^r")
120     (#x13 "Dc3" "^s")
121     (#x14 "Dc4" "^t")
122     (#x15 "Nak" "^u")
123     (#x16 "Syn" "^v")
124     (#x17 "Etb" "^w")
125     (#x18 "Can" "^x")
126     (#x19 "Em" "^y")
127     (#x1A "Sub" "^z")
128     (#x1B "Esc" "Escape" "^[" "Altmode" "Alt")
129     (#x1C "Fs" "^\\")
130     (#x1D "Gs" "^]")
131     (#x1E "Rs" "^^")
132     (#x1F "Us" "^_")
133     (#x20 "Space" "Sp") ; *** See Note above
134     (#x7f "Rubout" "Delete" "Del")
135     (#x80 "C80")
136     (#x81 "C81")
137     (#x82 "Break-Permitted")
138     (#x83 "No-Break-Permitted")
139     (#x84 "C84")
140     (#x85 "Next-Line")
141     (#x86 "Start-Selected-Area")
142     (#x87 "End-Selected-Area")
143     (#x88 "Character-Tabulation-Set")
144     (#x89 "Character-Tabulation-With-Justification")
145     (#x8A "Line-Tabulation-Set")
146     (#x8B "Partial-Line-Forward")
147     (#x8C "Partial-Line-Backward")
148     (#x8D "Reverse-Linefeed")
149     (#x8E "Single-Shift-Two")
150     (#x8F "Single-Shift-Three")
151     (#x90 "Device-Control-String")
152     (#x91 "Private-Use-One")
153     (#x92 "Private-Use-Two")
154     (#x93 "Set-Transmit-State")
155     (#x94 "Cancel-Character")
156     (#x95 "Message-Waiting")
157     (#x96 "Start-Guarded-Area")
158     (#x97 "End-Guarded-Area")
159     (#x98 "Start-String")
160     (#x99 "C99")
161     (#x9A "Single-Character-Introducer")
162     (#x9B "Control-Sequence-Introducer")
163     (#x9C "String-Terminator")
164     (#x9D "Operating-System-Command")
165     (#x9E "Privacy-Message")
166     (#x9F "Application-Program-Command"))) ; *** See Note above
167 \f
168 ;;;; UCD accessor functions
169
170 ;;; The first (* 8 395) => 3160 entries in **CHARACTER-DATABASE**
171 ;;; contain entries for the distinct character attributes:
172 ;;; specifically, indexes into the GC kinds, Bidi kinds, CCC kinds,
173 ;;; the decimal digit property, the digit property and the
174 ;;; bidi-mirrored boolean property.  (There are two spare bytes for
175 ;;; other information, should that become necessary)
176 ;;;
177 ;;; the next (ash #x110000 -8) entries contain single-byte indexes
178 ;;; into a table of 256-element 4-byte-sized entries.  These entries
179 ;;; follow directly on, and are of the form
180 ;;; {attribute-index[11b],transformed-code-point[21b]}x256, where the
181 ;;; attribute index is an index into the miscellaneous information
182 ;;; table, and the transformed code point is the code point of the
183 ;;; simple mapping of the character to its lowercase or uppercase
184 ;;; equivalent, as appropriate and if any.
185 ;;;
186 ;;; I feel the opacity of the above suggests the need for a diagram:
187 ;;;
188 ;;;         C  _______________________________________
189 ;;;           /                                       \
190 ;;;          L                                         \
191 ;;;  [***************|=============================|--------...]
192 ;;;                 (a)      \                       _
193 ;;;                         A \______________________/| B
194 ;;;
195 ;;; To look up information about a character, take the high 13 bits of
196 ;;; its code point, and index the character database with that and a
197 ;;; base of 3160 (going past the miscellaneous information[*], so
198 ;;; treating (a) as the start of the array).  This, labelled A, gives
199 ;;; us another index into the detailed pages[-], which we can use to
200 ;;; look up the details for the character in question: we add the low
201 ;;; 8 bits of the character, shifted twice (because we have four-byte
202 ;;; table entries) to 1024 times the `page' index, with a base of 6088
203 ;;; to skip over everything else.  This gets us to point B.  If we're
204 ;;; after a transformed code point (i.e. an upcase or downcase
205 ;;; operation), we can simply read it off now, beginning with an
206 ;;; offset of 11 bits from point B in some endianness; if we're
207 ;;; looking for miscellaneous information, we take the 11-bit value at
208 ;;; B, and index the character database once more to get to the
209 ;;; relevant miscellaneous information.
210 ;;;
211 ;;; As an optimization to the common case (pun intended) of looking up
212 ;;; case information for a character, the entries in C above are
213 ;;; sorted such that the characters which are UPPER-CASE-P in CL terms
214 ;;; have index values lower than all others, followed by those which
215 ;;; are LOWER-CASE-P in CL terms; this permits implementation of
216 ;;; character case tests without actually going to the trouble of
217 ;;; looking up the value associated with the index.  (Actually, this
218 ;;; isn't just a speed optimization; the information about whether a
219 ;;; character is BOTH-CASE-P is used just in the ordering and not
220 ;;; explicitly recorded in the database).
221 ;;;
222 ;;; The moral of all this?  Next time, don't just say "FIXME: document
223 ;;; this"
224 (defun ucd-index (char)
225   (let* ((cp (char-code char))
226          (cp-high (ash cp -8))
227          (page (aref **character-database** (+ 3160 cp-high))))
228     (+ 7512 (ash page 10) (ash (ldb (byte 8 0) cp) 2))))
229
230 (declaim (ftype (sfunction (t) (unsigned-byte 11)) ucd-value-0))
231 (defun ucd-value-0 (char)
232   (let ((index (ucd-index char))
233         (character-database **character-database**))
234     (dpb (aref character-database index)
235          (byte 8 3)
236          (ldb (byte 3 5) (aref character-database (+ index 1))))))
237
238 (declaim (ftype (sfunction (t) (unsigned-byte 21)) ucd-value-1))
239 (defun ucd-value-1 (char)
240   (let ((index (ucd-index char))
241         (character-database **character-database**))
242     (dpb (aref character-database (+ index 1))
243          (byte 5 16)
244          (dpb (aref character-database (+ index 2))
245               (byte 8 8)
246               (aref character-database (+ index 3))))))
247
248 (declaim (ftype (sfunction (t) (unsigned-byte 8)) ucd-general-category))
249 (defun ucd-general-category (char)
250   (aref **character-database** (* 8 (ucd-value-0 char))))
251
252 (defun ucd-decimal-digit (char)
253   (let ((decimal-digit (aref **character-database**
254                              (+ 3 (* 8 (ucd-value-0 char))))))
255     (when (< decimal-digit 10)
256       decimal-digit)))
257
258 (defun char-code (char)
259   #!+sb-doc
260   "Return the integer code of CHAR."
261   (char-code char))
262
263 (defun char-int (char)
264   #!+sb-doc
265   "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
266 there are no character bits or fonts.)"
267   (char-code char))
268
269 (defun code-char (code)
270   #!+sb-doc
271   "Return the character with the code CODE."
272   (code-char code))
273
274 (defun character (object)
275   #!+sb-doc
276   "Coerce OBJECT into a CHARACTER if possible. Legal inputs are characters,
277 strings and symbols of length 1."
278   (flet ((do-error (control args)
279            (error 'simple-type-error
280                   :datum object
281                   ;;?? how to express "symbol with name of length 1"?
282                   :expected-type '(or character (string 1))
283                   :format-control control
284                   :format-arguments args)))
285     (typecase object
286       (character object)
287       (string (if (= 1 (length (the string object)))
288                   (char object 0)
289                   (do-error
290                    "String is not of length one: ~S" (list object))))
291       (symbol (if (= 1 (length (symbol-name object)))
292                   (schar (symbol-name object) 0)
293                   (do-error
294                    "Symbol name is not of length one: ~S" (list object))))
295       (t (do-error "~S cannot be coerced to a character." (list object))))))
296
297 (defun char-name (char)
298   #!+sb-doc
299   "Return the name (a STRING) for a CHARACTER object."
300   (let ((char-code (char-code char)))
301     (or (second (assoc char-code *base-char-name-alist*))
302         #!+sb-unicode
303         (let ((h-code (cdr (binary-search char-code
304                                           (car *unicode-character-name-database*)
305                                           :key #'car))))
306           (cond
307             (h-code
308              (huffman-decode h-code *unicode-character-name-huffman-tree*))
309             ((< char-code #x10000)
310              (format nil "U~4,'0X" char-code))
311             (t
312              (format nil "U~8,'0X" char-code)))))))
313
314 (defun name-char (name)
315   #!+sb-doc
316   "Given an argument acceptable to STRING, NAME-CHAR returns a character whose
317 name is that string, if one exists. Otherwise, NIL is returned."
318   (or (let ((char-code (car (rassoc-if (lambda (names)
319                                          (member name names :test #'string-equal))
320                                        *base-char-name-alist*))))
321         (when char-code
322           (code-char char-code)))
323       #!+sb-unicode
324       (let ((encoding (huffman-encode (string-upcase name)
325                                        *unicode-character-name-huffman-tree*)))
326         (when encoding
327           (let* ((char-code
328                   (car (binary-search encoding
329                                       (cdr *unicode-character-name-database*)
330                                       :key #'cdr)))
331                  (name-string (string name))
332                  (name-length (length name-string)))
333             (cond
334               (char-code
335                (code-char char-code))
336               ((and (or (= name-length 9)
337                         (= name-length 5))
338                     (char-equal (char name-string 0) #\U)
339                     (loop for i from 1 below name-length
340                           always (digit-char-p (char name-string i) 16)))
341                (code-char (parse-integer name-string :start 1 :radix 16)))
342               (t
343                nil)))))))
344 \f
345 ;;;; predicates
346
347 (defun standard-char-p (char)
348   #!+sb-doc
349   "The argument must be a character object. STANDARD-CHAR-P returns T if the
350 argument is a standard character -- one of the 95 ASCII printing characters or
351 <return>."
352   (and (typep char 'base-char)
353        (let ((n (char-code (the base-char char))))
354          (or (< 31 n 127)
355              (= n 10)))))
356
357 (defun %standard-char-p (thing)
358   #!+sb-doc
359   "Return T if and only if THING is a standard-char. Differs from
360 STANDARD-CHAR-P in that THING doesn't have to be a character."
361   (and (characterp thing) (standard-char-p thing)))
362
363 (defun graphic-char-p (char)
364   #!+sb-doc
365   "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
366 argument is a printing character (space through ~ in ASCII), otherwise returns
367 NIL."
368   (let ((n (char-code char)))
369     (or (< 31 n 127)
370         (< 159 n))))
371
372 (defun alpha-char-p (char)
373   #!+sb-doc
374   "The argument must be a character object. ALPHA-CHAR-P returns T if the
375 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
376   (< (ucd-general-category char) 5))
377
378 (defun upper-case-p (char)
379   #!+sb-doc
380   "The argument must be a character object; UPPER-CASE-P returns T if the
381 argument is an upper-case character, NIL otherwise."
382   (< (ucd-value-0 char) 4))
383
384 (defun lower-case-p (char)
385   #!+sb-doc
386   "The argument must be a character object; LOWER-CASE-P returns T if the
387 argument is a lower-case character, NIL otherwise."
388   (< 3 (ucd-value-0 char) 8))
389
390 (defun both-case-p (char)
391   #!+sb-doc
392   "The argument must be a character object. BOTH-CASE-P returns T if the
393 argument is an alphabetic character and if the character exists in both upper
394 and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
395   (< (ucd-value-0 char) 8))
396
397 (defun digit-char-p (char &optional (radix 10.))
398   #!+sb-doc
399   "If char is a digit in the specified radix, returns the fixnum for which
400 that digit stands, else returns NIL."
401   (let ((m (- (char-code char) 48)))
402     (declare (fixnum m))
403     (cond ((<= radix 10.)
404            ;; Special-case decimal and smaller radices.
405            (if (and (>= m 0) (< m radix))  m  nil))
406           ;; Digits 0 - 9 are used as is, since radix is larger.
407           ((and (>= m 0) (< m 10)) m)
408           ;; Check for upper case A - Z.
409           ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
410           ;; Also check lower case a - z.
411           ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
412           ;; Else, fail.
413           (t (let ((number (ucd-decimal-digit char)))
414                (when (and number (< number radix))
415                  number))))))
416
417 (defun alphanumericp (char)
418   #!+sb-doc
419   "Given a character-object argument, ALPHANUMERICP returns T if the argument
420 is either numeric or alphabetic."
421   (let ((gc (ucd-general-category char)))
422     (or (< gc 5)
423         (= gc 12))))
424
425 (defun char= (character &rest more-characters)
426   #!+sb-doc
427   "Return T if all of the arguments are the same character."
428   (declare (truly-dynamic-extent more-characters))
429   (dolist (c more-characters t)
430     (declare (type character c))
431     (unless (eq c character) (return nil))))
432
433 (defun char/= (character &rest more-characters)
434   #!+sb-doc
435   "Return T if no two of the arguments are the same character."
436   (declare (truly-dynamic-extent more-characters))
437   (do* ((head character (car list))
438         (list more-characters (cdr list)))
439        ((null list) t)
440     (declare (type character head))
441     (dolist (c list)
442       (declare (type character c))
443       (when (eq head c) (return-from char/= nil)))))
444
445 (defun char< (character &rest more-characters)
446   #!+sb-doc
447   "Return T if the arguments are in strictly increasing alphabetic order."
448   (declare (truly-dynamic-extent more-characters))
449   (do* ((c character (car list))
450         (list more-characters (cdr list)))
451        ((null list) t)
452     (unless (< (char-int c)
453                (char-int (car list)))
454       (return nil))))
455
456 (defun char> (character &rest more-characters)
457   #!+sb-doc
458   "Return T if the arguments are in strictly decreasing alphabetic order."
459   (declare (truly-dynamic-extent more-characters))
460   (do* ((c character (car list))
461         (list more-characters (cdr list)))
462        ((null list) t)
463     (unless (> (char-int c)
464                (char-int (car list)))
465       (return nil))))
466
467 (defun char<= (character &rest more-characters)
468   #!+sb-doc
469   "Return T if the arguments are in strictly non-decreasing alphabetic order."
470   (declare (truly-dynamic-extent more-characters))
471   (do* ((c character (car list))
472         (list more-characters (cdr list)))
473        ((null list) t)
474     (unless (<= (char-int c)
475                 (char-int (car list)))
476       (return nil))))
477
478 (defun char>= (character &rest more-characters)
479   #!+sb-doc
480   "Return T if the arguments are in strictly non-increasing alphabetic order."
481   (declare (truly-dynamic-extent more-characters))
482   (do* ((c character (car list))
483         (list more-characters (cdr list)))
484        ((null list) t)
485     (unless (>= (char-int c)
486                 (char-int (car list)))
487       (return nil))))
488
489 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
490 ;;;  which loses font, bits, and case info.
491
492 (defmacro equal-char-code (character)
493   (let ((ch (gensym)))
494     `(let ((,ch ,character))
495       (if (= (ucd-value-0 ,ch) 0)
496           (ucd-value-1 ,ch)
497           (char-code ,ch)))))
498
499 (defun two-arg-char-equal (c1 c2)
500   (= (equal-char-code c1) (equal-char-code c2)))
501
502 (defun char-equal (character &rest more-characters)
503   #!+sb-doc
504   "Return T if all of the arguments are the same character.
505 Case is ignored."
506   (declare (truly-dynamic-extent more-characters))
507   (do ((clist more-characters (cdr clist)))
508       ((null clist) t)
509     (unless (two-arg-char-equal (car clist) character)
510       (return nil))))
511
512 (defun two-arg-char-not-equal (c1 c2)
513   (/= (equal-char-code c1) (equal-char-code c2)))
514
515 (defun char-not-equal (character &rest more-characters)
516   #!+sb-doc
517   "Return T if no two of the arguments are the same character.
518 Case is ignored."
519   (declare (truly-dynamic-extent more-characters))
520   (do* ((head character (car list))
521         (list more-characters (cdr list)))
522        ((null list) t)
523     (unless (do* ((l list (cdr l)))
524                  ((null l) t)
525               (if (two-arg-char-equal head (car l))
526                   (return nil)))
527       (return nil))))
528
529 (defun two-arg-char-lessp (c1 c2)
530   (< (equal-char-code c1) (equal-char-code c2)))
531
532 (defun char-lessp (character &rest more-characters)
533   #!+sb-doc
534   "Return T if the arguments are in strictly increasing alphabetic order.
535 Case is ignored."
536   (declare (truly-dynamic-extent more-characters))
537   (do* ((c character (car list))
538         (list more-characters (cdr list)))
539        ((null list) t)
540     (unless (two-arg-char-lessp c (car list))
541       (return nil))))
542
543 (defun two-arg-char-greaterp (c1 c2)
544   (> (equal-char-code c1) (equal-char-code c2)))
545
546 (defun char-greaterp (character &rest more-characters)
547   #!+sb-doc
548   "Return T if the arguments are in strictly decreasing alphabetic order.
549 Case is ignored."
550   (declare (truly-dynamic-extent more-characters))
551   (do* ((c character (car list))
552         (list more-characters (cdr list)))
553        ((null list) t)
554     (unless (two-arg-char-greaterp c (car list))
555       (return nil))))
556
557 (defun two-arg-char-not-greaterp (c1 c2)
558   (<= (equal-char-code c1) (equal-char-code c2)))
559
560 (defun char-not-greaterp (character &rest more-characters)
561   #!+sb-doc
562   "Return T if the arguments are in strictly non-decreasing alphabetic order.
563 Case is ignored."
564   (declare (truly-dynamic-extent more-characters))
565   (do* ((c character (car list))
566         (list more-characters (cdr list)))
567        ((null list) t)
568     (unless (two-arg-char-not-greaterp c (car list))
569       (return nil))))
570
571 (defun two-arg-char-not-lessp (c1 c2)
572   (>= (equal-char-code c1) (equal-char-code c2)))
573
574 (defun char-not-lessp (character &rest more-characters)
575   #!+sb-doc
576   "Return T if the arguments are in strictly non-increasing alphabetic order.
577 Case is ignored."
578   (declare (truly-dynamic-extent more-characters))
579   (do* ((c character (car list))
580         (list more-characters (cdr list)))
581        ((null list) t)
582     (unless (two-arg-char-not-lessp c (car list))
583       (return nil))))
584 \f
585 ;;;; miscellaneous functions
586
587 (defun char-upcase (char)
588   #!+sb-doc
589   "Return CHAR converted to upper-case if that is possible. Don't convert
590 lowercase eszet (U+DF)."
591   (if (< 3 (ucd-value-0 char) 8)
592       (code-char (ucd-value-1 char))
593       char))
594
595 (defun char-downcase (char)
596   #!+sb-doc
597   "Return CHAR converted to lower-case if that is possible."
598   (if (< (ucd-value-0 char) 4)
599       (code-char (ucd-value-1 char))
600       char))
601
602 (defun digit-char (weight &optional (radix 10))
603   #!+sb-doc
604   "All arguments must be integers. Returns a character object that represents
605 a digit of the given weight in the specified radix. Returns NIL if no such
606 character exists."
607   (and (typep weight 'fixnum)
608        (>= weight 0) (< weight radix) (< weight 36)
609        (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))
610 \f
611 (defun char-decomposition-info (char)
612   (aref **character-database** (+ 6 (* 8 (ucd-value-0 char)))))
613
614 (defun char-decomposition (char)
615   (let* ((cp (char-code char))
616          (cp-high (ash cp -8))
617          (decompositions **character-decompositions**)
618          (long-decompositions **character-long-decompositions**)
619          (index (+ #x1100
620                    (ash  (aref decompositions cp-high) 10)
621                    (ash (ldb (byte 8 0) cp) 2)))
622          (v0 (aref decompositions index))
623          (v1 (aref decompositions (+ index 1)))
624          (v2 (aref decompositions (+ index 2)))
625          (v3 (aref decompositions (+ index 3)))
626          (length (dpb v0 (byte 8 3) (ldb (byte 3 5) v1)))
627          (entry (dpb (ldb (byte 5 0) v1) (byte 5 16)
628                      (dpb v2 (byte 8 8) v3))))
629     (if (= length 1)
630         (string (code-char entry))
631         (let ((result (make-string length))
632               (e (* 4 entry)))
633           (dotimes (i length result)
634             (let ((code (dpb (aref long-decompositions (+ e 1))
635                              (byte 8 16)
636                              (dpb (aref long-decompositions (+ e 2))
637                                   (byte 8 8)
638                                   (aref long-decompositions (+ e 3))))))
639               (setf (char result i) (code-char code)))
640             (incf e 4))))))
641
642 (defun decompose-char (char)
643   (if (= (char-decomposition-info char) 0)
644       (string char)
645       (char-decomposition char)))