0.9.10.1: Unicode character names -- aka More Bloat
[sbcl.git] / src / code / target-char.lisp
1 ;;;; character functions
2 ;;;;
3 ;;;; This implementation assumes the use of ASCII codes and the
4 ;;;; specific character formats used in SBCL (and its ancestor, CMU
5 ;;;; CL). It is optimized for performance rather than for portability
6 ;;;; and elegance, and may have to be rewritten if the character
7 ;;;; representation is changed.
8 ;;;;
9 ;;;; KLUDGE: As of sbcl-0.6.11.25, at least, the ASCII-dependence is
10 ;;;; not confined to this file. E.g. there are DEFTRANSFORMs in
11 ;;;; srctran.lisp for CHAR-UPCASE, CHAR-EQUAL, and CHAR-DOWNCASE, and
12 ;;;; they assume ASCII. -- WHN 2001-03-25
13
14 ;;;; This software is part of the SBCL system. See the README file for
15 ;;;; more information.
16 ;;;;
17 ;;;; This software is derived from the CMU CL system, which was
18 ;;;; written at Carnegie Mellon University and released into the
19 ;;;; public domain. The software is in the public domain and is
20 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
21 ;;;; files for more information.
22
23 (in-package "SB!IMPL")
24
25 ;;; We compile some trivial character operations via inline expansion.
26 #!-sb-fluid
27 (declaim (inline standard-char-p graphic-char-p alpha-char-p
28                  upper-case-p lower-case-p both-case-p alphanumericp
29                  char-int))
30 (declaim (maybe-inline digit-char-p digit-weight))
31
32 (deftype char-code ()
33   `(integer 0 (,char-code-limit)))
34
35 (defvar *character-database*)
36 (declaim (type (simple-array (unsigned-byte 8) (*)) *character-database*))
37
38 #!+sb-unicode
39 (progn
40  (defvar *unicode-character-name-database*)
41  (defvar *unicode-character-name-huffman-tree*))
42
43 (macrolet ((frob ()
44              (flet ((file (name type)
45                       (merge-pathnames (make-pathname
46                                         :directory
47                                         '(:relative :up :up "output")
48                                         :name name :type type)
49                                        sb!xc:*compile-file-truename*)))
50                `(progn
51                   ,(with-open-file (stream (file "ucd" "dat")
52                                            :direction :input
53                                            :element-type '(unsigned-byte 8))
54                      (let* ((length (file-length stream))
55                             (array (make-array
56                                     length :element-type '(unsigned-byte 8))))
57                        (read-sequence array stream)
58                        `(defun !character-database-cold-init ()
59                           (setq *character-database* ',array))))
60                   ,(with-open-file (stream (file "ucd-names" "lisp-expr")
61                                            :direction :input
62                                            :element-type 'character)
63                      (let ((names (make-hash-table)))
64                        #!+sb-unicode
65                        (loop
66                           for code-point = (read stream nil nil)
67                           for char-name = (string-upcase (read stream nil nil))
68                           while code-point
69                           do (setf (gethash code-point names) char-name))
70                        (let ((tree (make-huffman-tree
71                                     (let (list)
72                                       (maphash (lambda (code name)
73                                                  (declare (ignore code))
74                                                  (push name list))
75                                                names)
76                                       list)))
77                              (code->name
78                               (make-array (hash-table-count names)
79                                           :fill-pointer 0))
80                              (name->code nil))
81                          (maphash (lambda (code name)
82                                     (vector-push
83                                      (cons code (huffman-encode name tree))
84                                      code->name))
85                                    names)
86                          (setf name->code
87                                (sort (copy-seq code->name) #'< :key #'cdr))
88                          (setf code->name
89                                (sort (copy-seq name->code) #'< :key #'car))
90                          (setf names nil)
91                          `(defun !character-name-database-cold-init ()
92                             #!+sb-unicode
93                             (setq *unicode-character-name-database*
94                                   (cons ',code->name ',name->code)
95                                   *unicode-character-name-huffman-tree* ',tree)))))))))
96   (frob))
97 #+sb-xc-host (!character-database-cold-init)
98 #+sb-xc-host (!character-name-database-cold-init)
99
100 (defparameter *base-char-name-alist*
101   ;; Note: The *** markers here indicate character names which are
102   ;; required by the ANSI specification of #'CHAR-NAME. For the others,
103   ;; we prefer the ASCII standard name.
104   '((#x00 "Nul" "Null" "^@")
105     (#x01 "Soh" "^a")
106     (#x02 "Stx" "^b")
107     (#x03 "Etx" "^c")
108     (#x04 "Eot" "^d")
109     (#x05 "Enq" "^e")
110     (#x06 "Ack" "^f")
111     (#x07 "Bel" "Bell" "^g")
112     (#x08 "Backspace" "^h" "Bs") ; *** See Note above
113     (#x09 "Tab" "^i" "Ht") ; *** See Note above
114     (#x0A "Newline" "Linefeed" "^j" "Lf" "Nl") ; *** See Note above
115     (#x0B "Vt" "^k")
116     (#x0C "Page" "^l" "Form" "Formfeed" "Ff" "Np") ; *** See Note above
117     (#x0D "Return" "^m" "Cr") ; *** See Note above
118     (#x0E "So" "^n")
119     (#x0F "Si" "^o")
120     (#x10 "Dle" "^p")
121     (#x11 "Dc1" "^q")
122     (#x12 "Dc2" "^r")
123     (#x13 "Dc3" "^s")
124     (#x14 "Dc4" "^t")
125     (#x15 "Nak" "^u")
126     (#x16 "Syn" "^v")
127     (#x17 "Etb" "^w")
128     (#x18 "Can" "^x")
129     (#x19 "Em" "^y")
130     (#x1A "Sub" "^z")
131     (#x1B "Esc" "Escape" "^[" "Altmode" "Alt")
132     (#x1C "Fs" "^\\")
133     (#x1D "Gs" "^]")
134     (#x1E "Rs" "^^")
135     (#x1F "Us" "^_")
136     (#x20 "Space" "Sp") ; *** See Note above
137     (#x7f "Rubout" "Delete" "Del")
138     (#x80 "C80")
139     (#x81 "C81")
140     (#x82 "Break-Permitted")
141     (#x83 "No-Break-Permitted")
142     (#x84 "C84")
143     (#x85 "Next-Line")
144     (#x86 "Start-Selected-Area")
145     (#x87 "End-Selected-Area")
146     (#x88 "Character-Tabulation-Set")
147     (#x89 "Character-Tabulation-With-Justification")
148     (#x8A "Line-Tabulation-Set")
149     (#x8B "Partial-Line-Forward")
150     (#x8C "Partial-Line-Backward")
151     (#x8D "Reverse-Linefeed")
152     (#x8E "Single-Shift-Two")
153     (#x8F "Single-Shift-Three")
154     (#x90 "Device-Control-String")
155     (#x91 "Private-Use-One")
156     (#x92 "Private-Use-Two")
157     (#x93 "Set-Transmit-State")
158     (#x94 "Cancel-Character")
159     (#x95 "Message-Waiting")
160     (#x96 "Start-Guarded-Area")
161     (#x97 "End-Guarded-Area")
162     (#x98 "Start-String")
163     (#x99 "C99")
164     (#x9A "Single-Character-Introducer")
165     (#x9B "Control-Sequence-Introducer")
166     (#x9C "String-Terminator")
167     (#x9D "Operating-System-Command")
168     (#x9E "Privacy-Message")
169     (#x9F "Application-Program-Command"))) ; *** See Note above
170 \f
171 ;;;; accessor functions
172
173 ;; (* 8 186) => 1488
174 ;; (+ 1488 (ash #x110000 -8)) => 5840
175 (defun ucd-index (char)
176   (let* ((cp (char-code char))
177          (cp-high (ash cp -8))
178          (page (aref *character-database* (+ 1488 cp-high))))
179     (+ 5840 (ash page 10) (ash (ldb (byte 8 0) cp) 2))))
180
181 (defun ucd-value-0 (char)
182   (aref *character-database* (ucd-index char)))
183
184 (defun ucd-value-1 (char)
185   (let ((index (ucd-index char)))
186     (dpb (aref *character-database* (+ index 3))
187          (byte 8 16)
188          (dpb (aref *character-database* (+ index 2))
189               (byte 8 8)
190               (aref *character-database* (1+ index))))))
191
192 (defun ucd-general-category (char)
193   (aref *character-database* (* 8 (ucd-value-0 char))))
194
195 (defun ucd-decimal-digit (char)
196   (let ((decimal-digit (aref *character-database*
197                              (+ 3 (* 8 (ucd-value-0 char))))))
198     (when (< decimal-digit 10)
199       decimal-digit)))
200
201 (defun char-code (char)
202   #!+sb-doc
203   "Return the integer code of CHAR."
204   ;; FIXME: do we actually need this?
205   (etypecase char
206     (character (char-code (truly-the character char)))))
207
208 (defun char-int (char)
209   #!+sb-doc
210   "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
211    there are no character bits or fonts.)"
212   (char-code char))
213
214 (defun code-char (code)
215   #!+sb-doc
216   "Return the character with the code CODE."
217   (code-char code))
218
219 (defun character (object)
220   #!+sb-doc
221   "Coerce OBJECT into a CHARACTER if possible. Legal inputs are
222   characters, strings and symbols of length 1."
223   (flet ((do-error (control args)
224            (error 'simple-type-error
225                   :datum object
226                   ;;?? how to express "symbol with name of length 1"?
227                   :expected-type '(or character (string 1))
228                   :format-control control
229                   :format-arguments args)))
230     (typecase object
231       (character object)
232       (string (if (= 1 (length (the string object)))
233                   (char object 0)
234                   (do-error
235                    "String is not of length one: ~S" (list object))))
236       (symbol (if (= 1 (length (symbol-name object)))
237                   (schar (symbol-name object) 0)
238                   (do-error
239                    "Symbol name is not of length one: ~S" (list object))))
240       (t (do-error "~S cannot be coerced to a character." (list object))))))
241
242 (defun char-name (char)
243   #!+sb-doc
244   "Return the name (a STRING) for a CHARACTER object."
245   (let ((char-code (char-code char)))
246     (or (second (assoc char-code *base-char-name-alist*))
247         #!+sb-unicode
248         (let ((h-code (cdr (binary-search char-code
249                                           (car *unicode-character-name-database*)
250                                           :key #'car))))
251           (when h-code
252             (huffman-decode h-code *unicode-character-name-huffman-tree*))))))
253
254 (defun name-char (name)
255   #!+sb-doc
256   "Given an argument acceptable to STRING, NAME-CHAR returns a character
257   whose name is that string, if one exists. Otherwise, NIL is returned."
258   (or (let ((char-code (car (rassoc-if (lambda (names)
259                                          (member name names :test #'string-equal))
260                                        *base-char-name-alist*))))
261         (when char-code
262           (code-char char-code)))
263       #!+sb-unicode
264       (let ((encoding (huffman-encode (string-upcase name)
265                                        *unicode-character-name-huffman-tree*)))
266         (when encoding
267           (let ((char-code
268                  (car (binary-search encoding
269                                      (cdr *unicode-character-name-database*)
270                                      :key #'cdr))))
271             (when char-code
272               (code-char char-code)))))))
273 \f
274 ;;;; predicates
275
276 (defun standard-char-p (char)
277   #!+sb-doc
278   "The argument must be a character object. STANDARD-CHAR-P returns T if the
279    argument is a standard character -- one of the 95 ASCII printing characters
280    or <return>."
281   (and (typep char 'base-char)
282        (let ((n (char-code (the base-char char))))
283          (or (< 31 n 127)
284              (= n 10)))))
285
286 (defun %standard-char-p (thing)
287   #!+sb-doc
288   "Return T if and only if THING is a standard-char. Differs from
289   STANDARD-CHAR-P in that THING doesn't have to be a character."
290   (and (characterp thing) (standard-char-p thing)))
291
292 (defun graphic-char-p (char)
293   #!+sb-doc
294   "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
295   argument is a printing character (space through ~ in ASCII), otherwise
296   returns NIL."
297   (let ((n (char-code char)))
298     (or (< 31 n 127)
299         (< 159 n))))
300
301 (defun alpha-char-p (char)
302   #!+sb-doc
303   "The argument must be a character object. ALPHA-CHAR-P returns T if the
304    argument is an alphabetic character, A-Z or a-z; otherwise NIL."
305   (< (ucd-general-category char) 5))
306
307 (defun upper-case-p (char)
308   #!+sb-doc
309   "The argument must be a character object; UPPER-CASE-P returns T if the
310    argument is an upper-case character, NIL otherwise."
311   (= (ucd-value-0 char) 0))
312
313 (defun lower-case-p (char)
314   #!+sb-doc
315   "The argument must be a character object; LOWER-CASE-P returns T if the
316    argument is a lower-case character, NIL otherwise."
317   (= (ucd-value-0 char) 1))
318
319 (defun both-case-p (char)
320   #!+sb-doc
321   "The argument must be a character object. BOTH-CASE-P returns T if the
322   argument is an alphabetic character and if the character exists in
323   both upper and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
324   (< (ucd-value-0 char) 2))
325
326 (defun digit-char-p (char &optional (radix 10.))
327   #!+sb-doc
328   "If char is a digit in the specified radix, returns the fixnum for
329   which that digit stands, else returns NIL."
330   (let ((m (- (char-code char) 48)))
331     (declare (fixnum m))
332     (cond ((<= radix 10.)
333            ;; Special-case decimal and smaller radices.
334            (if (and (>= m 0) (< m radix))  m  nil))
335           ;; Digits 0 - 9 are used as is, since radix is larger.
336           ((and (>= m 0) (< m 10)) m)
337           ;; Check for upper case A - Z.
338           ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
339           ;; Also check lower case a - z.
340           ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
341           ;; Else, fail.
342           (t (let ((number (ucd-decimal-digit char)))
343                (when (and number (< number radix))
344                  number))))))
345
346 (defun alphanumericp (char)
347   #!+sb-doc
348   "Given a character-object argument, ALPHANUMERICP returns T if the
349    argument is either numeric or alphabetic."
350   (let ((gc (ucd-general-category char)))
351     (or (< gc 5)
352         (= gc 12))))
353
354 (defun char= (character &rest more-characters)
355   #!+sb-doc
356   "Return T if all of the arguments are the same character."
357   (dolist (c more-characters t)
358     (declare (type character c))
359     (unless (eq c character) (return nil))))
360
361 (defun char/= (character &rest more-characters)
362   #!+sb-doc
363   "Return T if no two of the arguments are the same character."
364   (do* ((head character (car list))
365         (list more-characters (cdr list)))
366        ((null list) t)
367     (declare (type character head))
368     (dolist (c list)
369       (declare (type character c))
370       (when (eq head c) (return-from char/= nil)))))
371
372 (defun char< (character &rest more-characters)
373   #!+sb-doc
374   "Return T if the arguments are in strictly increasing alphabetic order."
375   (do* ((c character (car list))
376         (list more-characters (cdr list)))
377        ((null list) t)
378     (unless (< (char-int c)
379                (char-int (car list)))
380       (return nil))))
381
382 (defun char> (character &rest more-characters)
383   #!+sb-doc
384   "Return T if the arguments are in strictly decreasing alphabetic order."
385   (do* ((c character (car list))
386         (list more-characters (cdr list)))
387        ((null list) t)
388     (unless (> (char-int c)
389                (char-int (car list)))
390       (return nil))))
391
392 (defun char<= (character &rest more-characters)
393   #!+sb-doc
394   "Return T if the arguments are in strictly non-decreasing alphabetic order."
395   (do* ((c character (car list))
396         (list more-characters (cdr list)))
397        ((null list) t)
398     (unless (<= (char-int c)
399                 (char-int (car list)))
400       (return nil))))
401
402 (defun char>= (character &rest more-characters)
403   #!+sb-doc
404   "Return T if the arguments are in strictly non-increasing alphabetic order."
405   (do* ((c character (car list))
406         (list more-characters (cdr list)))
407        ((null list) t)
408     (unless (>= (char-int c)
409                 (char-int (car list)))
410       (return nil))))
411
412 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
413 ;;;  which loses font, bits, and case info.
414
415 (defmacro equal-char-code (character)
416   (let ((ch (gensym)))
417     `(let ((,ch ,character))
418       (if (= (ucd-value-0 ,ch) 0)
419           (ucd-value-1 ,ch)
420           (char-code ,ch)))))
421
422 (defun char-equal (character &rest more-characters)
423   #!+sb-doc
424   "Return T if all of the arguments are the same character.
425   Font, bits, and case are ignored."
426   (do ((clist more-characters (cdr clist)))
427       ((null clist) t)
428     (unless (= (equal-char-code (car clist))
429                (equal-char-code character))
430       (return nil))))
431
432 (defun char-not-equal (character &rest more-characters)
433   #!+sb-doc
434   "Return T if no two of the arguments are the same character.
435    Font, bits, and case are ignored."
436   (do* ((head character (car list))
437         (list more-characters (cdr list)))
438        ((null list) t)
439     (unless (do* ((l list (cdr l)))
440                  ((null l) t)
441               (if (= (equal-char-code head)
442                      (equal-char-code (car l)))
443                   (return nil)))
444       (return nil))))
445
446 (defun char-lessp (character &rest more-characters)
447   #!+sb-doc
448   "Return T if the arguments are in strictly increasing alphabetic order.
449    Font, bits, and case are ignored."
450   (do* ((c character (car list))
451         (list more-characters (cdr list)))
452        ((null list) t)
453     (unless (< (equal-char-code c)
454                (equal-char-code (car list)))
455       (return nil))))
456
457 (defun char-greaterp (character &rest more-characters)
458   #!+sb-doc
459   "Return T if the arguments are in strictly decreasing alphabetic order.
460    Font, bits, and case are ignored."
461   (do* ((c character (car list))
462         (list more-characters (cdr list)))
463        ((null list) t)
464     (unless (> (equal-char-code c)
465                (equal-char-code (car list)))
466       (return nil))))
467
468 (defun char-not-greaterp (character &rest more-characters)
469   #!+sb-doc
470   "Return T if the arguments are in strictly non-decreasing alphabetic order.
471    Font, bits, and case are ignored."
472   (do* ((c character (car list))
473         (list more-characters (cdr list)))
474        ((null list) t)
475     (unless (<= (equal-char-code c)
476                 (equal-char-code (car list)))
477       (return nil))))
478
479 (defun char-not-lessp (character &rest more-characters)
480   #!+sb-doc
481   "Return T if the arguments are in strictly non-increasing alphabetic order.
482    Font, bits, and case are ignored."
483   (do* ((c character (car list))
484         (list more-characters (cdr list)))
485        ((null list) t)
486     (unless (>= (equal-char-code c)
487                 (equal-char-code (car list)))
488       (return nil))))
489 \f
490 ;;;; miscellaneous functions
491
492 (defun char-upcase (char)
493   #!+sb-doc
494   "Return CHAR converted to upper-case if that is possible.  Don't convert
495    lowercase eszet (U+DF)."
496   (if (= (ucd-value-0 char) 1)
497       (code-char (ucd-value-1 char))
498       char))
499
500 (defun char-downcase (char)
501   #!+sb-doc
502   "Return CHAR converted to lower-case if that is possible."
503   (if (= (ucd-value-0 char) 0)
504       (code-char (ucd-value-1 char))
505       char))
506
507 (defun digit-char (weight &optional (radix 10))
508   #!+sb-doc
509   "All arguments must be integers. Returns a character object that
510   represents a digit of the given weight in the specified radix. Returns
511   NIL if no such character exists."
512   (and (typep weight 'fixnum)
513        (>= weight 0) (< weight radix) (< weight 36)
514        (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))