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 (macrolet ((frob (char-names-list)
37 (dolist (code char-names-list)
38 (destructuring-bind (ccode names) code
40 (results (cons name (code-char ccode))))))
41 `(defparameter *char-name-alist* ',(results)
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"))
53 (#x07 ("Bell" "^g" "Bel"))
54 (#x08 ("Backspace" "^h" "Bs"))
55 (#x09 ("Tab" "^i" "Ht"))
56 (#x0A ("Newline" "Linefeed" "^j" "Lf" "Nl" ))
58 (#x0C ("Page" "^l" "Form" "Formfeed" "Ff" "Np"))
59 (#x0D ("Return" "^m" "Cr"))
73 (#x1B ("Escape" "^[" "Altmode" "Esc" "Alt"))
79 (#x7f ("Rubout" "Delete" "Del")))))
81 ;;;; accessor functions
83 (defun char-code (char)
85 "Returns the integer code of CHAR."
87 (base-char (char-code (truly-the base-char char)))))
89 (defun char-int (char)
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."
95 (defun code-char (code)
97 "Returns the character with the code CODE."
98 (declare (type char-code code))
101 (defun character (object)
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
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)))
114 (string (if (= 1 (length (the string object)))
117 "String is not of length one: ~S" (list object))))
118 (symbol (if (= 1 (length (symbol-name object)))
119 (schar (symbol-name object) 0)
121 "Symbol name is not of length one: ~S" (list object))))
122 (t (do-error "~S cannot be coerced to a character." (list object))))))
124 (defun char-name (char)
126 "Given a character object, char-name returns the name for that
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 object whose name is that symbol, if one exists. Otherwise, () 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 (declare (character char))
144 (and (typep char 'base-char)
145 (let ((n (char-code (the base-char char))))
149 (defun %standard-char-p (thing)
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)))
155 (defun graphic-char-p (char)
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
160 (declare (character char))
161 (and (typep char 'base-char)
163 (char-code (the base-char char))
166 (defun alpha-char-p (char)
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))))
174 (defun upper-case-p (char)
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))
183 (defun lower-case-p (char)
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))
192 (defun both-case-p (char)
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))))
201 (defun digit-char-p (char &optional (radix 10.))
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
206 (declare (character char) (type (integer 2 36) radix))
207 (let ((m (- (char-code char) 48)))
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)
221 (defun alphanumericp (char)
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))))
229 (defun char= (character &rest more-characters)
231 "Returns T if all of its arguments are the same character."
232 (do ((clist more-characters (cdr clist)))
234 (unless (eq (car clist) character) (return nil))))
236 (defun char/= (character &rest more-characters)
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)))
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)))
247 (defun char< (character &rest more-characters)
249 "Returns T if its arguments are in strictly increasing alphabetic order."
250 (do* ((c character (car list))
251 (list more-characters (cdr list)))
253 (unless (< (char-int c)
254 (char-int (car list)))
257 (defun char> (character &rest more-characters)
259 "Returns T if its arguments are in strictly decreasing alphabetic order."
260 (do* ((c character (car list))
261 (list more-characters (cdr list)))
263 (unless (> (char-int c)
264 (char-int (car list)))
267 (defun char<= (character &rest more-characters)
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)))
273 (unless (<= (char-int c)
274 (char-int (car list)))
277 (defun char>= (character &rest more-characters)
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)))
283 (unless (>= (char-int c)
284 (char-int (car list)))
287 ;;; Equal-Char-Code is used by the following functions as a version of char-int
288 ;;; which loses font, bits, and case info.
290 (defmacro equal-char-code (character)
291 `(let ((ch (char-code ,character)))
292 (if (< 96 ch 123) (- ch 32) ch)))
294 (defun char-equal (character &rest more-characters)
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)))
300 (unless (= (equal-char-code (car clist))
301 (equal-char-code character))
304 (defun char-not-equal (character &rest more-characters)
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)))
311 (unless (do* ((l list (cdr l)))
313 (if (= (equal-char-code head)
314 (equal-char-code (car l)))
318 (defun char-lessp (character &rest more-characters)
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)))
325 (unless (< (equal-char-code c)
326 (equal-char-code (car list)))
329 (defun char-greaterp (character &rest more-characters)
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)))
336 (unless (> (equal-char-code c)
337 (equal-char-code (car list)))
340 (defun char-not-greaterp (character &rest more-characters)
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)))
347 (unless (<= (equal-char-code c)
348 (equal-char-code (car list)))
351 (defun char-not-lessp (character &rest more-characters)
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)))
358 (unless (>= (equal-char-code c)
359 (equal-char-code (car list)))
362 ;;;; miscellaneous functions
364 (defun char-upcase (char)
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))
372 (defun char-downcase (char)
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))
380 (defun digit-char (weight &optional (radix 10))
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
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)))))