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