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 (defvar *character-database*)
38 (with-open-file (stream (merge-pathnames
41 '(:relative :up :up "output")
44 sb!xc:*compile-file-truename*)
46 :element-type '(unsigned-byte 8))
47 (let* ((length (file-length stream))
48 (array (make-array length
49 :element-type '(unsigned-byte 8))))
50 (read-sequence array stream)
51 `(defun !character-database-cold-init ()
52 (setq *character-database* ',array))))))
54 #+sb-xc-host (!character-database-cold-init)
56 ;;; This is the alist of (character-name . character) for characters
57 ;;; with long names. The first name in this list for a given character
58 ;;; is used on typeout and is the preferred form for input.
59 (macrolet ((frob (char-names-list)
61 (dolist (code char-names-list)
62 (destructuring-bind (ccode names) code
64 (results (cons name ccode)))))
65 `(defparameter *char-name-alist*
66 (mapcar (lambda (x) (cons (car x) (code-char (cdr x))))
68 ;; Note: The *** markers here indicate character names which are
69 ;; required by the ANSI specification of #'CHAR-NAME. For the others,
70 ;; we prefer the ASCII standard name.
71 (frob ((#x00 ("Nul" "Null" "^@"))
78 (#x07 ("Bel" "Bell" "^g"))
79 (#x08 ("Backspace" "^h" "Bs")) ; *** See Note above.
80 (#x09 ("Tab" "^i" "Ht")) ; *** See Note above.
81 (#x0A ("Newline" "Linefeed" "^j" "Lf" "Nl" )) ; *** See Note above.
83 (#x0C ("Page" "^l" "Form" "Formfeed" "Ff" "Np")) ; *** See Note above.
84 (#x0D ("Return" "^m" "Cr")) ; *** See Note above.
98 (#x1B ("Esc" "Escape" "^[" "Altmode" "Alt"))
103 (#x20 ("Space" "Sp")) ; *** See Note above.
104 (#x7f ("Rubout" "Delete" "Del"))
107 (#x82 ("Break-Permitted"))
108 (#x83 ("No-Break-Permitted"))
111 (#x86 ("Start-Selected-Area"))
112 (#x87 ("End-Selected-Area"))
113 (#x88 ("Character-Tabulation-Set"))
114 (#x89 ("Character-Tabulation-With-Justification"))
115 (#x8A ("Line-Tabulation-Set"))
116 (#x8B ("Partial-Line-Forward"))
117 (#x8C ("Partial-Line-Backward"))
118 (#x8D ("Reverse-Linefeed"))
119 (#x8E ("Single-Shift-Two"))
120 (#x8F ("Single-Shift-Three"))
121 (#x90 ("Device-Control-String"))
122 (#x91 ("Private-Use-One"))
123 (#x92 ("Private-Use-Two"))
124 (#x93 ("Set-Transmit-State"))
125 (#x94 ("Cancel-Character"))
126 (#x95 ("Message-Waiting"))
127 (#x96 ("Start-Guarded-Area"))
128 (#x97 ("End-Guarded-Area"))
129 (#x98 ("Start-String"))
131 (#x9A ("Single-Character-Introducer"))
132 (#x9B ("Control-Sequence-Introducer"))
133 (#x9C ("String-Terminator"))
134 (#x9D ("Operating-System-Command"))
135 (#x9E ("Privacy-Message"))
136 (#x9F ("Application-Program-Command"))))) ; *** See Note above.
138 ;;;; accessor functions
141 ;; (+ 1488 (ash #x110000 -8)) => 5840
142 (defun ucd-index (char)
143 (let* ((cp (char-code char))
144 (cp-high (ash cp -8))
145 (page (aref *character-database* (+ 1488 cp-high))))
146 (+ 5840 (ash page 10) (ash (ldb (byte 8 0) cp) 2))))
148 (defun ucd-value-0 (char)
149 (aref *character-database* (ucd-index char)))
151 (defun ucd-value-1 (char)
152 (let ((index (ucd-index char)))
153 (dpb (aref *character-database* (+ index 3))
155 (dpb (aref *character-database* (+ index 2))
157 (aref *character-database* (1+ index))))))
159 (defun ucd-general-category (char)
160 (aref *character-database* (* 8 (ucd-value-0 char))))
162 (defun ucd-decimal-digit (char)
163 (let ((decimal-digit (aref *character-database*
164 (+ 3 (* 8 (ucd-value-0 char))))))
165 (when (< decimal-digit 10)
168 (defun char-code (char)
170 "Return the integer code of CHAR."
171 ;; FIXME: do we actually need this?
173 (character (char-code (truly-the character char)))))
175 (defun char-int (char)
177 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
178 there are no character bits or fonts.)"
181 (defun code-char (code)
183 "Return the character with the code CODE."
186 (defun character (object)
188 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are
189 characters, strings and symbols of length 1."
190 (flet ((do-error (control args)
191 (error 'simple-type-error
193 ;;?? how to express "symbol with name of length 1"?
194 :expected-type '(or character (string 1))
195 :format-control control
196 :format-arguments args)))
199 (string (if (= 1 (length (the string object)))
202 "String is not of length one: ~S" (list object))))
203 (symbol (if (= 1 (length (symbol-name object)))
204 (schar (symbol-name object) 0)
206 "Symbol name is not of length one: ~S" (list object))))
207 (t (do-error "~S cannot be coerced to a character." (list object))))))
209 (defun char-name (char)
211 "Return the name (a STRING) for a CHARACTER object."
212 (car (rassoc char *char-name-alist*)))
214 (defun name-char (name)
216 "Given an argument acceptable to STRING, NAME-CHAR returns a character
217 whose name is that string, if one exists. Otherwise, NIL is returned."
218 (cdr (assoc (string name) *char-name-alist* :test #'string-equal)))
222 (defun standard-char-p (char)
224 "The argument must be a character object. STANDARD-CHAR-P returns T if the
225 argument is a standard character -- one of the 95 ASCII printing characters
227 (and (typep char 'base-char)
228 (let ((n (char-code (the base-char char))))
232 (defun %standard-char-p (thing)
234 "Return T if and only if THING is a standard-char. Differs from
235 STANDARD-CHAR-P in that THING doesn't have to be a character."
236 (and (characterp thing) (standard-char-p thing)))
238 (defun graphic-char-p (char)
240 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
241 argument is a printing character (space through ~ in ASCII), otherwise
243 (let ((n (char-code char)))
247 (defun alpha-char-p (char)
249 "The argument must be a character object. ALPHA-CHAR-P returns T if the
250 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
251 (< (ucd-general-category char) 5))
253 (defun upper-case-p (char)
255 "The argument must be a character object; UPPER-CASE-P returns T if the
256 argument is an upper-case character, NIL otherwise."
257 (= (ucd-value-0 char) 0))
259 (defun lower-case-p (char)
261 "The argument must be a character object; LOWER-CASE-P returns T if the
262 argument is a lower-case character, NIL otherwise."
263 (= (ucd-value-0 char) 1))
265 (defun both-case-p (char)
267 "The argument must be a character object. BOTH-CASE-P returns T if the
268 argument is an alphabetic character and if the character exists in
269 both upper and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
270 (< (ucd-value-0 char) 2))
272 (defun digit-char-p (char &optional (radix 10.))
274 "If char is a digit in the specified radix, returns the fixnum for
275 which that digit stands, else returns NIL."
276 (let ((m (- (char-code char) 48)))
278 (cond ((<= radix 10.)
279 ;; Special-case decimal and smaller radices.
280 (if (and (>= m 0) (< m radix)) m nil))
281 ;; Digits 0 - 9 are used as is, since radix is larger.
282 ((and (>= m 0) (< m 10)) m)
283 ;; Check for upper case A - Z.
284 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
285 ;; Also check lower case a - z.
286 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
288 (t (let ((number (ucd-decimal-digit char)))
289 (when (and number (< number radix))
292 (defun alphanumericp (char)
294 "Given a character-object argument, ALPHANUMERICP returns T if the
295 argument is either numeric or alphabetic."
296 (let ((gc (ucd-general-category char)))
300 (defun char= (character &rest more-characters)
302 "Return T if all of the arguments are the same character."
303 (dolist (c more-characters t)
304 (declare (type character c))
305 (unless (eq c character) (return nil))))
307 (defun char/= (character &rest more-characters)
309 "Return T if no two of the arguments are the same character."
310 (do* ((head character (car list))
311 (list more-characters (cdr list)))
313 (declare (type character head))
315 (declare (type character c))
316 (when (eq head c) (return-from char/= nil)))))
318 (defun char< (character &rest more-characters)
320 "Return T if the arguments are in strictly increasing alphabetic order."
321 (do* ((c character (car list))
322 (list more-characters (cdr list)))
324 (unless (< (char-int c)
325 (char-int (car list)))
328 (defun char> (character &rest more-characters)
330 "Return T if the arguments are in strictly decreasing alphabetic order."
331 (do* ((c character (car list))
332 (list more-characters (cdr list)))
334 (unless (> (char-int c)
335 (char-int (car list)))
338 (defun char<= (character &rest more-characters)
340 "Return T if the arguments are in strictly non-decreasing alphabetic order."
341 (do* ((c character (car list))
342 (list more-characters (cdr list)))
344 (unless (<= (char-int c)
345 (char-int (car list)))
348 (defun char>= (character &rest more-characters)
350 "Return T if the arguments are in strictly non-increasing alphabetic order."
351 (do* ((c character (car list))
352 (list more-characters (cdr list)))
354 (unless (>= (char-int c)
355 (char-int (car list)))
358 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
359 ;;; which loses font, bits, and case info.
361 (defmacro equal-char-code (character)
363 `(let ((,ch ,character))
364 (if (= (ucd-value-0 ,ch) 0)
368 (defun char-equal (character &rest more-characters)
370 "Return T if all of the arguments are the same character.
371 Font, bits, and case are ignored."
372 (do ((clist more-characters (cdr clist)))
374 (unless (= (equal-char-code (car clist))
375 (equal-char-code character))
378 (defun char-not-equal (character &rest more-characters)
380 "Return T if no two of the arguments are the same character.
381 Font, bits, and case are ignored."
382 (do* ((head character (car list))
383 (list more-characters (cdr list)))
385 (unless (do* ((l list (cdr l)))
387 (if (= (equal-char-code head)
388 (equal-char-code (car l)))
392 (defun char-lessp (character &rest more-characters)
394 "Return T if the arguments are in strictly increasing alphabetic order.
395 Font, bits, and case are ignored."
396 (do* ((c character (car list))
397 (list more-characters (cdr list)))
399 (unless (< (equal-char-code c)
400 (equal-char-code (car list)))
403 (defun char-greaterp (character &rest more-characters)
405 "Return T if the arguments are in strictly decreasing alphabetic order.
406 Font, bits, and case are ignored."
407 (do* ((c character (car list))
408 (list more-characters (cdr list)))
410 (unless (> (equal-char-code c)
411 (equal-char-code (car list)))
414 (defun char-not-greaterp (character &rest more-characters)
416 "Return T if the arguments are in strictly non-decreasing alphabetic order.
417 Font, bits, and case are ignored."
418 (do* ((c character (car list))
419 (list more-characters (cdr list)))
421 (unless (<= (equal-char-code c)
422 (equal-char-code (car list)))
425 (defun char-not-lessp (character &rest more-characters)
427 "Return T if the arguments are in strictly non-increasing alphabetic order.
428 Font, bits, and case are ignored."
429 (do* ((c character (car list))
430 (list more-characters (cdr list)))
432 (unless (>= (equal-char-code c)
433 (equal-char-code (car list)))
436 ;;;; miscellaneous functions
438 (defun char-upcase (char)
440 "Return CHAR converted to upper-case if that is possible. Don't convert
441 lowercase eszet (U+DF)."
442 (if (= (ucd-value-0 char) 1)
443 (code-char (ucd-value-1 char))
446 (defun char-downcase (char)
448 "Return CHAR converted to lower-case if that is possible."
449 (if (= (ucd-value-0 char) 0)
450 (code-char (ucd-value-1 char))
453 (defun digit-char (weight &optional (radix 10))
455 "All arguments must be integers. Returns a character object that
456 represents a digit of the given weight in the specified radix. Returns
457 NIL if no such character exists."
458 (and (typep weight 'fixnum)
459 (>= weight 0) (< weight radix) (< weight 36)
460 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))