Declare EXPLICIT-CHECK on CONCATENATE, MAKE-STRING, SET-PPRINT-DISPATCH.
[sbcl.git] / src / code / target-char.lisp
blob65a0a4d85105724b45061ccf90605fdd3a4eb3a4
1 ;;;; character functions
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
15 #!-sb-fluid
16 (declaim (inline standard-char-p graphic-char-p alpha-char-p
17 upper-case-p lower-case-p both-case-p alphanumericp
18 char-int))
19 (declaim (maybe-inline digit-char-p))
21 (deftype char-code ()
22 `(integer 0 (,sb!xc:char-code-limit)))
24 (defglobal **unicode-character-name-huffman-tree** ())
26 (declaim (inline pack-3-codepoints))
27 (defun pack-3-codepoints (first &optional (second 0) (third 0))
28 (declare (type (unsigned-byte 21) first second third))
29 (sb!c::mask-signed-field 63 (logior first (ash second 21) (ash third 42))))
31 (macrolet ((frob ()
32 (flet ((coerce-it (array)
33 (!coerce-to-specialized array '(unsigned-byte 8)))
34 (file (name type)
35 (merge-pathnames (make-pathname
36 :directory
37 '(:relative :up :up "output")
38 :name name :type type)
39 sb!xc:*compile-file-truename*))
40 (read-ub8-vector (pathname)
41 (with-open-file (stream pathname
42 :element-type '(unsigned-byte 8))
43 (let* ((length (file-length stream))
44 (array (make-array
45 length :element-type '(unsigned-byte 8))))
46 (read-sequence array stream)
47 array)))
48 (init-global (name type)
49 `(progn
50 (defglobal ,name
51 ,(if (eql type 'hash-table)
52 `(make-hash-table)
53 `(make-array 0 :element-type ',type)))
54 (declaim (type ,(if (eql type 'hash-table)
55 'hash-table
56 `(simple-array ,type (*))) ,name)))))
57 (let ((misc-database (coerce-it (read-ub8-vector (file "ucdmisc" "dat"))))
58 (ucd-high-pages (coerce-it (read-ub8-vector (file "ucdhigh" "dat"))))
59 (ucd-low-pages (coerce-it (read-ub8-vector (file "ucdlow" "dat"))))
60 (decompositions (coerce-it (read-ub8-vector (file "decomp" "dat"))))
61 (primary-compositions (coerce-it (read-ub8-vector (file "comp" "dat"))))
62 (case-data (coerce-it (read-ub8-vector (file "case" "dat"))))
63 (case-pages (coerce-it (read-ub8-vector (file "casepages" "dat"))))
64 (collations (coerce-it (read-ub8-vector (file "collation" "dat")))))
65 `(progn
66 ;; KLUDGE: All temporary values, fixed up in cold-load
67 ,(init-global '**character-misc-database** '(unsigned-byte 8))
68 ,(init-global '**character-high-pages** '(unsigned-byte 16))
69 ,(init-global '**character-low-pages** '(unsigned-byte 16))
70 ,(init-global '**character-decompositions** '(unsigned-byte 21))
71 ,(init-global '**character-case-pages** '(unsigned-byte 8))
72 ,(init-global '**character-primary-compositions** 'hash-table)
73 ,(init-global '**character-cases** '(unsigned-byte 32))
74 ,(init-global '**character-unicode-cases** t)
75 ,(init-global '**character-collations** 'hash-table)
76 ,(init-global '**unicode-char-name-database** t)
77 ,(init-global '**unicode-name-char-database** t)
78 ,(init-global '**unicode-1-char-name-database** t)
79 ,(init-global '**unicode-1-name-char-database** t)
81 (defun !character-database-cold-init ()
82 (flet ((make-ubn-vector (raw-bytes n)
83 (let ((new-array
84 (make-array
85 (/ (length raw-bytes) n)
86 :element-type (list 'unsigned-byte (* 8 n)))))
87 (loop for i from 0 below (length raw-bytes) by n
88 for element = 0 do
89 (loop for offset from 0 below n do
90 (incf element
91 (ash (aref raw-bytes (+ i offset))
92 (* 8 (- n offset 1)))))
93 (setf (aref new-array (/ i n)) element))
94 new-array)))
95 (setf **character-misc-database**
96 ,misc-database
97 **character-high-pages**
98 (make-ubn-vector ,ucd-high-pages 2)
99 **character-low-pages**
100 (make-ubn-vector ,ucd-low-pages 2)
101 **character-case-pages**
102 ,(coerce-it case-pages)
103 **character-decompositions**
104 (make-ubn-vector ,decompositions 3))
106 (setf **character-primary-compositions**
107 (let ((table (make-hash-table))
108 (info (make-ubn-vector ,primary-compositions 3)))
109 (dotimes (i (/ (length info) 3))
110 (setf (gethash (dpb (aref info (* 3 i)) (byte 21 21)
111 (aref info (1+ (* 3 i))))
112 table)
113 (aref info (+ (* 3 i) 2))))
114 table))
116 (let* ((unicode-table
117 (make-array
118 (* 64 (1+ (aref **character-case-pages**
119 (1- (length **character-case-pages**)))))))
120 (table (make-array
121 (* 2 (length unicode-table))
122 :element-type '(unsigned-byte 32)))
123 (info ,case-data)
124 (index 0)
125 (length (length info)))
126 (labels ((read-codepoint ()
127 (let* ((b1 (aref info index))
128 (b2 (aref info (incf index)))
129 (b3 (aref info (incf index))))
130 (incf index)
131 (dpb b1 (byte 8 16)
132 (dpb b2 (byte 8 8) b3))))
133 (read-length-tagged ()
134 (let ((len (aref info index)) ret)
135 (incf index)
136 (cond ((zerop len)
137 (read-codepoint))
139 (dotimes (i len)
140 (push (read-codepoint) ret))
141 (nreverse ret))))))
142 (loop until (>= index length)
143 for key = (read-codepoint)
144 for upper = (read-length-tagged)
145 for lower = (read-length-tagged)
146 for page = (aref **character-case-pages** (ash key -6))
147 for i = (+ (ash page 6) (ldb (byte 6 0) key))
149 (setf (aref unicode-table i) (cons upper lower))
150 when (and (atom upper)
151 (atom lower)
152 ;; Some characters are only equal under unicode rules,
153 ;; e.g. #\MICRO_SIGN and #\GREEK_CAPITAL_LETTER_MU
154 #!+sb-unicode
155 (both-case-index-p (misc-index (code-char lower)))
156 #!+sb-unicode
157 (both-case-index-p (misc-index (code-char upper))))
159 (setf (aref table (* i 2)) lower
160 (aref table (1+ (* i 2))) upper)))
161 (setf **character-unicode-cases** unicode-table)
162 (setf **character-cases** table))
164 (setf **character-collations**
165 (let* ((table (make-hash-table))
166 (index 0)
167 (info (make-ubn-vector ,collations 4))
168 (len (length info)))
169 (loop while (< index len) do
170 (let* ((entry-head (aref info index))
171 (cp-length (ldb (byte 4 28) entry-head))
172 (key-length (ldb (byte 5 23) entry-head))
173 (key (make-array
174 key-length
175 :element-type '(unsigned-byte 32)))
176 (codepoints nil))
177 (assert (and (/= cp-length 0) (/= key-length 0)))
178 (loop repeat cp-length do
179 (push (dpb 0 (byte 10 22) (aref info index))
180 codepoints)
181 (incf index))
182 (setf codepoints (nreverse codepoints))
183 (dotimes (i key-length)
184 (setf (aref key i) (aref info index))
185 (incf index))
186 (setf (gethash
187 (apply #'pack-3-codepoints codepoints)
188 table) key)))
189 table))))
191 ,(with-open-file
192 (stream (file "ucd-names" "lisp-expr"))
193 (with-open-file (u1-stream (file "ucd1-names" "lisp-expr"))
194 (flet ((convert-to-double-vector (vector &optional reversed)
195 (let ((result (make-array (* (length vector) 2))))
196 (loop for (code . name) across vector
197 for i by 2
199 (when reversed
200 (rotatef code name))
201 (setf (aref result i) code
202 (aref result (1+ i)) name))
203 result)))
204 (let ((names (make-hash-table))
205 (u1-names (make-hash-table)))
206 (loop
207 for code-point = (read stream nil nil)
208 for char-name = (string-upcase (read stream nil nil))
209 while code-point
210 do (setf (gethash code-point names) char-name))
211 (loop
212 for code-point = (read u1-stream nil nil)
213 for char-name = (string-upcase (read u1-stream nil nil))
214 while code-point
215 do (setf (gethash code-point u1-names) char-name))
216 (let ((tree
217 (make-huffman-tree
218 (let (list)
219 (maphash (lambda (code name)
220 (declare (ignore code))
221 (push name list))
222 names)
223 (maphash (lambda (code u1-name)
224 (declare (ignore code))
225 (push u1-name list))
226 u1-names)
227 list)))
228 (code->name
229 (make-array (hash-table-count names)
230 :fill-pointer 0))
231 (name->code nil)
232 (code->u1-name
233 (make-array (hash-table-count u1-names)
234 :fill-pointer 0))
235 (u1-name->code nil))
236 (maphash (lambda (code name)
237 (vector-push
238 (cons code (huffman-encode name tree))
239 code->name))
240 names)
241 (maphash (lambda (code name)
242 (vector-push
243 (cons code (huffman-encode name tree))
244 code->u1-name))
245 u1-names)
246 (setf name->code
247 (sort (copy-seq code->name) #'< :key #'cdr))
248 (setf code->name
249 (sort (copy-seq name->code) #'< :key #'car))
250 (setf u1-name->code
251 (sort (copy-seq code->u1-name) #'< :key #'cdr))
252 (setf code->u1-name
253 (sort (copy-seq u1-name->code) #'< :key #'car))
254 (setf names nil u1-names nil)
255 `(defun !character-name-database-cold-init ()
256 (setf **unicode-character-name-huffman-tree** ',tree
257 **unicode-char-name-database**
258 ',(convert-to-double-vector code->name)
259 **unicode-name-char-database**
260 ',(convert-to-double-vector name->code t)
261 **unicode-1-char-name-database**
262 ',(convert-to-double-vector code->u1-name)
263 **unicode-1-name-char-database**
264 ',(convert-to-double-vector u1-name->code t)))))))))))))
266 (frob))
267 #+sb-xc-host (!character-name-database-cold-init)
269 (defparameter *base-char-name-alist*
270 ;; Note: The *** markers here indicate character names which are
271 ;; required by the ANSI specification of #'CHAR-NAME. For the others,
272 ;; we prefer the ASCII standard name.
273 '((#x00 "Nul" "Null" "^@")
274 (#x01 "Soh" "^a")
275 (#x02 "Stx" "^b")
276 (#x03 "Etx" "^c")
277 (#x04 "Eot" "^d")
278 (#x05 "Enq" "^e")
279 (#x06 "Ack" "^f")
280 ;; Don't alias to Bell, another Unicode character has that name.
281 (#x07 "Bel" "^g")
282 (#x08 "Backspace" "^h" "Bs") ; *** See Note above
283 (#x09 "Tab" "^i" "Ht") ; *** See Note above
284 (#x0A "Newline" "Linefeed" "^j" "Lf" "Nl") ; *** See Note above
285 (#x0B "Vt" "^k")
286 (#x0C "Page" "^l" "Form" "Formfeed" "Ff" "Np") ; *** See Note above
287 (#x0D "Return" "^m" "Cr") ; *** See Note above
288 (#x0E "So" "^n")
289 (#x0F "Si" "^o")
290 (#x10 "Dle" "^p")
291 (#x11 "Dc1" "^q")
292 (#x12 "Dc2" "^r")
293 (#x13 "Dc3" "^s")
294 (#x14 "Dc4" "^t")
295 (#x15 "Nak" "^u")
296 (#x16 "Syn" "^v")
297 (#x17 "Etb" "^w")
298 (#x18 "Can" "^x")
299 (#x19 "Em" "^y")
300 (#x1A "Sub" "^z")
301 (#x1B "Esc" "Escape" "^[" "Altmode" "Alt")
302 (#x1C "Fs" "^\\")
303 (#x1D "Gs" "^]")
304 (#x1E "Rs" "^^")
305 (#x1F "Us" "^_")
306 (#x20 "Space" "Sp") ; *** See Note above
307 (#x7f "Rubout" "Delete" "Del")
308 (#x80 "C80")
309 (#x81 "C81")
310 (#x82 "Break-Permitted")
311 (#x83 "No-Break-Permitted")
312 (#x84 "C84")
313 (#x85 "Next-Line")
314 (#x86 "Start-Selected-Area")
315 (#x87 "End-Selected-Area")
316 (#x88 "Character-Tabulation-Set")
317 (#x89 "Character-Tabulation-With-Justification")
318 (#x8A "Line-Tabulation-Set")
319 (#x8B "Partial-Line-Forward")
320 (#x8C "Partial-Line-Backward")
321 (#x8D "Reverse-Linefeed")
322 (#x8E "Single-Shift-Two")
323 (#x8F "Single-Shift-Three")
324 (#x90 "Device-Control-String")
325 (#x91 "Private-Use-One")
326 (#x92 "Private-Use-Two")
327 (#x93 "Set-Transmit-State")
328 (#x94 "Cancel-Character")
329 (#x95 "Message-Waiting")
330 (#x96 "Start-Guarded-Area")
331 (#x97 "End-Guarded-Area")
332 (#x98 "Start-String")
333 (#x99 "C99")
334 (#x9A "Single-Character-Introducer")
335 (#x9B "Control-Sequence-Introducer")
336 (#x9C "String-Terminator")
337 (#x9D "Operating-System-Command")
338 (#x9E "Privacy-Message")
339 (#x9F "Application-Program-Command"))) ; *** See Note above
341 ;;;; UCD accessor functions
343 ;;; The character database is made of several arrays.
344 ;;; **CHARACTER-MISC-DATABASE** is an array of bytes that encode character
345 ;;; attributes. Each entry in the misc database is +misc-width+ (currently 8)
346 ;;; bytes wide. Within each entry, the bytes represent: general category, BIDI
347 ;;; class, canonical combining class, digit value, decomposition info, other
348 ;;; flags, script, line break class, and age, respectively. Several of the
349 ;;; entries have additional information encoded in them at the bit level. The
350 ;;; digit value field is equal to 128 (has only its high bit set) if characters
351 ;;; with that set of attribute are not digits. Bit 6 is set if that entry
352 ;;; encodes decimal digits, that is, characters that are DIGIT-CHAR-P. The rest
353 ;;; of the value is the digit value of characters with that entry. Decomposition
354 ;;; info contains the length of the decomposition of characters with that entry,
355 ;;; and also sets its high bit if the decompositions are compatibility
356 ;;; decompositions. The other flags byte encodes boolean properties. Bit 7 is
357 ;;; set if the entry's characters are BOTH-CASE-P in the Common Lisp sense. Bit
358 ;;; 6 is set if the entry's characters hav a defined case transformation in
359 ;;; Unicode. Bit 5 is set if the characters have the property BIDI_Mirrored=Y.
360 ;;; Bits 3-0 encode the entry's East Asian Width. Bit 4 is unused. Age stores
361 ;;; the minor version in bits 0-2, and the major version in the remaining 5
362 ;;; bits.
364 ;;; To find which entry in **CHARACTER-MISC-DATABASE** encodes a character's
365 ;;; attributes, first index **CHARACTER-HIGH-PAGES** (an array of 16-bit
366 ;;; values) with the high 13 bits of the character's codepoint. If the result
367 ;;; value has its high bit set, the character is in a "compressed page". To
368 ;;; find the misc entry number, simply clear the high bit. If the high bit is
369 ;;; not set, the misc entry number must be looked up in
370 ;;; **CHARACTER-LOW-PAGES**, which is an array of 16-bit values. Each entry in
371 ;;; the array consists of two such values, the misc entry number and the
372 ;;; decomposition index. To find the misc entry number, index into
373 ;;; **CHARACTER-LOW-PAGES** using the value retreived from
374 ;;; **CHARACTER-HIGH-PAGES** (shifted left 8 bits) plus the low 8 bits of the
375 ;;; codepoint, all times two to account for the widtth of the entries. The
376 ;;; value in **CHARACTER-LOW-PAGES** at this point is the misc entry number. To
377 ;;; transform a misc entry number into an index into
378 ;;; **CHARACTER-MISC-DATABASE**, multiply it by +misc-width*. This gives the
379 ;;; index of the start of the charater's misc entry in
380 ;;; **CHARACTER-MISC-DATABASE**.
382 ;;; To look up a character's decomposition, first retreive its
383 ;;; decomposition-info from the misc database as described above. If the
384 ;;; decomposition info is not 0, the character has a decomposition with a
385 ;;; length given by the decomposition info with the high bit (which indicates
386 ;;; compatibility/canonical status) cleared. To find the decomposition, move
387 ;;; one value past the character's misc entry number in
388 ;;; **CHARACTER-LOW-DATABASE**, which gives an index into
389 ;;; **CHARACTER-DECOMPOSITIONS**. The next LENGTH values in
390 ;;; **CHARACTER-DECOMPOSITIONS** (an array of codepoints), starting at this
391 ;;; index, are the decomposition of the character. This proceduce does not
392 ;;; apply to Hangul syllables, which have their own decomposition algorithm.
394 ;;; Case information is stored in **CHARACTER-UNICODE-CASES**, an array that
395 ;;; indirectly maps a character's codepoint to (cons uppercase
396 ;;; lowercase). Uppercase and lowercase are either a single codepoint,
397 ;;; which is the upper- or lower-case of the given character, or a
398 ;;; list of codepoints which taken as a whole are the upper- or
399 ;;; lower-case. These case lists are only used in Unicode case
400 ;;; transformations, not in Common Lisp ones.
402 ;;; **CHARACTER-CASES** is similar to the above but it stores codes in
403 ;;; a flat array twice as large, and it includes only the standard casing rules,
404 ;;; so there's always just two characters.
406 ;;; Similarly, composition information is stored in **CHARACTER-COMPOSITIONS**,
407 ;;; which is a hash table of codepoints indexed by (+ (ash codepoint1 21)
408 ;;; codepoint2).
410 (declaim (inline clear-flag))
411 (defun clear-flag (bit integer)
412 (logandc2 integer (ash 1 bit)))
414 (defconstant +misc-width+ 9)
416 (declaim (ftype (sfunction (t) (unsigned-byte 16)) misc-index))
417 (defun misc-index (char)
418 (let* ((cp (char-code char))
419 (cp-high (ash cp -8))
420 (high-index (aref **character-high-pages** cp-high)))
421 (if (logbitp 15 high-index)
422 (* +misc-width+ (clear-flag 15 high-index))
423 (* +misc-width+
424 (aref **character-low-pages**
425 (* 2 (+ (ldb (byte 8 0) cp) (ash high-index 8))))))))
427 (declaim (ftype (sfunction (t) (unsigned-byte 8)) ucd-general-category)
428 (inline ucd-general-category))
429 (defun ucd-general-category (char)
430 (aref **character-misc-database** (misc-index char)))
432 (defun ucd-decimal-digit (char)
433 (let ((digit (aref **character-misc-database**
434 (+ 3 (misc-index char)))))
435 (when (logbitp 6 digit) ; decimalp flag
436 (ldb (byte 4 0) digit))))
438 (defun char-code (char)
439 #!+sb-doc
440 "Return the integer code of CHAR."
441 (char-code char))
443 (defun char-int (char)
444 #!+sb-doc
445 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
446 there are no character bits or fonts.)"
447 (char-code char))
449 (defun code-char (code)
450 #!+sb-doc
451 "Return the character with the code CODE."
452 (code-char code))
454 (defun character (object)
455 #!+sb-doc
456 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are characters,
457 strings and symbols of length 1."
458 (flet ((do-error (control args)
459 (error 'simple-type-error
460 :datum object
461 ;;?? how to express "symbol with name of length 1"?
462 :expected-type '(or character (string 1))
463 :format-control control
464 :format-arguments args)))
465 (typecase object
466 (character object)
467 (string (if (= 1 (length (the string object)))
468 (char object 0)
469 (do-error
470 "String is not of length one: ~S" (list object))))
471 (symbol (if (= 1 (length (symbol-name object)))
472 (schar (symbol-name object) 0)
473 (do-error
474 "Symbol name is not of length one: ~S" (list object))))
475 (t (do-error "~S cannot be coerced to a character." (list object))))))
477 (defun char-name (char)
478 #!+sb-doc
479 "Return the name (a STRING) for a CHARACTER object."
480 (let ((char-code (char-code char)))
481 (or (second (assoc char-code *base-char-name-alist*))
482 (let ((h-code (double-vector-binary-search char-code
483 **unicode-char-name-database**)))
484 (cond
485 (h-code
486 (huffman-decode h-code **unicode-character-name-huffman-tree**))
488 (format nil "U~X" char-code)))))))
490 (defun name-char (name)
491 #!+sb-doc
492 "Given an argument acceptable to STRING, NAME-CHAR returns a character whose
493 name is that string, if one exists. Otherwise, NIL is returned."
494 (let ((char-code (car (rassoc-if (lambda (names)
495 (member name names :test #'string-equal))
496 *base-char-name-alist*))))
497 (cond (char-code
498 (code-char char-code))
499 ((let ((start (cond ((eql (string-not-equal "U+" name) 2)
501 ((eql (string-not-equal "U" name) 1)
502 1))))
503 (and start
504 (loop for i from start
505 below (length name)
506 always (digit-char-p (char name i) 16))
507 (code-char (parse-integer name :start start :radix 16)))))
509 (let ((encoding (huffman-encode (string-upcase name)
510 **unicode-character-name-huffman-tree**)))
511 (when encoding
512 (let ((char-code
514 (double-vector-binary-search encoding
515 **unicode-name-char-database**)
516 (double-vector-binary-search encoding
517 **unicode-1-name-char-database**))))
518 (and char-code
519 (code-char char-code)))))))))
521 ;;;; predicates
523 (defun standard-char-p (char)
524 #!+sb-doc
525 "The argument must be a character object. STANDARD-CHAR-P returns T if the
526 argument is a standard character -- one of the 95 ASCII printing characters or
527 <return>."
528 (and (typep char 'base-char)
529 (let ((n (char-code (the base-char char))))
530 (or (< 31 n 127)
531 (= n 10)))))
533 (defun %standard-char-p (thing)
534 #!+sb-doc
535 "Return T if and only if THING is a standard-char. Differs from
536 STANDARD-CHAR-P in that THING doesn't have to be a character."
537 (and (characterp thing) (standard-char-p thing)))
539 (defun graphic-char-p (char)
540 #!+sb-doc
541 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
542 argument is a printing character (space through ~ in ASCII), otherwise returns
543 NIL."
544 (let ((n (char-code char)))
545 (or (< 31 n 127)
546 (< 159 n))))
548 (defun alpha-char-p (char)
549 #!+sb-doc
550 "The argument must be a character object. ALPHA-CHAR-P returns T if the
551 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
552 (< (ucd-general-category char) 5))
554 (declaim (inline both-case-index-p))
555 (defun both-case-index-p (misc-index)
556 (declare (type (unsigned-byte 16) misc-index))
557 (logbitp 7 (aref **character-misc-database** (+ 5 misc-index))))
559 (defun both-case-p (char)
560 #!+sb-doc
561 "The argument must be a character object. BOTH-CASE-P returns T if the
562 argument is an alphabetic character and if the character exists in both upper
563 and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
564 (both-case-index-p (misc-index char)))
566 (defun upper-case-p (char)
567 #!+sb-doc
568 "The argument must be a character object; UPPER-CASE-P returns T if the
569 argument is an upper-case character, NIL otherwise."
570 (let ((index (misc-index char)))
571 (and
572 (both-case-index-p index)
573 (= (aref **character-misc-database** index) 0))))
575 (defun lower-case-p (char)
576 #!+sb-doc
577 "The argument must be a character object; LOWER-CASE-P returns T if the
578 argument is a lower-case character, NIL otherwise."
579 (let ((index (misc-index char)))
580 (and
581 (both-case-index-p index)
582 (= (aref **character-misc-database** index) 1))))
584 (defun digit-char-p (char &optional (radix 10.))
585 #!+sb-doc
586 "If char is a digit in the specified radix, returns the fixnum for which
587 that digit stands, else returns NIL."
588 (if (<= (char-code char) 127)
589 (let ((weight (- (char-code char) 48)))
590 (cond ((minusp weight) nil)
591 ((<= radix 10.)
592 ;; Special-case ASCII digits in decimal and smaller radices.
593 (if (< weight radix) weight nil))
594 ;; Digits 0 - 9 are used as is, since radix is larger.
595 ((< weight 10) weight)
596 ;; Check for upper case A - Z.
597 ((and (>= (decf weight 7) 10) (< weight radix)) weight)
598 ;; Also check lower case a - z.
599 ((and (>= (decf weight 32) 10) (< weight radix)) weight)))
600 (let ((number (ucd-decimal-digit char)))
601 (when (and number (< (truly-the fixnum number) radix))
602 number))))
604 (defun alphanumericp (char)
605 #!+sb-doc
606 "Given a character-object argument, ALPHANUMERICP returns T if the argument
607 is either numeric or alphabetic."
608 (let ((gc (ucd-general-category char)))
609 (or (< gc 5)
610 (= gc 13))))
612 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
613 ;;; which loses font, bits, and case info.
615 ;;; Return a cons with (upper-case . lower-case), where it either can
616 ;;; be a character code or a list of character codes if the character
617 ;;; donwcases or upcases into multiple characters.
618 (declaim (inline char-case-info))
619 (defun char-case-info (character)
620 (let* ((code (char-code character))
621 (page (aref **character-case-pages** (ash code -6))))
622 ;; Pages with 255 means the character is not both-case.
623 ;; **character-cases** has 0 for those characters.
624 (aref **character-unicode-cases**
625 (+ (ash page 6)
626 (ldb (byte 6 0) code)))))
628 ;;; Returns the downcased code or the character code
629 (declaim (inline equal-char-code))
630 (defun equal-char-code (char)
631 (let* ((code (char-code char))
632 (shifted (ash code -6))
633 (page (if (>= shifted (length **character-case-pages**))
634 (return-from equal-char-code code)
635 (aref **character-case-pages** shifted))))
636 (if (= page 255)
637 code
638 (let ((down-code
639 (aref **character-cases**
640 (* (+ (ash page 6)
641 (ldb (byte 6 0) code))
642 2))))
643 (if (zerop down-code)
644 code
645 down-code)))))
647 (defun two-arg-char-equal (c1 c2)
648 (flet ((base-char-equal-p ()
649 (let* ((code1 (char-code c1))
650 (code2 (char-code c2))
651 (sum (logxor code1 code2)))
652 (when (eql sum #x20)
653 (let ((sum (+ code1 code2)))
654 (or (and (< 161 sum 213))
655 (and (< 415 sum 461))
656 (and (< 463 sum 477))))))))
657 (declare (inline base-char-equal-p))
658 (or (eq c1 c2)
659 #!-sb-unicode
660 (base-char-equal-p)
661 #!+sb-unicode
662 (typecase c1
663 (base-char
664 (and (base-char-p c2)
665 (base-char-equal-p)))
667 (= (equal-char-code c1) (equal-char-code c2)))))))
669 (defun char-equal-constant (x char reverse-case-char)
670 (declare (type character x))
671 (or (eq char x)
672 (eq reverse-case-char x)))
674 (defun two-arg-char-not-equal (c1 c2)
675 (/= (equal-char-code c1) (equal-char-code c2)))
677 (macrolet ((def (name test doc)
678 (declare (ignorable doc))
679 `(defun ,name (character &rest more-characters)
680 #!+sb-doc ,doc
681 (if more-characters
682 (do ((c character (nth i more-characters))
683 (i 0 (1+ i)))
684 ((>= i (length more-characters)) t)
685 (do-rest-arg ((c2) more-characters i)
686 (when ,test
687 (return-from ,name nil))))
688 ;; CHAR-NOT-EQUAL has explicit check attribute
689 (progn (the character character) t)))))
690 (def char/= (eq c (the character c2))
691 "Return T if no two of the arguments are the same character.")
692 (def char-not-equal (two-arg-char-equal c c2)
693 "Return T if no two of the arguments are the same character.
694 Case is ignored."))
696 (defun two-arg-char-lessp (c1 c2)
697 (< (equal-char-code c1) (equal-char-code c2)))
699 (defun two-arg-char-greaterp (c1 c2)
700 (> (equal-char-code c1) (equal-char-code c2)))
702 (defun two-arg-char-not-greaterp (c1 c2)
703 (<= (equal-char-code c1) (equal-char-code c2)))
705 (defun two-arg-char-not-lessp (c1 c2)
706 (>= (equal-char-code c1) (equal-char-code c2)))
708 (macrolet ((def (op test doc)
709 (declare (ignorable doc))
710 `(defun ,op (character &rest more-characters)
711 #!+sb-doc ,doc
712 (let ((c1 character))
713 (declare (character c1))
714 (do-rest-arg ((c2 i) more-characters 0 t)
715 (if ,test
716 (setq c1 c2)
717 (return (do-rest-arg ((c) more-characters (1+ i))
718 (the character c))))))))) ; for effect
719 ;; case-sensitive
720 (def char= (eq c1 (the character c2))
721 "Return T if all of the arguments are the same character.")
722 (def char< (< (char-int c1) (char-int c2))
723 "Return T if the arguments are in strictly increasing alphabetic order.")
724 (def char> (> (char-int c1) (char-int c2))
725 "Return T if the arguments are in strictly decreasing alphabetic order.")
726 (def char<= (<= (char-int c1) (char-int c2))
727 "Return T if the arguments are in strictly non-decreasing alphabetic order.")
728 (def char>= (>= (char-int c1) (char-int c2))
729 "Return T if the arguments are in strictly non-increasing alphabetic order.")
731 ;; case-insensitive
732 (def char-equal (two-arg-char-equal c1 c2)
733 "Return T if all of the arguments are the same character.
734 Case is ignored.")
735 (def char-lessp (two-arg-char-lessp c1 c2)
736 "Return T if the arguments are in strictly increasing alphabetic order.
737 Case is ignored.")
738 (def char-greaterp (two-arg-char-greaterp c1 c2)
739 "Return T if the arguments are in strictly decreasing alphabetic order.
740 Case is ignored.")
741 (def char-not-greaterp (two-arg-char-not-greaterp c1 c2)
742 "Return T if the arguments are in strictly non-decreasing alphabetic order.
743 Case is ignored.")
744 (def char-not-lessp (two-arg-char-not-lessp c1 c2)
745 "Return T if the arguments are in strictly non-increasing alphabetic order.
746 Case is ignored."))
749 ;;;; miscellaneous functions
751 (defun char-upcase (char)
752 #!+sb-doc
753 "Return CHAR converted to upper-case if that is possible. Don't convert
754 lowercase eszet (U+DF)."
755 (let* ((code (char-code char))
756 (shifted (ash code -6))
757 (page (if (>= shifted (length **character-case-pages**))
758 (return-from char-upcase char)
759 (aref **character-case-pages** shifted))))
760 (if (= page 255)
761 char
762 (let ((code
763 (aref **character-cases**
764 (1+ (* (+ (ash page 6)
765 (ldb (byte 6 0) code))
766 2)))))
767 (if (zerop code)
768 char
769 (code-char code))))))
771 (defun char-downcase (char)
772 #!+sb-doc
773 "Return CHAR converted to lower-case if that is possible."
774 (let* ((code (char-code char))
775 (shifted (ash code -6))
776 (page (if (< shifted (length **character-case-pages**))
777 (aref **character-case-pages** shifted)
778 (return-from char-downcase char))))
779 (if (= page 255)
780 char
781 (let ((code
782 (aref **character-cases**
783 (* 2 (+ (ash page 6)
784 (ldb (byte 6 0) code))))))
785 (if (zerop code)
786 char
787 (code-char code))))))
789 (defun digit-char (weight &optional (radix 10))
790 #!+sb-doc
791 "All arguments must be integers. Returns a character object that represents
792 a digit of the given weight in the specified radix. Returns NIL if no such
793 character exists."
794 (and (typep weight 'fixnum)
795 (>= weight 0) (< weight radix) (< weight 36)
796 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))