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*)
36 (declaim (type (simple-array (unsigned-byte 8) (*)) *character-database*))
39 (with-open-file (stream (merge-pathnames
42 '(:relative :up :up "output")
45 sb!xc:*compile-file-truename*)
47 :element-type '(unsigned-byte 8))
48 (let* ((length (file-length stream))
49 (array (make-array length
50 :element-type '(unsigned-byte 8))))
51 (read-sequence array stream)
52 `(defun !character-database-cold-init ()
53 (setq *character-database* ',array))))))
55 #+sb-xc-host (!character-database-cold-init)
57 ;;; This is the alist of (character-name . character) for characters
58 ;;; with long names. The first name in this list for a given character
59 ;;; is used on typeout and is the preferred form for input.
60 (macrolet ((frob (char-names-list)
62 (dolist (code char-names-list)
63 (destructuring-bind (ccode names) code
65 (results (cons name ccode)))))
66 `(defparameter *char-name-alist*
67 (mapcar (lambda (x) (cons (car x) (code-char (cdr x))))
69 ;; Note: The *** markers here indicate character names which are
70 ;; required by the ANSI specification of #'CHAR-NAME. For the others,
71 ;; we prefer the ASCII standard name.
72 (frob ((#x00 ("Nul" "Null" "^@"))
79 (#x07 ("Bel" "Bell" "^g"))
80 (#x08 ("Backspace" "^h" "Bs")) ; *** See Note above.
81 (#x09 ("Tab" "^i" "Ht")) ; *** See Note above.
82 (#x0A ("Newline" "Linefeed" "^j" "Lf" "Nl" )) ; *** See Note above.
84 (#x0C ("Page" "^l" "Form" "Formfeed" "Ff" "Np")) ; *** See Note above.
85 (#x0D ("Return" "^m" "Cr")) ; *** See Note above.
99 (#x1B ("Esc" "Escape" "^[" "Altmode" "Alt"))
104 (#x20 ("Space" "Sp")) ; *** See Note above.
105 (#x7f ("Rubout" "Delete" "Del"))
108 (#x82 ("Break-Permitted"))
109 (#x83 ("No-Break-Permitted"))
112 (#x86 ("Start-Selected-Area"))
113 (#x87 ("End-Selected-Area"))
114 (#x88 ("Character-Tabulation-Set"))
115 (#x89 ("Character-Tabulation-With-Justification"))
116 (#x8A ("Line-Tabulation-Set"))
117 (#x8B ("Partial-Line-Forward"))
118 (#x8C ("Partial-Line-Backward"))
119 (#x8D ("Reverse-Linefeed"))
120 (#x8E ("Single-Shift-Two"))
121 (#x8F ("Single-Shift-Three"))
122 (#x90 ("Device-Control-String"))
123 (#x91 ("Private-Use-One"))
124 (#x92 ("Private-Use-Two"))
125 (#x93 ("Set-Transmit-State"))
126 (#x94 ("Cancel-Character"))
127 (#x95 ("Message-Waiting"))
128 (#x96 ("Start-Guarded-Area"))
129 (#x97 ("End-Guarded-Area"))
130 (#x98 ("Start-String"))
132 (#x9A ("Single-Character-Introducer"))
133 (#x9B ("Control-Sequence-Introducer"))
134 (#x9C ("String-Terminator"))
135 (#x9D ("Operating-System-Command"))
136 (#x9E ("Privacy-Message"))
137 (#x9F ("Application-Program-Command"))))) ; *** See Note above.
139 ;;;; accessor functions
142 ;; (+ 1488 (ash #x110000 -8)) => 5840
143 (defun ucd-index (char)
144 (let* ((cp (char-code char))
145 (cp-high (ash cp -8))
146 (page (aref *character-database* (+ 1488 cp-high))))
147 (+ 5840 (ash page 10) (ash (ldb (byte 8 0) cp) 2))))
149 (defun ucd-value-0 (char)
150 (aref *character-database* (ucd-index char)))
152 (defun ucd-value-1 (char)
153 (let ((index (ucd-index char)))
154 (dpb (aref *character-database* (+ index 3))
156 (dpb (aref *character-database* (+ index 2))
158 (aref *character-database* (1+ index))))))
160 (defun ucd-general-category (char)
161 (aref *character-database* (* 8 (ucd-value-0 char))))
163 (defun ucd-decimal-digit (char)
164 (let ((decimal-digit (aref *character-database*
165 (+ 3 (* 8 (ucd-value-0 char))))))
166 (when (< decimal-digit 10)
169 (defun char-code (char)
171 "Return the integer code of CHAR."
172 ;; FIXME: do we actually need this?
174 (character (char-code (truly-the character char)))))
176 (defun char-int (char)
178 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
179 there are no character bits or fonts.)"
182 (defun code-char (code)
184 "Return the character with the code CODE."
187 (defun character (object)
189 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are
190 characters, strings and symbols of length 1."
191 (flet ((do-error (control args)
192 (error 'simple-type-error
194 ;;?? how to express "symbol with name of length 1"?
195 :expected-type '(or character (string 1))
196 :format-control control
197 :format-arguments args)))
200 (string (if (= 1 (length (the string object)))
203 "String is not of length one: ~S" (list object))))
204 (symbol (if (= 1 (length (symbol-name object)))
205 (schar (symbol-name object) 0)
207 "Symbol name is not of length one: ~S" (list object))))
208 (t (do-error "~S cannot be coerced to a character." (list object))))))
210 (defun char-name (char)
212 "Return the name (a STRING) for a CHARACTER object."
213 (car (rassoc char *char-name-alist*)))
215 (defun name-char (name)
217 "Given an argument acceptable to STRING, NAME-CHAR returns a character
218 whose name is that string, if one exists. Otherwise, NIL is returned."
219 (cdr (assoc (string name) *char-name-alist* :test #'string-equal)))
223 (defun standard-char-p (char)
225 "The argument must be a character object. STANDARD-CHAR-P returns T if the
226 argument is a standard character -- one of the 95 ASCII printing characters
228 (and (typep char 'base-char)
229 (let ((n (char-code (the base-char char))))
233 (defun %standard-char-p (thing)
235 "Return T if and only if THING is a standard-char. Differs from
236 STANDARD-CHAR-P in that THING doesn't have to be a character."
237 (and (characterp thing) (standard-char-p thing)))
239 (defun graphic-char-p (char)
241 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
242 argument is a printing character (space through ~ in ASCII), otherwise
244 (let ((n (char-code char)))
248 (defun alpha-char-p (char)
250 "The argument must be a character object. ALPHA-CHAR-P returns T if the
251 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
252 (< (ucd-general-category char) 5))
254 (defun upper-case-p (char)
256 "The argument must be a character object; UPPER-CASE-P returns T if the
257 argument is an upper-case character, NIL otherwise."
258 (= (ucd-value-0 char) 0))
260 (defun lower-case-p (char)
262 "The argument must be a character object; LOWER-CASE-P returns T if the
263 argument is a lower-case character, NIL otherwise."
264 (= (ucd-value-0 char) 1))
266 (defun both-case-p (char)
268 "The argument must be a character object. BOTH-CASE-P returns T if the
269 argument is an alphabetic character and if the character exists in
270 both upper and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
271 (< (ucd-value-0 char) 2))
273 (defun digit-char-p (char &optional (radix 10.))
275 "If char is a digit in the specified radix, returns the fixnum for
276 which that digit stands, else returns NIL."
277 (let ((m (- (char-code char) 48)))
279 (cond ((<= radix 10.)
280 ;; Special-case decimal and smaller radices.
281 (if (and (>= m 0) (< m radix)) m nil))
282 ;; Digits 0 - 9 are used as is, since radix is larger.
283 ((and (>= m 0) (< m 10)) m)
284 ;; Check for upper case A - Z.
285 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
286 ;; Also check lower case a - z.
287 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
289 (t (let ((number (ucd-decimal-digit char)))
290 (when (and number (< number radix))
293 (defun alphanumericp (char)
295 "Given a character-object argument, ALPHANUMERICP returns T if the
296 argument is either numeric or alphabetic."
297 (let ((gc (ucd-general-category char)))
301 (defun char= (character &rest more-characters)
303 "Return T if all of the arguments are the same character."
304 (dolist (c more-characters t)
305 (declare (type character c))
306 (unless (eq c character) (return nil))))
308 (defun char/= (character &rest more-characters)
310 "Return T if no two of the arguments are the same character."
311 (do* ((head character (car list))
312 (list more-characters (cdr list)))
314 (declare (type character head))
316 (declare (type character c))
317 (when (eq head c) (return-from char/= nil)))))
319 (defun char< (character &rest more-characters)
321 "Return T if the arguments are in strictly increasing alphabetic order."
322 (do* ((c character (car list))
323 (list more-characters (cdr list)))
325 (unless (< (char-int c)
326 (char-int (car list)))
329 (defun char> (character &rest more-characters)
331 "Return T if the arguments are in strictly decreasing alphabetic order."
332 (do* ((c character (car list))
333 (list more-characters (cdr list)))
335 (unless (> (char-int c)
336 (char-int (car list)))
339 (defun char<= (character &rest more-characters)
341 "Return T if the arguments are in strictly non-decreasing alphabetic order."
342 (do* ((c character (car list))
343 (list more-characters (cdr list)))
345 (unless (<= (char-int c)
346 (char-int (car list)))
349 (defun char>= (character &rest more-characters)
351 "Return T if the arguments are in strictly non-increasing alphabetic order."
352 (do* ((c character (car list))
353 (list more-characters (cdr list)))
355 (unless (>= (char-int c)
356 (char-int (car list)))
359 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
360 ;;; which loses font, bits, and case info.
362 (defmacro equal-char-code (character)
364 `(let ((,ch ,character))
365 (if (= (ucd-value-0 ,ch) 0)
369 (defun char-equal (character &rest more-characters)
371 "Return T if all of the arguments are the same character.
372 Font, bits, and case are ignored."
373 (do ((clist more-characters (cdr clist)))
375 (unless (= (equal-char-code (car clist))
376 (equal-char-code character))
379 (defun char-not-equal (character &rest more-characters)
381 "Return T if no two of the arguments are the same character.
382 Font, bits, and case are ignored."
383 (do* ((head character (car list))
384 (list more-characters (cdr list)))
386 (unless (do* ((l list (cdr l)))
388 (if (= (equal-char-code head)
389 (equal-char-code (car l)))
393 (defun char-lessp (character &rest more-characters)
395 "Return T if the arguments are in strictly increasing alphabetic order.
396 Font, bits, and case are ignored."
397 (do* ((c character (car list))
398 (list more-characters (cdr list)))
400 (unless (< (equal-char-code c)
401 (equal-char-code (car list)))
404 (defun char-greaterp (character &rest more-characters)
406 "Return T if the arguments are in strictly decreasing alphabetic order.
407 Font, bits, and case are ignored."
408 (do* ((c character (car list))
409 (list more-characters (cdr list)))
411 (unless (> (equal-char-code c)
412 (equal-char-code (car list)))
415 (defun char-not-greaterp (character &rest more-characters)
417 "Return T if the arguments are in strictly non-decreasing alphabetic order.
418 Font, bits, and case are ignored."
419 (do* ((c character (car list))
420 (list more-characters (cdr list)))
422 (unless (<= (equal-char-code c)
423 (equal-char-code (car list)))
426 (defun char-not-lessp (character &rest more-characters)
428 "Return T if the arguments are in strictly non-increasing alphabetic order.
429 Font, bits, and case are ignored."
430 (do* ((c character (car list))
431 (list more-characters (cdr list)))
433 (unless (>= (equal-char-code c)
434 (equal-char-code (car list)))
437 ;;;; miscellaneous functions
439 (defun char-upcase (char)
441 "Return CHAR converted to upper-case if that is possible. Don't convert
442 lowercase eszet (U+DF)."
443 (if (= (ucd-value-0 char) 1)
444 (code-char (ucd-value-1 char))
447 (defun char-downcase (char)
449 "Return CHAR converted to lower-case if that is possible."
450 (if (= (ucd-value-0 char) 0)
451 (code-char (ucd-value-1 char))
454 (defun digit-char (weight &optional (radix 10))
456 "All arguments must be integers. Returns a character object that
457 represents a digit of the given weight in the specified radix. Returns
458 NIL if no such character exists."
459 (and (typep weight 'fixnum)
460 (>= weight 0) (< weight radix) (< weight 36)
461 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))