1 ;;;; character functions
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.
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
14 ;;;; This software is part of the SBCL system. See the README file for
15 ;;;; more information.
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.
23 (in-package "SB!IMPL")
25 ;;; We compile some trivial character operations via inline expansion.
27 (declaim (inline standard-char-p graphic-char-p alpha-char-p
28 upper-case-p lower-case-p both-case-p alphanumericp
30 (declaim (maybe-inline digit-char-p digit-weight))
33 `(integer 0 (,char-code-limit)))
35 ;;; This is the alist of (character-name . character) for characters
36 ;;; with long names. The first name in this list for a given character
37 ;;; is used on typeout and is the preferred form for input.
38 (macrolet ((frob (char-names-list)
40 (dolist (code char-names-list)
41 (destructuring-bind (ccode names) code
43 (results (cons name (code-char ccode))))))
44 `(defparameter *char-name-alist* ',(results)))))
45 ;; Note: The *** markers here indicate character names which are
46 ;; required by the ANSI specification of #'CHAR-NAME. For the others,
47 ;; we prefer the ASCII standard name.
48 (frob ((#x00 ("Nul" "Null" "^@"))
55 (#x07 ("Bel" "Bell" "^g"))
56 (#x08 ("Backspace" "^h" "Bs")) ; *** See Note above.
57 (#x09 ("Tab" "^i" "Ht")) ; *** See Note above.
58 (#x0A ("Newline" "Linefeed" "^j" "Lf" "Nl" )) ; *** See Note above.
60 (#x0C ("Page" "^l" "Form" "Formfeed" "Ff" "Np")) ; *** See Note above.
61 (#x0D ("Return" "^m" "Cr")) ; *** See Note above.
75 (#x1B ("Esc" "Escape" "^[" "Altmode" "Alt"))
80 (#x20 ("Space" "Sp")) ; *** See Note above.
81 (#x7f ("Rubout" "Delete" "Del"))))) ; *** See Note above.
83 ;;;; accessor functions
85 (defun char-code (char)
87 "Return the integer code of CHAR."
89 (base-char (char-code (truly-the base-char char)))))
91 (defun char-int (char)
93 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
94 there are no character bits or fonts.)"
97 (defun code-char (code)
99 "Return the character with the code CODE."
102 (defun character (object)
104 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are
105 characters, strings and symbols of length 1."
106 (flet ((do-error (control args)
107 (error 'simple-type-error
109 ;;?? how to express "symbol with name of length 1"?
110 :expected-type '(or character (string 1))
111 :format-control control
112 :format-arguments args)))
115 (string (if (= 1 (length (the string object)))
118 "String is not of length one: ~S" (list object))))
119 (symbol (if (= 1 (length (symbol-name object)))
120 (schar (symbol-name object) 0)
122 "Symbol name is not of length one: ~S" (list object))))
123 (t (do-error "~S cannot be coerced to a character." (list object))))))
125 (defun char-name (char)
127 "Return the name (a STRING) for a CHARACTER object."
128 (car (rassoc char *char-name-alist*)))
130 (defun name-char (name)
132 "Given an argument acceptable to STRING, NAME-CHAR returns a character
133 whose name is that string, if one exists. Otherwise, NIL is returned."
134 (cdr (assoc (string name) *char-name-alist* :test #'string-equal)))
138 (defun standard-char-p (char)
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
143 (and (typep char 'base-char)
144 (let ((n (char-code (the base-char char))))
148 (defun %standard-char-p (thing)
150 "Return T if and only if THING is a standard-char. Differs from
151 STANDARD-CHAR-P in that THING doesn't have to be a character."
152 (and (characterp thing) (standard-char-p thing)))
154 (defun graphic-char-p (char)
156 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
157 argument is a printing character (space through ~ in ASCII), otherwise
159 (and (typep char 'base-char)
161 (char-code (the base-char char))
164 (defun alpha-char-p (char)
166 "The argument must be a character object. ALPHA-CHAR-P returns T if the
167 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
168 (let ((m (char-code char)))
169 (or (< 64 m 91) (< 96 m 123))))
171 (defun upper-case-p (char)
173 "The argument must be a character object; UPPER-CASE-P returns T if the
174 argument is an upper-case character, NIL otherwise."
179 (defun lower-case-p (char)
181 "The argument must be a character object; LOWER-CASE-P returns T if the
182 argument is a lower-case character, NIL otherwise."
187 (defun both-case-p (char)
189 "The argument must be a character object. BOTH-CASE-P returns T if the
190 argument is an alphabetic character and if the character exists in
191 both upper and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
192 (let ((m (char-code char)))
193 (or (< 64 m 91) (< 96 m 123))))
195 (defun digit-char-p (char &optional (radix 10.))
197 "If char is a digit in the specified radix, returns the fixnum for
198 which that digit stands, else returns NIL."
199 (let ((m (- (char-code char) 48)))
201 (cond ((<= radix 10.)
202 ;; Special-case decimal and smaller radices.
203 (if (and (>= m 0) (< m radix)) m nil))
204 ;; Digits 0 - 9 are used as is, since radix is larger.
205 ((and (>= m 0) (< m 10)) m)
206 ;; Check for upper case A - Z.
207 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
208 ;; Also check lower case a - z.
209 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
213 (defun whitespace-char-p (x)
215 (or (char= x #\space)
216 (char= x (code-char tab-char-code))
217 (char= x (code-char return-char-code))
218 (char= x #\linefeed))))
220 (defun alphanumericp (char)
222 "Given a character-object argument, ALPHANUMERICP returns T if the
223 argument is either numeric or alphabetic."
224 (let ((m (char-code char)))
225 (or (< 47 m 58) (< 64 m 91) (< 96 m 123))))
227 (defun char= (character &rest more-characters)
229 "Return T if all of the arguments are the same character."
230 (dolist (c more-characters t)
231 (declare (type character c))
232 (unless (eq c character) (return nil))))
234 (defun char/= (character &rest more-characters)
236 "Return T if no two of the arguments are the same character."
237 (do* ((head character (car list))
238 (list more-characters (cdr list)))
240 (declare (type character head))
242 (declare (type character c))
243 (when (eq head c) (return-from char/= nil)))))
245 (defun char< (character &rest more-characters)
247 "Return T if the arguments are in strictly increasing alphabetic order."
248 (do* ((c character (car list))
249 (list more-characters (cdr list)))
251 (unless (< (char-int c)
252 (char-int (car list)))
255 (defun char> (character &rest more-characters)
257 "Return T if the arguments are in strictly decreasing alphabetic order."
258 (do* ((c character (car list))
259 (list more-characters (cdr list)))
261 (unless (> (char-int c)
262 (char-int (car list)))
265 (defun char<= (character &rest more-characters)
267 "Return T if the arguments are in strictly non-decreasing alphabetic order."
268 (do* ((c character (car list))
269 (list more-characters (cdr list)))
271 (unless (<= (char-int c)
272 (char-int (car list)))
275 (defun char>= (character &rest more-characters)
277 "Return T if the arguments are in strictly non-increasing alphabetic order."
278 (do* ((c character (car list))
279 (list more-characters (cdr list)))
281 (unless (>= (char-int c)
282 (char-int (car list)))
285 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
286 ;;; which loses font, bits, and case info.
288 (defmacro equal-char-code (character)
289 `(let ((ch (char-code ,character)))
290 (if (< 96 ch 123) (- ch 32) ch)))
292 (defun char-equal (character &rest more-characters)
294 "Return T if all of the arguments are the same character.
295 Font, bits, and case are ignored."
296 (do ((clist more-characters (cdr clist)))
298 (unless (= (equal-char-code (car clist))
299 (equal-char-code character))
302 (defun char-not-equal (character &rest more-characters)
304 "Return T if no two of the arguments are the same character.
305 Font, bits, and case are ignored."
306 (do* ((head character (car list))
307 (list more-characters (cdr list)))
309 (unless (do* ((l list (cdr l)))
311 (if (= (equal-char-code head)
312 (equal-char-code (car l)))
316 (defun char-lessp (character &rest more-characters)
318 "Return T if the arguments are in strictly increasing alphabetic order.
319 Font, bits, and case are ignored."
320 (do* ((c character (car list))
321 (list more-characters (cdr list)))
323 (unless (< (equal-char-code c)
324 (equal-char-code (car list)))
327 (defun char-greaterp (character &rest more-characters)
329 "Return T if the arguments are in strictly decreasing alphabetic order.
330 Font, bits, and case are ignored."
331 (do* ((c character (car list))
332 (list more-characters (cdr list)))
334 (unless (> (equal-char-code c)
335 (equal-char-code (car list)))
338 (defun char-not-greaterp (character &rest more-characters)
340 "Return T if the arguments are in strictly non-decreasing alphabetic order.
341 Font, bits, and case are ignored."
342 (do* ((c character (car list))
343 (list more-characters (cdr list)))
345 (unless (<= (equal-char-code c)
346 (equal-char-code (car list)))
349 (defun char-not-lessp (character &rest more-characters)
351 "Return T if the arguments are in strictly non-increasing alphabetic order.
352 Font, bits, and case are ignored."
353 (do* ((c character (car list))
354 (list more-characters (cdr list)))
356 (unless (>= (equal-char-code c)
357 (equal-char-code (car list)))
360 ;;;; miscellaneous functions
362 (defun char-upcase (char)
364 "Return CHAR converted to upper-case if that is possible."
365 (if (lower-case-p char)
366 (code-char (- (char-code char) 32))
369 (defun char-downcase (char)
371 "Return CHAR converted to lower-case if that is possible."
372 (if (upper-case-p char)
373 (code-char (+ (char-code char) 32))
376 (defun digit-char (weight &optional (radix 10))
378 "All arguments must be integers. Returns a character object that
379 represents a digit of the given weight in the specified radix. Returns
380 NIL if no such character exists."
381 (and (typep weight 'fixnum)
382 (>= weight 0) (< weight radix) (< weight 36)
383 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))