1 ;;;; character functions
3 ;;;; This file assumes the use of ASCII codes and the specific
4 ;;;; character formats used in SBCL (and its ancestor, CMU CL). It is
5 ;;;; optimized for performance rather than for portability and
6 ;;;; elegance, and may have to be rewritten if the character
7 ;;;; representation is changed.
9 ;;;; FIXME: should perhaps be renamed ascii.lisp since it's an
10 ;;;; unportable ASCII-dependent implementation
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
15 ;;;; This software is derived from the CMU CL system, which was
16 ;;;; written at Carnegie Mellon University and released into the
17 ;;;; public domain. The software is in the public domain and is
18 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
19 ;;;; files for more information.
21 (in-package "SB!IMPL")
26 ;;; We compile some trivial character operations via inline expansion.
28 (declaim (inline standard-char-p graphic-char-p alpha-char-p
29 upper-case-p lower-case-p both-case-p alphanumericp
31 (declaim (maybe-inline digit-char-p digit-weight))
33 (defconstant char-code-limit 256
35 "the upper exclusive bound on values produced by CHAR-CODE")
38 `(integer 0 (,char-code-limit)))
40 (macrolet ((frob (char-names-list)
42 (dolist (code char-names-list)
43 (destructuring-bind (ccode names) code
45 (results (cons name (code-char ccode))))))
46 `(defparameter *char-name-alist* ',(results)
48 "This is the alist of (character-name . character) for characters with
49 long names. The first name in this list for a given character is used
50 on typeout and is the preferred form for input."))))
51 (frob ((#x00 ("Null" "^@" "Nul"))
58 (#x07 ("Bell" "^g" "Bel"))
59 (#x08 ("Backspace" "^h" "Bs"))
60 (#x09 ("Tab" "^i" "Ht"))
61 (#x0A ("Newline" "Linefeed" "^j" "Lf" "Nl" ))
63 (#x0C ("Page" "^l" "Form" "Formfeed" "Ff" "Np"))
64 (#x0D ("Return" "^m" "Cr"))
78 (#x1B ("Escape" "^[" "Altmode" "Esc" "Alt"))
84 (#x7f ("Rubout" "Delete" "Del")))))
86 ;;;; accessor functions
88 (defun char-code (char)
90 "Returns the integer code of CHAR."
92 (base-char (char-code (truly-the base-char char)))))
94 (defun char-int (char)
96 "Returns the integer code of CHAR. This is the same as char-code, as
97 CMU Common Lisp does not implement character bits or fonts."
100 (defun code-char (code)
102 "Returns the character with the code CODE."
103 (declare (type char-code code))
106 (defun character (object)
108 "Coerces its argument into a character object if possible. Accepts
109 characters, strings and symbols of length 1."
110 (flet ((do-error (control args)
111 (error 'simple-type-error
113 ;;?? how to express "symbol with name of length 1"?
114 :expected-type '(or character (string 1))
115 :format-control control
116 :format-arguments args)))
119 (string (if (= 1 (length (the string object)))
122 "String is not of length one: ~S" (list object))))
123 (symbol (if (= 1 (length (symbol-name object)))
124 (schar (symbol-name object) 0)
126 "Symbol name is not of length one: ~S" (list object))))
127 (t (do-error "~S cannot be coerced to a character." (list object))))))
129 (defun char-name (char)
131 "Given a character object, char-name returns the name for that
133 (car (rassoc char *char-name-alist*)))
135 (defun name-char (name)
137 "Given an argument acceptable to string, name-char returns a character
138 object whose name is that symbol, if one exists. Otherwise, () is returned."
139 (cdr (assoc (string name) *char-name-alist* :test #'string-equal)))
143 (defun standard-char-p (char)
145 "The argument must be a character object. Standard-char-p returns T if the
146 argument is a standard character -- one of the 95 ASCII printing characters
148 (declare (character char))
149 (and (typep char 'base-char)
150 (let ((n (char-code (the base-char char))))
154 (defun %standard-char-p (thing)
156 "Return T if and only if THING is a standard-char. Differs from
157 standard-char-p in that THING doesn't have to be a character."
158 (and (characterp thing) (standard-char-p thing)))
160 (defun graphic-char-p (char)
162 "The argument must be a character object. Graphic-char-p returns T if the
163 argument is a printing character (space through ~ in ASCII), otherwise
165 (declare (character char))
166 (and (typep char 'base-char)
168 (char-code (the base-char char))
171 (defun alpha-char-p (char)
173 "The argument must be a character object. Alpha-char-p returns T if the
174 argument is an alphabetic character, A-Z or a-z; otherwise ()."
175 (declare (character char))
176 (let ((m (char-code char)))
177 (or (< 64 m 91) (< 96 m 123))))
179 (defun upper-case-p (char)
181 "The argument must be a character object; upper-case-p returns T if the
182 argument is an upper-case character, () otherwise."
183 (declare (character char))
188 (defun lower-case-p (char)
190 "The argument must be a character object; lower-case-p returns T if the
191 argument is a lower-case character, () otherwise."
192 (declare (character char))
197 (defun both-case-p (char)
199 "The argument must be a character object. Both-case-p returns T if the
200 argument is an alphabetic character and if the character exists in
201 both upper and lower case. For ASCII, this is the same as Alpha-char-p."
202 (declare (character char))
203 (let ((m (char-code char)))
204 (or (< 64 m 91) (< 96 m 123))))
206 (defun digit-char-p (char &optional (radix 10.))
208 "If char is a digit in the specified radix, returns the fixnum for
209 which that digit stands, else returns NIL. Radix defaults to 10
211 (declare (character char) (type (integer 2 36) radix))
212 (let ((m (- (char-code char) 48)))
214 (cond ((<= radix 10.)
215 ;; Special-case decimal and smaller radices.
216 (if (and (>= m 0) (< m radix)) m nil))
217 ;; Digits 0 - 9 are used as is, since radix is larger.
218 ((and (>= m 0) (< m 10)) m)
219 ;; Check for upper case A - Z.
220 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
221 ;; Also check lower case a - z.
222 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
226 (defun alphanumericp (char)
228 "Given a character-object argument, alphanumericp returns T if the
229 argument is either numeric or alphabetic."
230 (declare (character char))
231 (let ((m (char-code char)))
232 (or (< 47 m 58) (< 64 m 91) (< 96 m 123))))
234 (defun char= (character &rest more-characters)
236 "Returns T if all of its arguments are the same character."
237 (do ((clist more-characters (cdr clist)))
239 (unless (eq (car clist) character) (return nil))))
241 (defun char/= (character &rest more-characters)
243 "Returns T if no two of its arguments are the same character."
244 (do* ((head character (car list))
245 (list more-characters (cdr list)))
247 (unless (do* ((l list (cdr l))) ;inner loop returns T
248 ((atom l) T) ; iff head /= rest.
249 (if (eq head (car l)) (return nil)))
252 (defun char< (character &rest more-characters)
254 "Returns T if its arguments are in strictly increasing alphabetic order."
255 (do* ((c character (car list))
256 (list more-characters (cdr list)))
258 (unless (< (char-int c)
259 (char-int (car list)))
262 (defun char> (character &rest more-characters)
264 "Returns T if its arguments are in strictly decreasing alphabetic order."
265 (do* ((c character (car list))
266 (list more-characters (cdr list)))
268 (unless (> (char-int c)
269 (char-int (car list)))
272 (defun char<= (character &rest more-characters)
274 "Returns T if its arguments are in strictly non-decreasing alphabetic order."
275 (do* ((c character (car list))
276 (list more-characters (cdr list)))
278 (unless (<= (char-int c)
279 (char-int (car list)))
282 (defun char>= (character &rest more-characters)
284 "Returns T if its arguments are in strictly non-increasing alphabetic order."
285 (do* ((c character (car list))
286 (list more-characters (cdr list)))
288 (unless (>= (char-int c)
289 (char-int (car list)))
292 ;;; Equal-Char-Code is used by the following functions as a version of char-int
293 ;;; which loses font, bits, and case info.
295 (defmacro equal-char-code (character)
296 `(let ((ch (char-code ,character)))
297 (if (< 96 ch 123) (- ch 32) ch)))
299 (defun char-equal (character &rest more-characters)
301 "Returns T if all of its arguments are the same character.
302 Font, bits, and case are ignored."
303 (do ((clist more-characters (cdr clist)))
305 (unless (= (equal-char-code (car clist))
306 (equal-char-code character))
309 (defun char-not-equal (character &rest more-characters)
311 "Returns T if no two of its arguments are the same character.
312 Font, bits, and case are ignored."
313 (do* ((head character (car list))
314 (list more-characters (cdr list)))
316 (unless (do* ((l list (cdr l)))
318 (if (= (equal-char-code head)
319 (equal-char-code (car l)))
323 (defun char-lessp (character &rest more-characters)
325 "Returns T if its arguments are in strictly increasing alphabetic order.
326 Font, bits, and case are ignored."
327 (do* ((c character (car list))
328 (list more-characters (cdr list)))
330 (unless (< (equal-char-code c)
331 (equal-char-code (car list)))
334 (defun char-greaterp (character &rest more-characters)
336 "Returns T if its arguments are in strictly decreasing alphabetic order.
337 Font, bits, and case are ignored."
338 (do* ((c character (car list))
339 (list more-characters (cdr list)))
341 (unless (> (equal-char-code c)
342 (equal-char-code (car list)))
345 (defun char-not-greaterp (character &rest more-characters)
347 "Returns T if its arguments are in strictly non-decreasing alphabetic order.
348 Font, bits, and case are ignored."
349 (do* ((c character (car list))
350 (list more-characters (cdr list)))
352 (unless (<= (equal-char-code c)
353 (equal-char-code (car list)))
356 (defun char-not-lessp (character &rest more-characters)
358 "Returns T if its arguments are in strictly non-increasing alphabetic order.
359 Font, bits, and case are ignored."
360 (do* ((c character (car list))
361 (list more-characters (cdr list)))
363 (unless (>= (equal-char-code c)
364 (equal-char-code (car list)))
367 ;;;; miscellaneous functions
369 (defun char-upcase (char)
371 "Returns CHAR converted to upper-case if that is possible."
372 (declare (character char))
373 (if (lower-case-p char)
374 (code-char (- (char-code char) 32))
377 (defun char-downcase (char)
379 "Returns CHAR converted to lower-case if that is possible."
380 (declare (character char))
381 (if (upper-case-p char)
382 (code-char (+ (char-code char) 32))
385 (defun digit-char (weight &optional (radix 10))
387 "All arguments must be integers. Returns a character object that
388 represents a digit of the given weight in the specified radix. Returns
389 NIL if no such character exists. The character will have the specified
391 (declare (type (integer 2 36) radix) (type unsigned-byte weight))
392 (and (typep weight 'fixnum)
393 (>= weight 0) (< weight radix) (< weight 36)
394 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))