Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / target-char.lisp
blob437a20f866a2c57630eda447f5c9b015b934dc20
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 (defun ucd-general-category (char)
429 (aref **character-misc-database** (misc-index char)))
431 (defun ucd-decimal-digit (char)
432 (let ((digit (aref **character-misc-database**
433 (+ 3 (misc-index char)))))
434 (when (logbitp 6 digit) ; decimalp flag
435 (ldb (byte 4 0) digit))))
437 (defun char-code (char)
438 #!+sb-doc
439 "Return the integer code of CHAR."
440 (char-code char))
442 (defun char-int (char)
443 #!+sb-doc
444 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
445 there are no character bits or fonts.)"
446 (char-code char))
448 (defun code-char (code)
449 #!+sb-doc
450 "Return the character with the code CODE."
451 (code-char code))
453 (defun character (object)
454 #!+sb-doc
455 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are characters,
456 strings and symbols of length 1."
457 (flet ((do-error (control args)
458 (error 'simple-type-error
459 :datum object
460 ;;?? how to express "symbol with name of length 1"?
461 :expected-type '(or character (string 1))
462 :format-control control
463 :format-arguments args)))
464 (typecase object
465 (character object)
466 (string (if (= 1 (length (the string object)))
467 (char object 0)
468 (do-error
469 "String is not of length one: ~S" (list object))))
470 (symbol (if (= 1 (length (symbol-name object)))
471 (schar (symbol-name object) 0)
472 (do-error
473 "Symbol name is not of length one: ~S" (list object))))
474 (t (do-error "~S cannot be coerced to a character." (list object))))))
476 (defun char-name (char)
477 #!+sb-doc
478 "Return the name (a STRING) for a CHARACTER object."
479 (let ((char-code (char-code char)))
480 (or (second (assoc char-code *base-char-name-alist*))
481 (let ((h-code (double-vector-binary-search char-code
482 **unicode-char-name-database**)))
483 (cond
484 (h-code
485 (huffman-decode h-code **unicode-character-name-huffman-tree**))
487 (format nil "U~X" char-code)))))))
489 (defun name-char (name)
490 #!+sb-doc
491 "Given an argument acceptable to STRING, NAME-CHAR returns a character whose
492 name is that string, if one exists. Otherwise, NIL is returned."
493 (let ((char-code (car (rassoc-if (lambda (names)
494 (member name names :test #'string-equal))
495 *base-char-name-alist*))))
496 (cond (char-code
497 (code-char char-code))
498 ((let ((start (cond ((eql (string-not-equal "U+" name) 2)
500 ((eql (string-not-equal "U" name) 1)
501 1))))
502 (and start
503 (loop for i from start
504 below (length name)
505 always (digit-char-p (char name i) 16))
506 (code-char (parse-integer name :start start :radix 16)))))
508 (let ((encoding (huffman-encode (string-upcase name)
509 **unicode-character-name-huffman-tree**)))
510 (when encoding
511 (let ((char-code
513 (double-vector-binary-search encoding
514 **unicode-name-char-database**)
515 (double-vector-binary-search encoding
516 **unicode-1-name-char-database**))))
517 (and char-code
518 (code-char char-code)))))))))
520 ;;;; predicates
522 (defun standard-char-p (char)
523 #!+sb-doc
524 "The argument must be a character object. STANDARD-CHAR-P returns T if the
525 argument is a standard character -- one of the 95 ASCII printing characters or
526 <return>."
527 (and (typep char 'base-char)
528 (let ((n (char-code (the base-char char))))
529 (or (< 31 n 127)
530 (= n 10)))))
532 (defun %standard-char-p (thing)
533 #!+sb-doc
534 "Return T if and only if THING is a standard-char. Differs from
535 STANDARD-CHAR-P in that THING doesn't have to be a character."
536 (and (characterp thing) (standard-char-p thing)))
538 (defun graphic-char-p (char)
539 #!+sb-doc
540 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
541 argument is a printing character (space through ~ in ASCII), otherwise returns
542 NIL."
543 (let ((n (char-code char)))
544 (or (< 31 n 127)
545 (< 159 n))))
547 (defun alpha-char-p (char)
548 #!+sb-doc
549 "The argument must be a character object. ALPHA-CHAR-P returns T if the
550 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
551 (< (ucd-general-category char) 5))
553 (declaim (inline both-case-index-p))
554 (defun both-case-index-p (misc-index)
555 (declare (type (unsigned-byte 16) misc-index))
556 (logbitp 7 (aref **character-misc-database** (+ 5 misc-index))))
558 (defun both-case-p (char)
559 #!+sb-doc
560 "The argument must be a character object. BOTH-CASE-P returns T if the
561 argument is an alphabetic character and if the character exists in both upper
562 and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
563 (both-case-index-p (misc-index char)))
565 (defun upper-case-p (char)
566 #!+sb-doc
567 "The argument must be a character object; UPPER-CASE-P returns T if the
568 argument is an upper-case character, NIL otherwise."
569 (let ((index (misc-index char)))
570 (and
571 (both-case-index-p index)
572 (= (aref **character-misc-database** index) 0))))
574 (defun lower-case-p (char)
575 #!+sb-doc
576 "The argument must be a character object; LOWER-CASE-P returns T if the
577 argument is a lower-case character, NIL otherwise."
578 (let ((index (misc-index char)))
579 (and
580 (both-case-index-p index)
581 (= (aref **character-misc-database** index) 1))))
583 (defun digit-char-p (char &optional (radix 10.))
584 #!+sb-doc
585 "If char is a digit in the specified radix, returns the fixnum for which
586 that digit stands, else returns NIL."
587 (if (<= (char-code char) 127)
588 (let ((weight (- (char-code char) 48)))
589 (cond ((minusp weight) nil)
590 ((<= radix 10.)
591 ;; Special-case ASCII digits in decimal and smaller radices.
592 (if (< weight radix) weight nil))
593 ;; Digits 0 - 9 are used as is, since radix is larger.
594 ((< weight 10) weight)
595 ;; Check for upper case A - Z.
596 ((and (>= (decf weight 7) 10) (< weight radix)) weight)
597 ;; Also check lower case a - z.
598 ((and (>= (decf weight 32) 10) (< weight radix)) weight)))
599 (let ((number (ucd-decimal-digit char)))
600 (when (and number (< (truly-the fixnum number) radix))
601 number))))
603 (defun alphanumericp (char)
604 #!+sb-doc
605 "Given a character-object argument, ALPHANUMERICP returns T if the argument
606 is either numeric or alphabetic."
607 (let ((gc (ucd-general-category char)))
608 (or (< gc 5)
609 (= gc 13))))
611 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
612 ;;; which loses font, bits, and case info.
614 ;;; Return a cons with (upper-case . lower-case), where it either can
615 ;;; be a character code or a list of character codes if the character
616 ;;; donwcases or upcases into multiple characters.
617 (declaim (inline char-case-info))
618 (defun char-case-info (character)
619 (let* ((code (char-code character))
620 (page (aref **character-case-pages** (ash code -6))))
621 ;; Pages with 255 means the character is not both-case.
622 ;; **character-cases** has 0 for those characters.
623 (aref **character-unicode-cases**
624 (+ (ash page 6)
625 (ldb (byte 6 0) code)))))
627 ;;; Returns the downcased code or the character code
628 (declaim (inline equal-char-code))
629 (defun equal-char-code (char)
630 (let* ((code (char-code char))
631 (shifted (ash code -6))
632 (page (if (>= shifted (length **character-case-pages**))
633 (return-from equal-char-code code)
634 (aref **character-case-pages** shifted))))
635 (if (= page 255)
636 code
637 (let ((down-code
638 (aref **character-cases**
639 (* (+ (ash page 6)
640 (ldb (byte 6 0) code))
641 2))))
642 (if (zerop down-code)
643 code
644 down-code)))))
646 (defun two-arg-char-equal (c1 c2)
647 (flet ((base-char-equal-p ()
648 (let* ((code1 (char-code c1))
649 (code2 (char-code c2))
650 (sum (logxor code1 code2)))
651 (when (eql sum #x20)
652 (let ((sum (+ code1 code2)))
653 (or (and (< 161 sum 213))
654 (and (< 415 sum 461))
655 (and (< 463 sum 477))))))))
656 (declare (inline base-char-equal-p))
657 (or (eq c1 c2)
658 #!-sb-unicode
659 (base-char-equal-p)
660 #!+sb-unicode
661 (typecase c1
662 (base-char
663 (and (base-char-p c2)
664 (base-char-equal-p)))
666 (= (equal-char-code c1) (equal-char-code c2)))))))
668 (defun char-equal-constant (x char reverse-case-char)
669 (declare (type character x))
670 (or (eq char x)
671 (eq reverse-case-char x)))
673 (defun two-arg-char-not-equal (c1 c2)
674 (/= (equal-char-code c1) (equal-char-code c2)))
676 (macrolet ((def (name test doc)
677 (declare (ignorable doc))
678 `(defun ,name (character &rest more-characters)
679 #!+sb-doc ,doc
680 (if more-characters
681 (do ((c character (nth i more-characters))
682 (i 0 (1+ i)))
683 ((>= i (length more-characters)) t)
684 (do-rest-arg ((c2) more-characters i)
685 (when ,test
686 (return-from ,name nil))))
687 ;; CHAR-NOT-EQUAL has explicit check attribute
688 (progn (the character character) t)))))
689 (def char/= (eq c (the character c2))
690 "Return T if no two of the arguments are the same character.")
691 (def char-not-equal (two-arg-char-equal c c2)
692 "Return T if no two of the arguments are the same character.
693 Case is ignored."))
695 (defun two-arg-char-lessp (c1 c2)
696 (< (equal-char-code c1) (equal-char-code c2)))
698 (defun two-arg-char-greaterp (c1 c2)
699 (> (equal-char-code c1) (equal-char-code c2)))
701 (defun two-arg-char-not-greaterp (c1 c2)
702 (<= (equal-char-code c1) (equal-char-code c2)))
704 (defun two-arg-char-not-lessp (c1 c2)
705 (>= (equal-char-code c1) (equal-char-code c2)))
707 (macrolet ((def (op test doc)
708 (declare (ignorable doc))
709 `(defun ,op (character &rest more-characters)
710 #!+sb-doc ,doc
711 (let ((c1 character))
712 (declare (character c1))
713 (do-rest-arg ((c2 i) more-characters 0 t)
714 (if ,test
715 (setq c1 c2)
716 (return (do-rest-arg ((c) more-characters (1+ i))
717 (the character c))))))))) ; for effect
718 ;; case-sensitive
719 (def char= (eq c1 (the character c2))
720 "Return T if all of the arguments are the same character.")
721 (def char< (< (char-int c1) (char-int c2))
722 "Return T if the arguments are in strictly increasing alphabetic order.")
723 (def char> (> (char-int c1) (char-int c2))
724 "Return T if the arguments are in strictly decreasing alphabetic order.")
725 (def char<= (<= (char-int c1) (char-int c2))
726 "Return T if the arguments are in strictly non-decreasing alphabetic order.")
727 (def char>= (>= (char-int c1) (char-int c2))
728 "Return T if the arguments are in strictly non-increasing alphabetic order.")
730 ;; case-insensitive
731 (def char-equal (two-arg-char-equal c1 c2)
732 "Return T if all of the arguments are the same character.
733 Case is ignored.")
734 (def char-lessp (two-arg-char-lessp c1 c2)
735 "Return T if the arguments are in strictly increasing alphabetic order.
736 Case is ignored.")
737 (def char-greaterp (two-arg-char-greaterp c1 c2)
738 "Return T if the arguments are in strictly decreasing alphabetic order.
739 Case is ignored.")
740 (def char-not-greaterp (two-arg-char-not-greaterp c1 c2)
741 "Return T if the arguments are in strictly non-decreasing alphabetic order.
742 Case is ignored.")
743 (def char-not-lessp (two-arg-char-not-lessp c1 c2)
744 "Return T if the arguments are in strictly non-increasing alphabetic order.
745 Case is ignored."))
748 ;;;; miscellaneous functions
750 (defun char-upcase (char)
751 #!+sb-doc
752 "Return CHAR converted to upper-case if that is possible. Don't convert
753 lowercase eszet (U+DF)."
754 (let* ((code (char-code char))
755 (shifted (ash code -6))
756 (page (if (>= shifted (length **character-case-pages**))
757 (return-from char-upcase char)
758 (aref **character-case-pages** shifted))))
759 (if (= page 255)
760 char
761 (let ((code
762 (aref **character-cases**
763 (1+ (* (+ (ash page 6)
764 (ldb (byte 6 0) code))
765 2)))))
766 (if (zerop code)
767 char
768 (code-char code))))))
770 (defun char-downcase (char)
771 #!+sb-doc
772 "Return CHAR converted to lower-case if that is possible."
773 (let* ((code (char-code char))
774 (shifted (ash code -6))
775 (page (if (< shifted (length **character-case-pages**))
776 (aref **character-case-pages** shifted)
777 (return-from char-downcase char))))
778 (if (= page 255)
779 char
780 (let ((code
781 (aref **character-cases**
782 (* 2 (+ (ash page 6)
783 (ldb (byte 6 0) code))))))
784 (if (zerop code)
785 char
786 (code-char code))))))
788 (defun digit-char (weight &optional (radix 10))
789 #!+sb-doc
790 "All arguments must be integers. Returns a character object that represents
791 a digit of the given weight in the specified radix. Returns NIL if no such
792 character exists."
793 (and (typep weight 'fixnum)
794 (>= weight 0) (< weight radix) (< weight 36)
795 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))