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)
257 (declaim (ftype (sfunction (t) (unsigned-byte 8)) ucd-ccc))
258 (defun ucd-ccc (char)
259 (aref **character-database** (+ 2 (* 8 (ucd-value-0 char)))))
261 (defun char-code (char)
263 "Return the integer code of CHAR."
266 (defun char-int (char)
268 "Return the integer code of CHAR. (In SBCL this is the same as CHAR-CODE, as
269 there are no character bits or fonts.)"
272 (defun code-char (code)
274 "Return the character with the code CODE."
277 (defun character (object)
279 "Coerce OBJECT into a CHARACTER if possible. Legal inputs are characters,
280 strings and symbols of length 1."
281 (flet ((do-error (control args)
282 (error 'simple-type-error
284 ;;?? how to express "symbol with name of length 1"?
285 :expected-type '(or character (string 1))
286 :format-control control
287 :format-arguments args)))
290 (string (if (= 1 (length (the string object)))
293 "String is not of length one: ~S" (list object))))
294 (symbol (if (= 1 (length (symbol-name object)))
295 (schar (symbol-name object) 0)
297 "Symbol name is not of length one: ~S" (list object))))
298 (t (do-error "~S cannot be coerced to a character." (list object))))))
300 (defun char-name (char)
302 "Return the name (a STRING) for a CHARACTER object."
303 (let ((char-code (char-code char)))
304 (or (second (assoc char-code *base-char-name-alist*))
306 (let ((h-code (cdr (binary-search char-code
307 (car *unicode-character-name-database*)
311 (huffman-decode h-code *unicode-character-name-huffman-tree*))
312 ((< char-code #x10000)
313 (format nil "U~4,'0X" char-code))
315 (format nil "U~8,'0X" char-code)))))))
317 (defun name-char (name)
319 "Given an argument acceptable to STRING, NAME-CHAR returns a character whose
320 name is that string, if one exists. Otherwise, NIL is returned."
321 (or (let ((char-code (car (rassoc-if (lambda (names)
322 (member name names :test #'string-equal))
323 *base-char-name-alist*))))
325 (code-char char-code)))
327 (let ((encoding (huffman-encode (string-upcase name)
328 *unicode-character-name-huffman-tree*)))
331 (car (binary-search encoding
332 (cdr *unicode-character-name-database*)
334 (name-string (string name))
335 (name-length (length name-string)))
338 (code-char char-code))
339 ((and (or (= name-length 9)
341 (char-equal (char name-string 0) #\U)
342 (loop for i from 1 below name-length
343 always (digit-char-p (char name-string i) 16)))
344 (code-char (parse-integer name-string :start 1 :radix 16)))
350 (defun standard-char-p (char)
352 "The argument must be a character object. STANDARD-CHAR-P returns T if the
353 argument is a standard character -- one of the 95 ASCII printing characters or
355 (and (typep char 'base-char)
356 (let ((n (char-code (the base-char char))))
360 (defun %standard-char-p (thing)
362 "Return T if and only if THING is a standard-char. Differs from
363 STANDARD-CHAR-P in that THING doesn't have to be a character."
364 (and (characterp thing) (standard-char-p thing)))
366 (defun graphic-char-p (char)
368 "The argument must be a character object. GRAPHIC-CHAR-P returns T if the
369 argument is a printing character (space through ~ in ASCII), otherwise returns
371 (let ((n (char-code char)))
375 (defun alpha-char-p (char)
377 "The argument must be a character object. ALPHA-CHAR-P returns T if the
378 argument is an alphabetic character, A-Z or a-z; otherwise NIL."
379 (< (ucd-general-category char) 5))
381 (defun upper-case-p (char)
383 "The argument must be a character object; UPPER-CASE-P returns T if the
384 argument is an upper-case character, NIL otherwise."
385 (< (ucd-value-0 char) 4))
387 (defun lower-case-p (char)
389 "The argument must be a character object; LOWER-CASE-P returns T if the
390 argument is a lower-case character, NIL otherwise."
391 (< 3 (ucd-value-0 char) 8))
393 (defun both-case-p (char)
395 "The argument must be a character object. BOTH-CASE-P returns T if the
396 argument is an alphabetic character and if the character exists in both upper
397 and lower case. For ASCII, this is the same as ALPHA-CHAR-P."
398 (< (ucd-value-0 char) 8))
400 (defun digit-char-p (char &optional (radix 10.))
402 "If char is a digit in the specified radix, returns the fixnum for which
403 that digit stands, else returns NIL."
404 (let ((m (- (char-code char) 48)))
406 (cond ((<= radix 10.)
407 ;; Special-case decimal and smaller radices.
408 (if (and (>= m 0) (< m radix)) m nil))
409 ;; Digits 0 - 9 are used as is, since radix is larger.
410 ((and (>= m 0) (< m 10)) m)
411 ;; Check for upper case A - Z.
412 ((and (>= (setq m (- m 7)) 10) (< m radix)) m)
413 ;; Also check lower case a - z.
414 ((and (>= (setq m (- m 32)) 10) (< m radix)) m)
416 (t (let ((number (ucd-decimal-digit char)))
417 (when (and number (< number radix))
420 (defun alphanumericp (char)
422 "Given a character-object argument, ALPHANUMERICP returns T if the argument
423 is either numeric or alphabetic."
424 (let ((gc (ucd-general-category char)))
428 (defun char= (character &rest more-characters)
430 "Return T if all of the arguments are the same character."
431 (declare (truly-dynamic-extent more-characters))
432 (dolist (c more-characters t)
433 (declare (type character c))
434 (unless (eq c character) (return nil))))
436 (defun char/= (character &rest more-characters)
438 "Return T if no two of the arguments are the same character."
439 (declare (truly-dynamic-extent more-characters))
440 (do* ((head character (car list))
441 (list more-characters (cdr list)))
443 (declare (type character head))
445 (declare (type character c))
446 (when (eq head c) (return-from char/= nil)))))
448 (defun char< (character &rest more-characters)
450 "Return T if the arguments are in strictly increasing alphabetic order."
451 (declare (truly-dynamic-extent more-characters))
452 (do* ((c character (car list))
453 (list more-characters (cdr list)))
455 (unless (< (char-int c)
456 (char-int (car list)))
459 (defun char> (character &rest more-characters)
461 "Return T if the arguments are in strictly decreasing alphabetic order."
462 (declare (truly-dynamic-extent more-characters))
463 (do* ((c character (car list))
464 (list more-characters (cdr list)))
466 (unless (> (char-int c)
467 (char-int (car list)))
470 (defun char<= (character &rest more-characters)
472 "Return T if the arguments are in strictly non-decreasing alphabetic order."
473 (declare (truly-dynamic-extent more-characters))
474 (do* ((c character (car list))
475 (list more-characters (cdr list)))
477 (unless (<= (char-int c)
478 (char-int (car list)))
481 (defun char>= (character &rest more-characters)
483 "Return T if the arguments are in strictly non-increasing alphabetic order."
484 (declare (truly-dynamic-extent more-characters))
485 (do* ((c character (car list))
486 (list more-characters (cdr list)))
488 (unless (>= (char-int c)
489 (char-int (car list)))
492 ;;; EQUAL-CHAR-CODE is used by the following functions as a version of CHAR-INT
493 ;;; which loses font, bits, and case info.
495 (defmacro equal-char-code (character)
497 `(let ((,ch ,character))
498 (if (= (ucd-value-0 ,ch) 0)
502 (defun two-arg-char-equal (c1 c2)
503 (= (equal-char-code c1) (equal-char-code c2)))
505 (defun char-equal (character &rest more-characters)
507 "Return T if all of the arguments are the same character.
509 (declare (truly-dynamic-extent more-characters))
510 (do ((clist more-characters (cdr clist)))
512 (unless (two-arg-char-equal (car clist) character)
515 (defun two-arg-char-not-equal (c1 c2)
516 (/= (equal-char-code c1) (equal-char-code c2)))
518 (defun char-not-equal (character &rest more-characters)
520 "Return T if no two of the arguments are the same character.
522 (declare (truly-dynamic-extent more-characters))
523 (do* ((head character (car list))
524 (list more-characters (cdr list)))
526 (unless (do* ((l list (cdr l)))
528 (if (two-arg-char-equal head (car l))
532 (defun two-arg-char-lessp (c1 c2)
533 (< (equal-char-code c1) (equal-char-code c2)))
535 (defun char-lessp (character &rest more-characters)
537 "Return T if the arguments are in strictly increasing alphabetic order.
539 (declare (truly-dynamic-extent more-characters))
540 (do* ((c character (car list))
541 (list more-characters (cdr list)))
543 (unless (two-arg-char-lessp c (car list))
546 (defun two-arg-char-greaterp (c1 c2)
547 (> (equal-char-code c1) (equal-char-code c2)))
549 (defun char-greaterp (character &rest more-characters)
551 "Return T if the arguments are in strictly decreasing alphabetic order.
553 (declare (truly-dynamic-extent more-characters))
554 (do* ((c character (car list))
555 (list more-characters (cdr list)))
557 (unless (two-arg-char-greaterp c (car list))
560 (defun two-arg-char-not-greaterp (c1 c2)
561 (<= (equal-char-code c1) (equal-char-code c2)))
563 (defun char-not-greaterp (character &rest more-characters)
565 "Return T if the arguments are in strictly non-decreasing alphabetic order.
567 (declare (truly-dynamic-extent more-characters))
568 (do* ((c character (car list))
569 (list more-characters (cdr list)))
571 (unless (two-arg-char-not-greaterp c (car list))
574 (defun two-arg-char-not-lessp (c1 c2)
575 (>= (equal-char-code c1) (equal-char-code c2)))
577 (defun char-not-lessp (character &rest more-characters)
579 "Return T if the arguments are in strictly non-increasing alphabetic order.
581 (declare (truly-dynamic-extent more-characters))
582 (do* ((c character (car list))
583 (list more-characters (cdr list)))
585 (unless (two-arg-char-not-lessp c (car list))
588 ;;;; miscellaneous functions
590 (defun char-upcase (char)
592 "Return CHAR converted to upper-case if that is possible. Don't convert
593 lowercase eszet (U+DF)."
594 (if (< 3 (ucd-value-0 char) 8)
595 (code-char (ucd-value-1 char))
598 (defun char-downcase (char)
600 "Return CHAR converted to lower-case if that is possible."
601 (if (< (ucd-value-0 char) 4)
602 (code-char (ucd-value-1 char))
605 (defun digit-char (weight &optional (radix 10))
607 "All arguments must be integers. Returns a character object that represents
608 a digit of the given weight in the specified radix. Returns NIL if no such
610 (and (typep weight 'fixnum)
611 (>= weight 0) (< weight radix) (< weight 36)
612 (code-char (if (< weight 10) (+ 48 weight) (+ 55 weight)))))
614 (defun char-decomposition-info (char)
615 (aref **character-database** (+ 6 (* 8 (ucd-value-0 char)))))
617 (defun char-decomposition (char)
618 (let* ((cp (char-code char))
619 (cp-high (ash cp -8))
620 (decompositions **character-decompositions**)
621 (long-decompositions **character-long-decompositions**)
623 (ash (aref decompositions cp-high) 10)
624 (ash (ldb (byte 8 0) cp) 2)))
625 (v0 (aref decompositions index))
626 (v1 (aref decompositions (+ index 1)))
627 (v2 (aref decompositions (+ index 2)))
628 (v3 (aref decompositions (+ index 3)))
629 (length (dpb v0 (byte 8 3) (ldb (byte 3 5) v1)))
630 (entry (dpb (ldb (byte 5 0) v1) (byte 5 16)
631 (dpb v2 (byte 8 8) v3))))
633 (string (code-char entry))
634 (if (<= #xac00 cp #xd7a3)
635 ;; see Unicode 6.2, section 3-12
636 (let* ((sbase #xac00)
643 (ncount (* vcount tcount))
644 (scount (* lcount ncount))
645 (sindex (- cp sbase))
646 (lindex (floor sindex ncount))
647 (vindex (floor (mod sindex ncount) tcount))
648 (tindex (mod sindex tcount))
649 (result (make-string length)))
650 (declare (ignore scount))
651 (setf (char result 0) (code-char (+ lbase lindex)))
652 (setf (char result 1) (code-char (+ vbase vindex)))
654 (setf (char result 2) (code-char (+ tbase tindex))))
656 (let ((result (make-string length))
658 (dotimes (i length result)
659 (let ((code (dpb (aref long-decompositions (+ e 1))
661 (dpb (aref long-decompositions (+ e 2))
663 (aref long-decompositions (+ e 3))))))
664 (setf (char result i) (code-char code)))
667 (defun decompose-char (char)
668 (if (= (char-decomposition-info char) 0)
670 (char-decomposition char)))
672 (defun decompose-string (string &optional (kind :canonical))
673 (declare (type (member :canonical :compatibility) kind))
674 (flet ((canonical (char)
675 (= 1 (char-decomposition-info char)))
677 (/= 0 (char-decomposition-info char))))
680 (:canonical #'canonical)
681 (:compatibility #'compat))))
682 (do* ((start 0 (1+ end))
683 (end (position-if fun string :start start)
684 (position-if fun string :start start)))
685 ((null end) (push (subseq string start end) result))
686 (unless (= start end)
687 (push (subseq string start end) result))
688 (push (decompose-char (char string end)) result))
689 (apply 'concatenate 'string (nreverse result)))))
691 (defun sort-combiners (string)
692 (let (result (start 0) first-cc first-non-cc)
695 (setf first-cc (position 0 string :key #'ucd-ccc :test #'/= :start start))
697 (setf first-non-cc (position 0 string :key #'ucd-ccc :test #'= :start first-cc)))
698 (push (subseq string start first-cc) result)
700 (push (stable-sort (subseq string first-cc first-non-cc) #'< :key #'ucd-ccc) result))
702 (setf start first-non-cc first-cc nil first-non-cc nil)
704 (apply 'concatenate 'string (nreverse result))))
707 (defun primary-composition (char1 char2)
708 (when (and (char= char1 #\e)
709 (char= char2 #\combining_acute_accent))
710 #\latin_small_letter_e_with_acute))
712 ;;; This implements a sequence data structure, specialized for
713 ;;; efficient deletion of characters at an index, along with tolerable
714 ;;; random access. The purpose is to support the canonical
715 ;;; composition algorithm from Unicode, which involves replacing (not
716 ;;; necessarily consecutive) pairs of code points with a single code
717 ;;; point (e.g. [#\e #\combining_acute_accent] with
718 ;;; #\latin_small_letter_e_with_acute). The data structure is a list
719 ;;; of three-element lists, each denoting a chunk of string data
720 ;;; starting at the first index and ending at the second.
722 ;;; Actually, the implementation isn't particularly efficient, and
723 ;;; would probably benefit from being rewritten in terms of displaced
724 ;;; arrays, which would substantially reduce copying.
726 ;;; (also, generic sequences. *sigh*.)
727 (defun lref (lstring index)
729 (when (and (<= (first l) index)
730 (< index (second l)))
731 (return (aref (third l) (- index (first l)))))))
732 (defun (setf lref) (newchar lstring index)
734 (when (and (<= (first l) index)
735 (< index (second l)))
736 (return (setf (aref (third l) (- index (first l))) newchar)))))
737 (defun llength (lstring)
738 (second (first (last lstring))))
739 (defun lstring (lstring)
740 (let ((result (make-string (llength lstring))))
741 (dolist (l lstring result)
742 (replace result (third l) :start1 (first l) :end1 (second l)))))
743 (defun ldelete (lstring index)
744 (do* ((ls lstring (cdr ls))
745 (l (car ls) (car ls))
747 ((and (<= (first l) index)
748 (< index (second l)))
753 (list (list (first l) (1- (second l)) (subseq (third l) 1))))
754 ((= index (1- (second l)))
755 (list (list (first l) (1- (second l)) (subseq (third l) 0 (1- (length (third l)))))))
758 (list (first l) index
759 (subseq (third l) 0 (- index (first l))))
760 (list index (1- (second l))
761 (subseq (third l) (1+ (- index (first l))))))))
762 (mapcar (lambda (x) (list (1- (first x)) (1- (second x)) (third x)))
766 (defun canonically-compose (string)
768 (let* ((result (list (list 0 (length string) string)))
769 (previous-starter-index (position 0 string :key #'ucd-ccc))
770 (i (1+ previous-starter-index)))
771 (when (= i (length string))
772 (return-from canonically-compose string))
775 (when (and (> (- i previous-starter-index) 2)
776 (= (ucd-ccc (lref result i)) (ucd-ccc (lref result (1- i)))))
777 (when (= (ucd-ccc (lref result i)) 0)
778 (setf previous-starter-index i))
782 (let ((comp (primary-composition (lref result previous-starter-index)
786 (setf (lref result previous-starter-index) comp)
787 (setf result (ldelete result i)))
789 (when (= (ucd-ccc (lref result i)) 0)
790 (setf previous-starter-index i))
793 (unless (= i (llength result))
795 (if (= i (length string))
799 (defun normalize-string (string &optional (form :nfd))
800 (declare (type (member :nfd :nfkd :nfc :nfkc) form))
804 ((or (array character (*)) #!-sb-unicode base-string)
807 (sort-combiners (decompose-string string)))
809 (sort-combiners (decompose-string string :compatibility)))))
810 ((array nil (*)) string)))