0.6.11.26:
[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 (macrolet ((frob (char-names-list)
36              (collect ((results))
37                (dolist (code char-names-list)
38                  (destructuring-bind (ccode names) code
39                    (dolist (name names)
40                      (results (cons name (code-char ccode))))))
41                `(defparameter *char-name-alist* ',(results)
42   #!+sb-doc
43   "This is the alist of (character-name . character) for characters with
44   long names. The first name in this list for a given character is used
45   on typeout and is the preferred form for input."))))
46   (frob ((#x00 ("Null" "^@" "Nul"))
47          (#x01 ("^a" "Soh"))
48          (#x02 ("^b" "Stx"))
49          (#x03 ("^c" "Etx"))
50          (#x04 ("^d" "Eot"))
51          (#x05 ("^e" "Enq"))
52          (#x06 ("^f" "Ack"))
53          (#x07 ("Bell" "^g" "Bel"))
54          (#x08 ("Backspace" "^h" "Bs"))
55          (#x09 ("Tab" "^i" "Ht"))
56          (#x0A ("Newline" "Linefeed" "^j" "Lf" "Nl" ))
57          (#x0B ("Vt" "^k"))
58          (#x0C ("Page" "^l" "Form" "Formfeed" "Ff" "Np"))
59          (#x0D ("Return" "^m" "Cr"))
60          (#x0E ("^n" "So"))
61          (#x0F ("^o" "Si"))
62          (#x10 ("^p" "Dle"))
63          (#x11 ("^q" "Dc1"))
64          (#x12 ("^r" "Dc2"))
65          (#x13 ("^s" "Dc3"))
66          (#x14 ("^t" "Dc4"))
67          (#x15 ("^u" "Nak"))
68          (#x16 ("^v" "Syn"))
69          (#x17 ("^w" "Etb"))
70          (#x18 ("^x" "Can"))
71          (#x19 ("^y" "Em"))
72          (#x1A ("^z" "Sub"))
73          (#x1B ("Escape" "^[" "Altmode" "Esc" "Alt"))
74          (#x1C ("^\\" "Fs"))
75          (#x1D ("^]" "Gs"))
76          (#x1E ("^^" "Rs"))
77          (#x1F ("^_" "Us"))
78          (#x20 ("Space" "Sp"))
79          (#x7f ("Rubout" "Delete" "Del")))))
80 \f
81 ;;;; accessor functions
82
83 (defun char-code (char)
84   #!+sb-doc
85   "Returns the integer code of CHAR."
86   (etypecase char
87     (base-char (char-code (truly-the base-char char)))))
88
89 (defun char-int (char)
90   #!+sb-doc
91   "Returns the integer code of CHAR. This is the same as char-code, as
92    CMU Common Lisp does not implement character bits or fonts."
93   (char-code char))
94
95 (defun code-char (code)
96   #!+sb-doc
97   "Returns the character with the code CODE."
98   (declare (type char-code code))
99   (code-char code))
100
101 (defun character (object)
102   #!+sb-doc
103   "Coerces its argument into a character object if possible. Accepts
104   characters, strings and symbols of length 1."
105   (flet ((do-error (control args)
106            (error 'simple-type-error
107                   :datum object
108                   ;;?? how to express "symbol with name of length 1"?
109                   :expected-type '(or character (string 1))
110                   :format-control control
111                   :format-arguments args)))
112     (typecase object
113       (character object)
114       (string (if (= 1 (length (the string object)))
115                   (char object 0)
116                   (do-error
117                    "String is not of length one: ~S" (list object))))
118       (symbol (if (= 1 (length (symbol-name object)))
119                   (schar (symbol-name object) 0)
120                   (do-error
121                    "Symbol name is not of length one: ~S" (list object))))
122       (t (do-error "~S cannot be coerced to a character." (list object))))))
123
124 (defun char-name (char)
125   #!+sb-doc
126   "Given a character object, char-name returns the name for that
127   object (a symbol)."
128   (car (rassoc char *char-name-alist*)))
129
130 (defun name-char (name)
131   #!+sb-doc
132   "Given an argument acceptable to string, name-char returns a character
133   object whose name is that symbol, if one exists. Otherwise, () is returned."
134   (cdr (assoc (string name) *char-name-alist* :test #'string-equal)))
135 \f
136 ;;;; predicates
137
138 (defun standard-char-p (char)
139   #!+sb-doc
140   "The argument must be a character object. Standard-char-p returns T if the
141    argument is a standard character -- one of the 95 ASCII printing characters
142    or <return>."
143   (declare (character char))
144   (and (typep char 'base-char)
145        (let ((n (char-code (the base-char char))))
146          (or (< 31 n 127)
147              (= n 10)))))
148
149 (defun %standard-char-p (thing)
150   #!+sb-doc
151   "Return T if and only if THING is a standard-char. Differs from
152   standard-char-p in that THING doesn't have to be a character."
153   (and (characterp thing) (standard-char-p thing)))
154
155 (defun graphic-char-p (char)
156   #!+sb-doc
157   "The argument must be a character object. Graphic-char-p returns T if the
158   argument is a printing character (space through ~ in ASCII), otherwise
159   returns ()."
160   (declare (character char))
161   (and (typep char 'base-char)
162        (< 31
163           (char-code (the base-char char))
164           127)))
165
166 (defun alpha-char-p (char)
167   #!+sb-doc
168   "The argument must be a character object. Alpha-char-p returns T if the
169    argument is an alphabetic character, A-Z or a-z; otherwise ()."
170   (declare (character char))
171   (let ((m (char-code char)))
172     (or (< 64 m 91) (< 96 m 123))))
173
174 (defun upper-case-p (char)
175   #!+sb-doc
176   "The argument must be a character object; upper-case-p returns T if the
177    argument is an upper-case character, () otherwise."
178   (declare (character char))
179   (< 64
180      (char-code char)
181      91))
182
183 (defun lower-case-p (char)
184   #!+sb-doc
185   "The argument must be a character object; lower-case-p returns T if the
186    argument is a lower-case character, () otherwise."
187   (declare (character char))
188   (< 96
189      (char-code char)
190      123))
191
192 (defun both-case-p (char)
193   #!+sb-doc
194   "The argument must be a character object. Both-case-p returns T if the
195   argument is an alphabetic character and if the character exists in
196   both upper and lower case. For ASCII, this is the same as Alpha-char-p."
197   (declare (character char))
198   (let ((m (char-code char)))
199     (or (< 64 m 91) (< 96 m 123))))
200
201 (defun digit-char-p (char &optional (radix 10.))
202   #!+sb-doc
203   "If char is a digit in the specified radix, returns the fixnum for
204   which that digit stands, else returns NIL. Radix defaults to 10
205   (decimal)."
206   (declare (character char) (type (integer 2 36) radix))
207   (let ((m (- (char-code char) 48)))
208     (declare (fixnum m))
209     (cond ((<= radix 10.)
210            ;; Special-case decimal and smaller radices.
211            (if (and (>= m 0) (< m radix))  m  nil))
212           ;; Digits 0 - 9 are used as is, since radix is larger.
213           ((and (>= m 0) (< m 10)) m)
214           ;; Check for upper case A - Z.
215           ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
216           ;; Also check lower case a - z.
217           ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
218           ;; Else, fail.
219           (t nil))))
220
221 (defun alphanumericp (char)
222   #!+sb-doc
223   "Given a character-object argument, alphanumericp returns T if the
224    argument is either numeric or alphabetic."
225   (declare (character char))
226   (let ((m (char-code char)))
227     (or (< 47 m 58) (< 64 m 91) (< 96 m 123))))
228
229 (defun char= (character &rest more-characters)
230   #!+sb-doc
231   "Returns T if all of its arguments are the same character."
232   (do ((clist more-characters (cdr clist)))
233       ((atom clist) T)
234     (unless (eq (car clist) character) (return nil))))
235
236 (defun char/= (character &rest more-characters)
237   #!+sb-doc
238   "Returns T if no two of its arguments are the same character."
239   (do* ((head character (car list))
240         (list more-characters (cdr list)))
241        ((atom list) T)
242     (unless (do* ((l list (cdr l)))               ;inner loop returns T
243                  ((atom l) T)                        ; iff head /= rest.
244               (if (eq head (car l)) (return nil)))
245       (return nil))))
246
247 (defun char< (character &rest more-characters)
248   #!+sb-doc
249   "Returns T if its arguments are in strictly increasing alphabetic order."
250   (do* ((c character (car list))
251         (list more-characters (cdr list)))
252        ((atom list) T)
253     (unless (< (char-int c)
254                (char-int (car list)))
255       (return nil))))
256
257 (defun char> (character &rest more-characters)
258   #!+sb-doc
259   "Returns T if its arguments are in strictly decreasing alphabetic order."
260   (do* ((c character (car list))
261         (list more-characters (cdr list)))
262        ((atom list) T)
263     (unless (> (char-int c)
264                (char-int (car list)))
265       (return nil))))
266
267 (defun char<= (character &rest more-characters)
268   #!+sb-doc
269   "Returns T if its arguments are in strictly non-decreasing alphabetic order."
270   (do* ((c character (car list))
271         (list more-characters (cdr list)))
272        ((atom list) T)
273     (unless (<= (char-int c)
274                 (char-int (car list)))
275       (return nil))))
276
277 (defun char>= (character &rest more-characters)
278   #!+sb-doc
279   "Returns T if its arguments are in strictly non-increasing alphabetic order."
280   (do* ((c character (car list))
281         (list more-characters (cdr list)))
282        ((atom list) T)
283     (unless (>= (char-int c)
284                 (char-int (car list)))
285       (return nil))))
286
287 ;;; Equal-Char-Code is used by the following functions as a version of char-int
288 ;;;  which loses font, bits, and case info.
289
290 (defmacro equal-char-code (character)
291   `(let ((ch (char-code ,character)))
292      (if (< 96 ch 123) (- ch 32) ch)))
293
294 (defun char-equal (character &rest more-characters)
295   #!+sb-doc
296   "Returns T if all of its arguments are the same character.
297   Font, bits, and case are ignored."
298   (do ((clist more-characters (cdr clist)))
299       ((atom clist) T)
300     (unless (= (equal-char-code (car clist))
301                (equal-char-code character))
302       (return nil))))
303
304 (defun char-not-equal (character &rest more-characters)
305   #!+sb-doc
306   "Returns T if no two of its arguments are the same character.
307    Font, bits, and case are ignored."
308   (do* ((head character (car list))
309         (list more-characters (cdr list)))
310        ((atom list) T)
311     (unless (do* ((l list (cdr l)))
312                  ((atom l) T)
313               (if (= (equal-char-code head)
314                      (equal-char-code (car l)))
315                   (return nil)))
316       (return nil))))
317
318 (defun char-lessp (character &rest more-characters)
319   #!+sb-doc
320   "Returns T if its arguments are in strictly increasing alphabetic order.
321    Font, bits, and case are ignored."
322   (do* ((c character (car list))
323         (list more-characters (cdr list)))
324        ((atom list) T)
325     (unless (< (equal-char-code c)
326                (equal-char-code (car list)))
327       (return nil))))
328
329 (defun char-greaterp (character &rest more-characters)
330   #!+sb-doc
331   "Returns T if its arguments are in strictly decreasing alphabetic order.
332    Font, bits, and case are ignored."
333   (do* ((c character (car list))
334         (list more-characters (cdr list)))
335        ((atom list) T)
336     (unless (> (equal-char-code c)
337                (equal-char-code (car list)))
338       (return nil))))
339
340 (defun char-not-greaterp (character &rest more-characters)
341   #!+sb-doc
342   "Returns T if its arguments are in strictly non-decreasing alphabetic order.
343    Font, bits, and case are ignored."
344   (do* ((c character (car list))
345         (list more-characters (cdr list)))
346        ((atom list) T)
347     (unless (<= (equal-char-code c)
348                 (equal-char-code (car list)))
349       (return nil))))
350
351 (defun char-not-lessp (character &rest more-characters)
352   #!+sb-doc
353   "Returns T if its arguments are in strictly non-increasing alphabetic order.
354    Font, bits, and case are ignored."
355   (do* ((c character (car list))
356         (list more-characters (cdr list)))
357        ((atom list) T)
358     (unless (>= (equal-char-code c)
359                 (equal-char-code (car list)))
360       (return nil))))
361 \f
362 ;;;; miscellaneous functions
363
364 (defun char-upcase (char)
365   #!+sb-doc
366   "Returns CHAR converted to upper-case if that is possible."
367   (declare (character char))
368   (if (lower-case-p char)
369       (code-char (- (char-code char) 32))
370       char))
371
372 (defun char-downcase (char)
373   #!+sb-doc
374   "Returns CHAR converted to lower-case if that is possible."
375   (declare (character char))
376   (if (upper-case-p char)
377       (code-char (+ (char-code char) 32))
378       char))
379
380 (defun digit-char (weight &optional (radix 10))
381   #!+sb-doc
382   "All arguments must be integers. Returns a character object that
383   represents a digit of the given weight in the specified radix. Returns
384   NIL if no such character exists. The character will have the specified
385   font attributes."
386   (declare (type (integer 2 36) radix) (type unsigned-byte weight))
387   (and (typep weight 'fixnum)
388        (>= weight 0) (< weight radix) (< weight 36)
389        (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))