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*))
40 (defvar *unicode-character-name-database*)
41 (defvar *unicode-character-name-huffman-tree*))
44 (flet ((file (name type)
45 (merge-pathnames (make-pathname
47 '(:relative :up :up "output")
48 :name name :type type)
49 sb!xc:*compile-file-truename*)))
51 ,(with-open-file (stream (file "ucd" "dat")
53 :element-type '(unsigned-byte 8))
54 (let* ((length (file-length stream))
56 length :element-type '(unsigned-byte 8))))
57 (read-sequence array stream)
58 `(defun !character-database-cold-init ()
59 (setq *character-database* ',array))))
60 ,(with-open-file (stream (file "ucd-names" "lisp-expr")
62 :element-type 'character)
63 (let ((names (make-hash-table)))
66 for code-point = (read stream nil nil)
67 for char-name = (string-upcase (read stream nil nil))
69 do (setf (gethash code-point names) char-name))
70 (let ((tree (make-huffman-tree
72 (maphash (lambda (code name)
73 (declare (ignore code))
78 (make-array (hash-table-count names)
81 (maphash (lambda (code name)
83 (cons code (huffman-encode name tree))
87 (sort (copy-seq code->name) #'< :key #'cdr))
89 (sort (copy-seq name->code) #'< :key #'car))
91 `(defun !character-name-database-cold-init ()
93 (setq *unicode-character-name-database*
94 (cons ',code->name ',name->code)
95 *unicode-character-name-huffman-tree* ',tree)))))))))
97 #+sb-xc-host (!character-database-cold-init)
98 #+sb-xc-host (!character-name-database-cold-init)
100 (defparameter *base-char-name-alist*
101 ;; Note: The *** markers here indicate character names which are
102 ;; required by the ANSI specification of #'CHAR-NAME. For the others,
103 ;; we prefer the ASCII standard name.
104 '((#x00 "Nul" "Null" "^@")
111 (#x07 "Bel" "Bell" "^g")
112 (#x08 "Backspace" "^h" "Bs") ; *** See Note above
113 (#x09 "Tab" "^i" "Ht") ; *** See Note above
114 (#x0A "Newline" "Linefeed" "^j" "Lf" "Nl") ; *** See Note above
116 (#x0C "Page" "^l" "Form" "Formfeed" "Ff" "Np") ; *** See Note above
117 (#x0D "Return" "^m" "Cr") ; *** See Note above
131 (#x1B "Esc" "Escape" "^[" "Altmode" "Alt")
136 (#x20 "Space" "Sp") ; *** See Note above
137 (#x7f "Rubout" "Delete" "Del")
140 (#x82 "Break-Permitted")
141 (#x83 "No-Break-Permitted")
144 (#x86 "Start-Selected-Area")
145 (#x87 "End-Selected-Area")
146 (#x88 "Character-Tabulation-Set")
147 (#x89 "Character-Tabulation-With-Justification")
148 (#x8A "Line-Tabulation-Set")
149 (#x8B "Partial-Line-Forward")
150 (#x8C "Partial-Line-Backward")
151 (#x8D "Reverse-Linefeed")
152 (#x8E "Single-Shift-Two")
153 (#x8F "Single-Shift-Three")
154 (#x90 "Device-Control-String")
155 (#x91 "Private-Use-One")
156 (#x92 "Private-Use-Two")
157 (#x93 "Set-Transmit-State")
158 (#x94 "Cancel-Character")
159 (#x95 "Message-Waiting")
160 (#x96 "Start-Guarded-Area")
161 (#x97 "End-Guarded-Area")
162 (#x98 "Start-String")
164 (#x9A "Single-Character-Introducer")
165 (#x9B "Control-Sequence-Introducer")
166 (#x9C "String-Terminator")
167 (#x9D "Operating-System-Command")
168 (#x9E "Privacy-Message")
169 (#x9F "Application-Program-Command"))) ; *** See Note above
171 ;;;; accessor functions
174 ;; (+ 1488 (ash #x110000 -8)) => 5840
175 (defun ucd-index (char)
176 (let* ((cp (char-code char))
177 (cp-high (ash cp -8))
178 (page (aref *character-database* (+ 1488 cp-high))))
179 (+ 5840 (ash page 10) (ash (ldb (byte 8 0) cp) 2))))
181 (defun ucd-value-0 (char)
182 (aref *character-database* (ucd-index char)))
184 (defun ucd-value-1 (char)
185 (let ((index (ucd-index char)))
186 (dpb (aref *character-database* (+ index 3))
188 (dpb (aref *character-database* (+ index 2))
190 (aref *character-database* (1+ index))))))
192 (defun ucd-general-category (char)
193 (aref *character-database* (* 8 (ucd-value-0 char))))
195 (defun ucd-decimal-digit (char)
196 (let ((decimal-digit (aref *character-database*
197 (+ 3 (* 8 (ucd-value-0 char))))))
198 (when (< decimal-digit 10)
201 (defun char-code (char)
203 "Return the integer code of CHAR."
204 ;; FIXME: do we actually need this?
206 (character (char-code (truly-the character char)))))
208 (defun char-int (char)
210 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
211 there are no character bits or fonts.)"
214 (defun code-char (code)
216 "Return the character with the code CODE."
219 (defun character (object)
221 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are
222 characters, strings and symbols of length 1."
223 (flet ((do-error (control args)
224 (error 'simple-type-error
226 ;;?? how to express "symbol with name of length 1"?
227 :expected-type '(or character (string 1))
228 :format-control control
229 :format-arguments args)))
232 (string (if (= 1 (length (the string object)))
235 "String is not of length one: ~S" (list object))))
236 (symbol (if (= 1 (length (symbol-name object)))
237 (schar (symbol-name object) 0)
239 "Symbol name is not of length one: ~S" (list object))))
240 (t (do-error "~S cannot be coerced to a character." (list object))))))
242 (defun char-name (char)
244 "Return the name (a STRING) for a CHARACTER object."
245 (let ((char-code (char-code char)))
246 (or (second (assoc char-code *base-char-name-alist*))
248 (let ((h-code (cdr (binary-search char-code
249 (car *unicode-character-name-database*)
252 (huffman-decode h-code *unicode-character-name-huffman-tree*))))))
254 (defun name-char (name)
256 "Given an argument acceptable to STRING, NAME-CHAR returns a character
257 whose name is that string, if one exists. Otherwise, NIL is returned."
258 (or (let ((char-code (car (rassoc-if (lambda (names)
259 (member name names :test #'string-equal))
260 *base-char-name-alist*))))
262 (code-char char-code)))
264 (let ((encoding (huffman-encode (string-upcase name)
265 *unicode-character-name-huffman-tree*)))
268 (car (binary-search encoding
269 (cdr *unicode-character-name-database*)
272 (code-char char-code)))))))
276 (defun standard-char-p (char)
278 "The argument must be a character object. STANDARD-CHAR-P returns T if the
279 argument is a standard character -- one of the 95 ASCII printing characters
281 (and (typep char 'base-char)
282 (let ((n (char-code (the base-char char))))
286 (defun %standard-char-p (thing)
288 "Return T if and only if THING is a standard-char. Differs from
289 STANDARD-CHAR-P in that THING doesn't have to be a character."
290 (and (characterp thing) (standard-char-p thing)))
292 (defun graphic-char-p (char)
294 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
295 argument is a printing character (space through ~ in ASCII), otherwise
297 (let ((n (char-code char)))
301 (defun alpha-char-p (char)
303 "The argument must be a character object. ALPHA-CHAR-P returns T if the
304 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
305 (< (ucd-general-category char) 5))
307 (defun upper-case-p (char)
309 "The argument must be a character object; UPPER-CASE-P returns T if the
310 argument is an upper-case character, NIL otherwise."
311 (= (ucd-value-0 char) 0))
313 (defun lower-case-p (char)
315 "The argument must be a character object; LOWER-CASE-P returns T if the
316 argument is a lower-case character, NIL otherwise."
317 (= (ucd-value-0 char) 1))
319 (defun both-case-p (char)
321 "The argument must be a character object. BOTH-CASE-P returns T if the
322 argument is an alphabetic character and if the character exists in
323 both upper and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
324 (< (ucd-value-0 char) 2))
326 (defun digit-char-p (char &optional (radix 10.))
328 "If char is a digit in the specified radix, returns the fixnum for
329 which that digit stands, else returns NIL."
330 (let ((m (- (char-code char) 48)))
332 (cond ((<= radix 10.)
333 ;; Special-case decimal and smaller radices.
334 (if (and (>= m 0) (< m radix)) m nil))
335 ;; Digits 0 - 9 are used as is, since radix is larger.
336 ((and (>= m 0) (< m 10)) m)
337 ;; Check for upper case A - Z.
338 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
339 ;; Also check lower case a - z.
340 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
342 (t (let ((number (ucd-decimal-digit char)))
343 (when (and number (< number radix))
346 (defun alphanumericp (char)
348 "Given a character-object argument, ALPHANUMERICP returns T if the
349 argument is either numeric or alphabetic."
350 (let ((gc (ucd-general-category char)))
354 (defun char= (character &rest more-characters)
356 "Return T if all of the arguments are the same character."
357 (dolist (c more-characters t)
358 (declare (type character c))
359 (unless (eq c character) (return nil))))
361 (defun char/= (character &rest more-characters)
363 "Return T if no two of the arguments are the same character."
364 (do* ((head character (car list))
365 (list more-characters (cdr list)))
367 (declare (type character head))
369 (declare (type character c))
370 (when (eq head c) (return-from char/= nil)))))
372 (defun char< (character &rest more-characters)
374 "Return T if the arguments are in strictly increasing alphabetic order."
375 (do* ((c character (car list))
376 (list more-characters (cdr list)))
378 (unless (< (char-int c)
379 (char-int (car list)))
382 (defun char> (character &rest more-characters)
384 "Return T if the arguments are in strictly decreasing alphabetic order."
385 (do* ((c character (car list))
386 (list more-characters (cdr list)))
388 (unless (> (char-int c)
389 (char-int (car list)))
392 (defun char<= (character &rest more-characters)
394 "Return T if the arguments are in strictly non-decreasing alphabetic order."
395 (do* ((c character (car list))
396 (list more-characters (cdr list)))
398 (unless (<= (char-int c)
399 (char-int (car list)))
402 (defun char>= (character &rest more-characters)
404 "Return T if the arguments are in strictly non-increasing alphabetic order."
405 (do* ((c character (car list))
406 (list more-characters (cdr list)))
408 (unless (>= (char-int c)
409 (char-int (car list)))
412 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
413 ;;; which loses font, bits, and case info.
415 (defmacro equal-char-code (character)
417 `(let ((,ch ,character))
418 (if (= (ucd-value-0 ,ch) 0)
422 (defun char-equal (character &rest more-characters)
424 "Return T if all of the arguments are the same character.
425 Font, bits, and case are ignored."
426 (do ((clist more-characters (cdr clist)))
428 (unless (= (equal-char-code (car clist))
429 (equal-char-code character))
432 (defun char-not-equal (character &rest more-characters)
434 "Return T if no two of the arguments are the same character.
435 Font, bits, and case are ignored."
436 (do* ((head character (car list))
437 (list more-characters (cdr list)))
439 (unless (do* ((l list (cdr l)))
441 (if (= (equal-char-code head)
442 (equal-char-code (car l)))
446 (defun char-lessp (character &rest more-characters)
448 "Return T if the arguments are in strictly increasing alphabetic order.
449 Font, bits, and case are ignored."
450 (do* ((c character (car list))
451 (list more-characters (cdr list)))
453 (unless (< (equal-char-code c)
454 (equal-char-code (car list)))
457 (defun char-greaterp (character &rest more-characters)
459 "Return T if the arguments are in strictly decreasing alphabetic order.
460 Font, bits, and case are ignored."
461 (do* ((c character (car list))
462 (list more-characters (cdr list)))
464 (unless (> (equal-char-code c)
465 (equal-char-code (car list)))
468 (defun char-not-greaterp (character &rest more-characters)
470 "Return T if the arguments are in strictly non-decreasing alphabetic order.
471 Font, bits, and case are ignored."
472 (do* ((c character (car list))
473 (list more-characters (cdr list)))
475 (unless (<= (equal-char-code c)
476 (equal-char-code (car list)))
479 (defun char-not-lessp (character &rest more-characters)
481 "Return T if the arguments are in strictly non-increasing alphabetic order.
482 Font, bits, and case are ignored."
483 (do* ((c character (car list))
484 (list more-characters (cdr list)))
486 (unless (>= (equal-char-code c)
487 (equal-char-code (car list)))
490 ;;;; miscellaneous functions
492 (defun char-upcase (char)
494 "Return CHAR converted to upper-case if that is possible. Don't convert
495 lowercase eszet (U+DF)."
496 (if (= (ucd-value-0 char) 1)
497 (code-char (ucd-value-1 char))
500 (defun char-downcase (char)
502 "Return CHAR converted to lower-case if that is possible."
503 (if (= (ucd-value-0 char) 0)
504 (code-char (ucd-value-1 char))
507 (defun digit-char (weight &optional (radix 10))
509 "All arguments must be integers. Returns a character object that
510 represents a digit of the given weight in the specified radix. Returns
511 NIL if no such character exists."
512 (and (typep weight 'fixnum)
513 (>= weight 0) (< weight radix) (< weight 36)
514 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))