1 ;;;; character functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;; We compile some trivial character operations via inline expansion.
16 (declaim (inline standard-char-p graphic-char-p alpha-char-p
17 upper-case-p lower-case-p both-case-p alphanumericp
19 (declaim (maybe-inline digit-char-p digit-weight))
22 `(integer 0 (,char-code-limit)))
26 (defvar *unicode-character-name-database*)
27 (defvar *unicode-character-name-huffman-tree*))
31 (flet ((file (name type)
32 (merge-pathnames (make-pathname
34 '(:relative :up :up "output")
35 :name name :type type)
36 sb!xc:*compile-file-truename*))
37 (read-ub8-vector (pathname)
38 (with-open-file (stream pathname
40 :element-type '(unsigned-byte 8))
41 (let* ((length (file-length stream))
43 length :element-type '(unsigned-byte 8))))
44 (read-sequence array stream)
46 (let ((character-database (read-ub8-vector (file "ucd" "dat")))
47 (decompositions (read-ub8-vector (file "decomp" "dat")))
48 (long-decompositions (read-ub8-vector (file "ldecomp" "dat"))))
50 (declaim (type (simple-array (unsigned-byte 8) (*)) **character-database** **character-decompositions** **character-long-decompositions**))
51 (defglobal **character-database** ,character-database)
52 (defglobal **character-decompositions** ,decompositions)
53 (defglobal **character-long-decompositions** ,long-decompositions)
54 (defun !character-database-cold-init ()
55 (setf **character-database** ,character-database))
56 ,(with-open-file (stream (file "ucd-names" "lisp-expr")
58 :element-type 'character)
59 (let ((names (make-hash-table)))
62 for code-point = (read stream nil nil)
63 for char-name = (string-upcase (read stream nil nil))
65 do (setf (gethash code-point names) char-name))
70 (maphash (lambda (code name)
71 (declare (ignore code))
76 (make-array (hash-table-count names)
79 (maphash (lambda (code name)
81 (cons code (huffman-encode name tree))
85 (sort (copy-seq code->name) #'< :key #'cdr))
87 (sort (copy-seq name->code) #'< :key #'car))
89 `(defun !character-name-database-cold-init ()
91 (setq *unicode-character-name-database*
92 (cons ',code->name ',name->code)
93 *unicode-character-name-huffman-tree* ',tree))))))))))
95 #+sb-xc-host (!character-name-database-cold-init)
97 (defparameter *base-char-name-alist*
98 ;; Note: The *** markers here indicate character names which are
99 ;; required by the ANSI specification of #'CHAR-NAME. For the others,
100 ;; we prefer the ASCII standard name.
101 '((#x00 "Nul" "Null" "^@")
108 (#x07 "Bel" "Bell" "^g")
109 (#x08 "Backspace" "^h" "Bs") ; *** See Note above
110 (#x09 "Tab" "^i" "Ht") ; *** See Note above
111 (#x0A "Newline" "Linefeed" "^j" "Lf" "Nl") ; *** See Note above
113 (#x0C "Page" "^l" "Form" "Formfeed" "Ff" "Np") ; *** See Note above
114 (#x0D "Return" "^m" "Cr") ; *** See Note above
128 (#x1B "Esc" "Escape" "^[" "Altmode" "Alt")
133 (#x20 "Space" "Sp") ; *** See Note above
134 (#x7f "Rubout" "Delete" "Del")
137 (#x82 "Break-Permitted")
138 (#x83 "No-Break-Permitted")
141 (#x86 "Start-Selected-Area")
142 (#x87 "End-Selected-Area")
143 (#x88 "Character-Tabulation-Set")
144 (#x89 "Character-Tabulation-With-Justification")
145 (#x8A "Line-Tabulation-Set")
146 (#x8B "Partial-Line-Forward")
147 (#x8C "Partial-Line-Backward")
148 (#x8D "Reverse-Linefeed")
149 (#x8E "Single-Shift-Two")
150 (#x8F "Single-Shift-Three")
151 (#x90 "Device-Control-String")
152 (#x91 "Private-Use-One")
153 (#x92 "Private-Use-Two")
154 (#x93 "Set-Transmit-State")
155 (#x94 "Cancel-Character")
156 (#x95 "Message-Waiting")
157 (#x96 "Start-Guarded-Area")
158 (#x97 "End-Guarded-Area")
159 (#x98 "Start-String")
161 (#x9A "Single-Character-Introducer")
162 (#x9B "Control-Sequence-Introducer")
163 (#x9C "String-Terminator")
164 (#x9D "Operating-System-Command")
165 (#x9E "Privacy-Message")
166 (#x9F "Application-Program-Command"))) ; *** See Note above
168 ;;;; UCD accessor functions
170 ;;; The first (* 8 395) => 3160 entries in **CHARACTER-DATABASE**
171 ;;; contain entries for the distinct character attributes:
172 ;;; specifically, indexes into the GC kinds, Bidi kinds, CCC kinds,
173 ;;; the decimal digit property, the digit property and the
174 ;;; bidi-mirrored boolean property. (There are two spare bytes for
175 ;;; other information, should that become necessary)
177 ;;; the next (ash #x110000 -8) entries contain single-byte indexes
178 ;;; into a table of 256-element 4-byte-sized entries. These entries
179 ;;; follow directly on, and are of the form
180 ;;; {attribute-index[11b],transformed-code-point[21b]}x256, where the
181 ;;; attribute index is an index into the miscellaneous information
182 ;;; table, and the transformed code point is the code point of the
183 ;;; simple mapping of the character to its lowercase or uppercase
184 ;;; equivalent, as appropriate and if any.
186 ;;; I feel the opacity of the above suggests the need for a diagram:
188 ;;; C _______________________________________
191 ;;; [***************|=============================|--------...]
193 ;;; A \______________________/| B
195 ;;; To look up information about a character, take the high 13 bits of
196 ;;; its code point, and index the character database with that and a
197 ;;; base of 3160 (going past the miscellaneous information[*], so
198 ;;; treating (a) as the start of the array). This, labelled A, gives
199 ;;; us another index into the detailed pages[-], which we can use to
200 ;;; look up the details for the character in question: we add the low
201 ;;; 8 bits of the character, shifted twice (because we have four-byte
202 ;;; table entries) to 1024 times the `page' index, with a base of 6088
203 ;;; to skip over everything else. This gets us to point B. If we're
204 ;;; after a transformed code point (i.e. an upcase or downcase
205 ;;; operation), we can simply read it off now, beginning with an
206 ;;; offset of 11 bits from point B in some endianness; if we're
207 ;;; looking for miscellaneous information, we take the 11-bit value at
208 ;;; B, and index the character database once more to get to the
209 ;;; relevant miscellaneous information.
211 ;;; As an optimization to the common case (pun intended) of looking up
212 ;;; case information for a character, the entries in C above are
213 ;;; sorted such that the characters which are UPPER-CASE-P in CL terms
214 ;;; have index values lower than all others, followed by those which
215 ;;; are LOWER-CASE-P in CL terms; this permits implementation of
216 ;;; character case tests without actually going to the trouble of
217 ;;; looking up the value associated with the index. (Actually, this
218 ;;; isn't just a speed optimization; the information about whether a
219 ;;; character is BOTH-CASE-P is used just in the ordering and not
220 ;;; explicitly recorded in the database).
222 ;;; The moral of all this? Next time, don't just say "FIXME: document
224 (defun ucd-index (char)
225 (let* ((cp (char-code char))
226 (cp-high (ash cp -8))
227 (page (aref **character-database** (+ 3160 cp-high))))
228 (+ 7512 (ash page 10) (ash (ldb (byte 8 0) cp) 2))))
230 (declaim (ftype (sfunction (t) (unsigned-byte 11)) ucd-value-0))
231 (defun ucd-value-0 (char)
232 (let ((index (ucd-index char))
233 (character-database **character-database**))
234 (dpb (aref character-database index)
236 (ldb (byte 3 5) (aref character-database (+ index 1))))))
238 (declaim (ftype (sfunction (t) (unsigned-byte 21)) ucd-value-1))
239 (defun ucd-value-1 (char)
240 (let ((index (ucd-index char))
241 (character-database **character-database**))
242 (dpb (aref character-database (+ index 1))
244 (dpb (aref character-database (+ index 2))
246 (aref character-database (+ index 3))))))
248 (declaim (ftype (sfunction (t) (unsigned-byte 8)) ucd-general-category))
249 (defun ucd-general-category (char)
250 (aref **character-database** (* 8 (ucd-value-0 char))))
252 (defun ucd-decimal-digit (char)
253 (let ((decimal-digit (aref **character-database**
254 (+ 3 (* 8 (ucd-value-0 char))))))
255 (when (< decimal-digit 10)
258 (defun char-code (char)
260 "Return the integer code of CHAR."
263 (defun char-int (char)
265 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
266 there are no character bits or fonts.)"
269 (defun code-char (code)
271 "Return the character with the code CODE."
274 (defun character (object)
276 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are characters,
277 strings and symbols of length 1."
278 (flet ((do-error (control args)
279 (error 'simple-type-error
281 ;;?? how to express "symbol with name of length 1"?
282 :expected-type '(or character (string 1))
283 :format-control control
284 :format-arguments args)))
287 (string (if (= 1 (length (the string object)))
290 "String is not of length one: ~S" (list object))))
291 (symbol (if (= 1 (length (symbol-name object)))
292 (schar (symbol-name object) 0)
294 "Symbol name is not of length one: ~S" (list object))))
295 (t (do-error "~S cannot be coerced to a character." (list object))))))
297 (defun char-name (char)
299 "Return the name (a STRING) for a CHARACTER object."
300 (let ((char-code (char-code char)))
301 (or (second (assoc char-code *base-char-name-alist*))
303 (let ((h-code (cdr (binary-search char-code
304 (car *unicode-character-name-database*)
308 (huffman-decode h-code *unicode-character-name-huffman-tree*))
309 ((< char-code #x10000)
310 (format nil "U~4,'0X" char-code))
312 (format nil "U~8,'0X" char-code)))))))
314 (defun name-char (name)
316 "Given an argument acceptable to STRING, NAME-CHAR returns a character whose
317 name is that string, if one exists. Otherwise, NIL is returned."
318 (or (let ((char-code (car (rassoc-if (lambda (names)
319 (member name names :test #'string-equal))
320 *base-char-name-alist*))))
322 (code-char char-code)))
324 (let ((encoding (huffman-encode (string-upcase name)
325 *unicode-character-name-huffman-tree*)))
328 (car (binary-search encoding
329 (cdr *unicode-character-name-database*)
331 (name-string (string name))
332 (name-length (length name-string)))
335 (code-char char-code))
336 ((and (or (= name-length 9)
338 (char-equal (char name-string 0) #\U)
339 (loop for i from 1 below name-length
340 always (digit-char-p (char name-string i) 16)))
341 (code-char (parse-integer name-string :start 1 :radix 16)))
347 (defun standard-char-p (char)
349 "The argument must be a character object. STANDARD-CHAR-P returns T if the
350 argument is a standard character -- one of the 95 ASCII printing characters or
352 (and (typep char 'base-char)
353 (let ((n (char-code (the base-char char))))
357 (defun %standard-char-p (thing)
359 "Return T if and only if THING is a standard-char. Differs from
360 STANDARD-CHAR-P in that THING doesn't have to be a character."
361 (and (characterp thing) (standard-char-p thing)))
363 (defun graphic-char-p (char)
365 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
366 argument is a printing character (space through ~ in ASCII), otherwise returns
368 (let ((n (char-code char)))
372 (defun alpha-char-p (char)
374 "The argument must be a character object. ALPHA-CHAR-P returns T if the
375 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
376 (< (ucd-general-category char) 5))
378 (defun upper-case-p (char)
380 "The argument must be a character object; UPPER-CASE-P returns T if the
381 argument is an upper-case character, NIL otherwise."
382 (< (ucd-value-0 char) 4))
384 (defun lower-case-p (char)
386 "The argument must be a character object; LOWER-CASE-P returns T if the
387 argument is a lower-case character, NIL otherwise."
388 (< 3 (ucd-value-0 char) 8))
390 (defun both-case-p (char)
392 "The argument must be a character object. BOTH-CASE-P returns T if the
393 argument is an alphabetic character and if the character exists in both upper
394 and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
395 (< (ucd-value-0 char) 8))
397 (defun digit-char-p (char &optional (radix 10.))
399 "If char is a digit in the specified radix, returns the fixnum for which
400 that digit stands, else returns NIL."
401 (let ((m (- (char-code char) 48)))
403 (cond ((<= radix 10.)
404 ;; Special-case decimal and smaller radices.
405 (if (and (>= m 0) (< m radix)) m nil))
406 ;; Digits 0 - 9 are used as is, since radix is larger.
407 ((and (>= m 0) (< m 10)) m)
408 ;; Check for upper case A - Z.
409 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
410 ;; Also check lower case a - z.
411 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
413 (t (let ((number (ucd-decimal-digit char)))
414 (when (and number (< number radix))
417 (defun alphanumericp (char)
419 "Given a character-object argument, ALPHANUMERICP returns T if the argument
420 is either numeric or alphabetic."
421 (let ((gc (ucd-general-category char)))
425 (defun char= (character &rest more-characters)
427 "Return T if all of the arguments are the same character."
428 (declare (truly-dynamic-extent more-characters))
429 (dolist (c more-characters t)
430 (declare (type character c))
431 (unless (eq c character) (return nil))))
433 (defun char/= (character &rest more-characters)
435 "Return T if no two of the arguments are the same character."
436 (declare (truly-dynamic-extent more-characters))
437 (do* ((head character (car list))
438 (list more-characters (cdr list)))
440 (declare (type character head))
442 (declare (type character c))
443 (when (eq head c) (return-from char/= nil)))))
445 (defun char< (character &rest more-characters)
447 "Return T if the arguments are in strictly increasing alphabetic order."
448 (declare (truly-dynamic-extent more-characters))
449 (do* ((c character (car list))
450 (list more-characters (cdr list)))
452 (unless (< (char-int c)
453 (char-int (car list)))
456 (defun char> (character &rest more-characters)
458 "Return T if the arguments are in strictly decreasing alphabetic order."
459 (declare (truly-dynamic-extent more-characters))
460 (do* ((c character (car list))
461 (list more-characters (cdr list)))
463 (unless (> (char-int c)
464 (char-int (car list)))
467 (defun char<= (character &rest more-characters)
469 "Return T if the arguments are in strictly non-decreasing alphabetic order."
470 (declare (truly-dynamic-extent more-characters))
471 (do* ((c character (car list))
472 (list more-characters (cdr list)))
474 (unless (<= (char-int c)
475 (char-int (car list)))
478 (defun char>= (character &rest more-characters)
480 "Return T if the arguments are in strictly non-increasing alphabetic order."
481 (declare (truly-dynamic-extent more-characters))
482 (do* ((c character (car list))
483 (list more-characters (cdr list)))
485 (unless (>= (char-int c)
486 (char-int (car list)))
489 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
490 ;;; which loses font, bits, and case info.
492 (defmacro equal-char-code (character)
494 `(let ((,ch ,character))
495 (if (= (ucd-value-0 ,ch) 0)
499 (defun two-arg-char-equal (c1 c2)
500 (= (equal-char-code c1) (equal-char-code c2)))
502 (defun char-equal (character &rest more-characters)
504 "Return T if all of the arguments are the same character.
506 (declare (truly-dynamic-extent more-characters))
507 (do ((clist more-characters (cdr clist)))
509 (unless (two-arg-char-equal (car clist) character)
512 (defun two-arg-char-not-equal (c1 c2)
513 (/= (equal-char-code c1) (equal-char-code c2)))
515 (defun char-not-equal (character &rest more-characters)
517 "Return T if no two of the arguments are the same character.
519 (declare (truly-dynamic-extent more-characters))
520 (do* ((head character (car list))
521 (list more-characters (cdr list)))
523 (unless (do* ((l list (cdr l)))
525 (if (two-arg-char-equal head (car l))
529 (defun two-arg-char-lessp (c1 c2)
530 (< (equal-char-code c1) (equal-char-code c2)))
532 (defun char-lessp (character &rest more-characters)
534 "Return T if the arguments are in strictly increasing alphabetic order.
536 (declare (truly-dynamic-extent more-characters))
537 (do* ((c character (car list))
538 (list more-characters (cdr list)))
540 (unless (two-arg-char-lessp c (car list))
543 (defun two-arg-char-greaterp (c1 c2)
544 (> (equal-char-code c1) (equal-char-code c2)))
546 (defun char-greaterp (character &rest more-characters)
548 "Return T if the arguments are in strictly decreasing alphabetic order.
550 (declare (truly-dynamic-extent more-characters))
551 (do* ((c character (car list))
552 (list more-characters (cdr list)))
554 (unless (two-arg-char-greaterp c (car list))
557 (defun two-arg-char-not-greaterp (c1 c2)
558 (<= (equal-char-code c1) (equal-char-code c2)))
560 (defun char-not-greaterp (character &rest more-characters)
562 "Return T if the arguments are in strictly non-decreasing alphabetic order.
564 (declare (truly-dynamic-extent more-characters))
565 (do* ((c character (car list))
566 (list more-characters (cdr list)))
568 (unless (two-arg-char-not-greaterp c (car list))
571 (defun two-arg-char-not-lessp (c1 c2)
572 (>= (equal-char-code c1) (equal-char-code c2)))
574 (defun char-not-lessp (character &rest more-characters)
576 "Return T if the arguments are in strictly non-increasing alphabetic order.
578 (declare (truly-dynamic-extent more-characters))
579 (do* ((c character (car list))
580 (list more-characters (cdr list)))
582 (unless (two-arg-char-not-lessp c (car list))
585 ;;;; miscellaneous functions
587 (defun char-upcase (char)
589 "Return CHAR converted to upper-case if that is possible. Don't convert
590 lowercase eszet (U+DF)."
591 (if (< 3 (ucd-value-0 char) 8)
592 (code-char (ucd-value-1 char))
595 (defun char-downcase (char)
597 "Return CHAR converted to lower-case if that is possible."
598 (if (< (ucd-value-0 char) 4)
599 (code-char (ucd-value-1 char))
602 (defun digit-char (weight &optional (radix 10))
604 "All arguments must be integers. Returns a character object that represents
605 a digit of the given weight in the specified radix. Returns NIL if no such
607 (and (typep weight 'fixnum)
608 (>= weight 0) (< weight radix) (< weight 36)
609 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))
611 (defun char-decomposition-info (char)
612 (aref **character-database** (+ 6 (* 8 (ucd-value-0 char)))))
614 (defun char-decomposition (char)
615 (let* ((cp (char-code char))
616 (cp-high (ash cp -8))
617 (decompositions **character-decompositions**)
618 (long-decompositions **character-long-decompositions**)
620 (ash (aref decompositions cp-high) 10)
621 (ash (ldb (byte 8 0) cp) 2)))
622 (v0 (aref decompositions index))
623 (v1 (aref decompositions (+ index 1)))
624 (v2 (aref decompositions (+ index 2)))
625 (v3 (aref decompositions (+ index 3)))
626 (length (dpb v0 (byte 8 3) (ldb (byte 3 5) v1)))
627 (entry (dpb (ldb (byte 5 0) v1) (byte 5 16)
628 (dpb v2 (byte 8 8) v3))))
630 (string (code-char entry))
631 (let ((result (make-string length))
633 (dotimes (i length result)
634 (let ((code (dpb (aref long-decompositions (+ e 1))
636 (dpb (aref long-decompositions (+ e 2))
638 (aref long-decompositions (+ e 3))))))
639 (setf (char result i) (code-char code)))
642 (defun decompose-char (char)
643 (if (= (char-decomposition-info char) 0)
645 (char-decomposition char)))