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 whitespace-char-p (x)
223 (or (char= x #\space)
224 (char= x (code-char tab-char-code))
225 (char= x (code-char return-char-code))
226 (char= x #\linefeed))))
228 (defun alphanumericp (char)
230 "Given a character-object argument, alphanumericp returns T if the
231 argument is either numeric or alphabetic."
232 (declare (character char))
233 (let ((m (char-code char)))
234 (or (< 47 m 58) (< 64 m 91) (< 96 m 123))))
236 (defun char= (character &rest more-characters)
238 "Returns T if all of its arguments are the same character."
239 (do ((clist more-characters (cdr clist)))
241 (unless (eq (car clist) character) (return nil))))
243 (defun char/= (character &rest more-characters)
245 "Returns T if no two of its arguments are the same character."
246 (do* ((head character (car list))
247 (list more-characters (cdr list)))
249 (unless (do* ((l list (cdr l))) ;inner loop returns T
250 ((atom l) T) ; iff head /= rest.
251 (if (eq head (car l)) (return nil)))
254 (defun char< (character &rest more-characters)
256 "Returns T if its arguments are in strictly increasing alphabetic order."
257 (do* ((c character (car list))
258 (list more-characters (cdr list)))
260 (unless (< (char-int c)
261 (char-int (car list)))
264 (defun char> (character &rest more-characters)
266 "Returns T if its arguments are in strictly decreasing alphabetic order."
267 (do* ((c character (car list))
268 (list more-characters (cdr list)))
270 (unless (> (char-int c)
271 (char-int (car list)))
274 (defun char<= (character &rest more-characters)
276 "Returns T if its arguments are in strictly non-decreasing alphabetic order."
277 (do* ((c character (car list))
278 (list more-characters (cdr list)))
280 (unless (<= (char-int c)
281 (char-int (car list)))
284 (defun char>= (character &rest more-characters)
286 "Returns T if its arguments are in strictly non-increasing alphabetic order."
287 (do* ((c character (car list))
288 (list more-characters (cdr list)))
290 (unless (>= (char-int c)
291 (char-int (car list)))
294 ;;; Equal-Char-Code is used by the following functions as a version of char-int
295 ;;; which loses font, bits, and case info.
297 (defmacro equal-char-code (character)
298 `(let ((ch (char-code ,character)))
299 (if (< 96 ch 123) (- ch 32) ch)))
301 (defun char-equal (character &rest more-characters)
303 "Returns T if all of its arguments are the same character.
304 Font, bits, and case are ignored."
305 (do ((clist more-characters (cdr clist)))
307 (unless (= (equal-char-code (car clist))
308 (equal-char-code character))
311 (defun char-not-equal (character &rest more-characters)
313 "Returns T if no two of its arguments are the same character.
314 Font, bits, and case are ignored."
315 (do* ((head character (car list))
316 (list more-characters (cdr list)))
318 (unless (do* ((l list (cdr l)))
320 (if (= (equal-char-code head)
321 (equal-char-code (car l)))
325 (defun char-lessp (character &rest more-characters)
327 "Returns T if its arguments are in strictly increasing alphabetic order.
328 Font, bits, and case are ignored."
329 (do* ((c character (car list))
330 (list more-characters (cdr list)))
332 (unless (< (equal-char-code c)
333 (equal-char-code (car list)))
336 (defun char-greaterp (character &rest more-characters)
338 "Returns T if its arguments are in strictly decreasing alphabetic order.
339 Font, bits, and case are ignored."
340 (do* ((c character (car list))
341 (list more-characters (cdr list)))
343 (unless (> (equal-char-code c)
344 (equal-char-code (car list)))
347 (defun char-not-greaterp (character &rest more-characters)
349 "Returns T if its arguments are in strictly non-decreasing alphabetic order.
350 Font, bits, and case are ignored."
351 (do* ((c character (car list))
352 (list more-characters (cdr list)))
354 (unless (<= (equal-char-code c)
355 (equal-char-code (car list)))
358 (defun char-not-lessp (character &rest more-characters)
360 "Returns T if its arguments are in strictly non-increasing alphabetic order.
361 Font, bits, and case are ignored."
362 (do* ((c character (car list))
363 (list more-characters (cdr list)))
365 (unless (>= (equal-char-code c)
366 (equal-char-code (car list)))
369 ;;;; miscellaneous functions
371 (defun char-upcase (char)
373 "Returns CHAR converted to upper-case if that is possible."
374 (declare (character char))
375 (if (lower-case-p char)
376 (code-char (- (char-code char) 32))
379 (defun char-downcase (char)
381 "Returns CHAR converted to lower-case if that is possible."
382 (declare (character char))
383 (if (upper-case-p char)
384 (code-char (+ (char-code char) 32))
387 (defun digit-char (weight &optional (radix 10))
389 "All arguments must be integers. Returns a character object that
390 represents a digit of the given weight in the specified radix. Returns
391 NIL if no such character exists. The character will have the specified
393 (declare (type (integer 2 36) radix) (type unsigned-byte weight))
394 (and (typep weight 'fixnum)
395 (>= weight 0) (< weight radix) (< weight 36)
396 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))