Optimize sb-unicode:normalize-string.
[sbcl.git] / src / code / target-unicode.lisp
blob8c2c4d21b95d42b5ce86b8c85cd74ba442db2c31
1 ;;;; Unicode 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!UNICODE")
14 (declaim (type simple-vector **special-numerics**))
15 (sb!impl::defglobal **special-numerics**
16 #.(with-open-file (stream
17 (merge-pathnames
18 (make-pathname
19 :directory
20 '(:relative :up :up "output")
21 :name "numerics" :type "lisp-expr")
22 sb!xc:*compile-file-truename*)
23 :direction :input
24 :element-type 'character)
25 (read stream)))
28 (declaim (type (simple-array (unsigned-byte 32) (*)) **block-ranges**))
29 (sb!impl::defglobal **block-ranges**
30 #.(sb!int:!coerce-to-specialized
31 (with-open-file (stream
32 (merge-pathnames
33 (make-pathname
34 :directory
35 '(:relative :up :up "output")
36 :name "blocks" :type "lisp-expr")
37 sb!xc:*compile-file-truename*)
38 :direction :input
39 :element-type 'character)
40 (read stream))
41 '(unsigned-byte 32)))
43 (macrolet ((unicode-property-init ()
44 (let ((proplist-dump
45 (with-open-file (stream
46 (merge-pathnames
47 (make-pathname
48 :directory
49 '(:relative :up :up "output")
50 :name "misc-properties" :type "lisp-expr")
51 sb!xc:*compile-file-truename*)
52 :direction :input
53 :element-type 'character)
54 (read stream)))
55 (confusable-sets
56 (with-open-file (stream
57 (merge-pathnames
58 (make-pathname
59 :directory
60 '(:relative :up :up "output")
61 :name "confusables" :type "lisp-expr")
62 sb!xc:*compile-file-truename*)
63 :direction :input
64 :element-type 'character)
65 (read stream)))
66 (bidi-mirroring-list
67 (with-open-file (stream
68 (merge-pathnames
69 (make-pathname
70 :directory
71 '(:relative :up :up "output")
72 :name "bidi-mirrors" :type "lisp-expr")
73 sb!xc:*compile-file-truename*)
74 :direction :input
75 :element-type 'character)
76 (read stream))))
77 `(progn
78 (sb!impl::defglobal **proplist-properties** ',proplist-dump)
79 (sb!impl::defglobal **confusables** ',confusable-sets)
80 (sb!impl::defglobal **bidi-mirroring-glyphs** ',bidi-mirroring-list)
81 (defun !unicode-properties-cold-init ()
82 (let ((hash (make-hash-table)) (list ',proplist-dump))
83 (do ((k (car list) (car list)) (v (cadr list) (cadr list)))
84 ((not list) hash)
85 (setf (gethash k hash) v)
86 (setf list (cddr list)))
87 (setf **proplist-properties** hash))
88 (let ((hash (make-hash-table :test #'equal)))
89 (loop for set in ',confusable-sets
90 for items = (mapcar #'(lambda (item)
91 (map 'simple-string
92 #'code-char item))
93 #!+sb-unicode set
94 #!-sb-unicode
95 (remove-if-not
96 #'(lambda (item)
97 (every
98 #'(lambda (x)
99 (< x sb!xc:char-code-limit))
100 item)) set))
101 do (dolist (i items)
102 (setf (gethash i hash) (first items))))
103 (setf **confusables** hash))
104 (let ((hash (make-hash-table)) (list ',bidi-mirroring-list))
105 (loop for (k v) in list do
106 (setf (gethash k hash) v))
107 (setf **bidi-mirroring-glyphs** hash)))))))
108 (unicode-property-init))
110 ;;; Unicode property access
111 (defun ordered-ranges-member (item vector)
112 (declare (type simple-vector vector)
113 (type fixnum item)
114 (optimize speed))
115 (labels ((recurse (start end)
116 (declare (type index start end))
117 (when (< start end)
118 (let* ((i (+ start (truncate (- end start) 2)))
119 (index (* 2 i))
120 (elt1 (svref vector index))
121 (elt2 (svref vector (1+ index))))
122 (declare (type index i)
123 (fixnum elt1 elt2))
124 (cond ((< item elt1)
125 (recurse start i))
126 ((> item elt2)
127 (recurse (+ 1 i) end))
129 item))))))
130 (recurse 0 (truncate (length vector) 2))))
132 ;; Returns which range `item` was found in or NIL
133 ;; First range = 0, second range = 1 ...
134 (defun ordered-ranges-position (item vector)
135 (declare (type (simple-array (unsigned-byte 32) (*)) vector)
136 (type fixnum item))
137 (labels ((recurse (start end)
138 (declare (type index start end))
139 (when (< start end)
140 (let* ((i (+ start (truncate (- end start) 2)))
141 (index (* 2 i))
142 (elt1 (aref vector index))
143 (elt2 (aref vector (1+ index))))
144 (declare (type index i))
145 (cond ((< item elt1)
146 (recurse start i))
147 ((> item elt2)
148 (recurse (+ 1 i) end))
150 i))))))
151 (recurse 0 (truncate (length vector) 2))))
153 (defun proplist-p (character property)
154 #!+sb-doc
155 "Returns T if CHARACTER has the specified PROPERTY.
156 PROPERTY is a keyword representing one of the properties from PropList.txt,
157 with underscores replaced by dashes."
158 (ordered-ranges-member (char-code character)
159 (gethash property **proplist-properties**)))
161 ;; WARNING: These have to be manually kept in sync with the values in ucd.lisp
162 (declaim (type simple-vector *general-categories* *bidi-classes* *east-asian-widths*
163 *scripts* *line-break-classes* *blocks*))
164 (sb!impl::defglobal *general-categories*
165 #(:Lu :Ll :Lt :Lm :Lo :Cc :Cf :Co :Cs :Cn :Mc :Me :Mn :Nd
166 :Nl :No :Pc :Pd :Pe :Pf :Pi :Po :Ps :Sc :Sk :Sm :So :Zl
167 :Zp :Zs))
169 (sb!impl::defglobal *bidi-classes*
170 #(:BN :AL :AN :B :CS :EN :ES :ET :L :LRE :LRO :NSM :ON
171 :PDF :R :RLE :RLO :S :WS :LRI :RLI :FSI :PDI))
173 (sb!impl::defglobal *east-asian-widths*
174 #(:N :A :H :W :F :Na))
176 (sb!impl::defglobal *scripts*
177 #(:Unknown :Common :Latin :Greek :Cyrillic :Armenian :Hebrew :Arabic :Syriac
178 :Thaana :Devanagari :Bengali :Gurmukhi :Gujarati :Oriya :Tamil :Telugu
179 :Kannada :Malayalam :Sinhala :Thai :Lao :Tibetan :Myanmar :Georgian :Hangul
180 :Ethiopic :Cherokee :Canadian-Aboriginal :Ogham :Runic :Khmer :Mongolian
181 :Hiragana :Katakana :Bopomofo :Han :Yi :Old-Italic :Gothic :Deseret
182 :Inherited :Tagalog :Hanunoo :Buhid :Tagbanwa :Limbu :Tai-Le :Linear-B
183 :Ugaritic :Shavian :Osmanya :Cypriot :Braille :Buginese :Coptic :New-Tai-Lue
184 :Glagolitic :Tifinagh :Syloti-Nagri :Old-Persian :Kharoshthi :Balinese
185 :Cuneiform :Phoenician :Phags-Pa :Nko :Sundanese :Lepcha :Ol-Chiki :Vai
186 :Saurashtra :Kayah-Li :Rejang :Lycian :Carian :Lydian :Cham :Tai-Tham
187 :Tai-Viet :Avestan :Egyptian-Hieroglyphs :Samaritan :Lisu :Bamum :Javanese
188 :Meetei-Mayek :Imperial-Aramaic :Old-South-Arabian :Inscriptional-Parthian
189 :Inscriptional-Pahlavi :Old-Turkic :Kaithi :Batak :Brahmi :Mandaic :Chakma
190 :Meroitic-Cursive :Meroitic-Hieroglyphs :Miao :Sharada :Sora-Sompeng
191 :Takri :Bassa-Vah :Mahajani :Pahawh-Hmong :Caucasian-Albanian :Manichaean
192 :Palmyrene :Duployan :Mende-Kikakui :Pau-Cin-Hau :Elbasan :Modi
193 :Psalter-Pahlavi :Grantha :Mro :Siddham :Khojki :Nabataean :Tirhuta
194 :Khudawadi :Old-North-Arabian :Warang-Citi :Linear-A :Old-Permic))
196 (sb!impl::defglobal *line-break-classes*
197 #(:XX :AI :AL :B2 :BA :BB :BK :CB :CJ :CL :CM :CP :CR :EX :GL
198 :HL :HY :ID :IN :IS :LF :NL :NS :NU :OP :PO :PR :QU :RI :SA
199 :SG :SP :SY :WJ :ZW))
201 (sb!impl::defglobal *blocks*
202 #(:Basic-Latin :Latin-1-Supplement :Latin-Extended-A :Latin-Extended-B
203 :IPA-Extensions :Spacing-Modifier-Letters :Combining-Diacritical-Marks
204 :Greek-and-Coptic :Cyrillic :Cyrillic-Supplement :Armenian :Hebrew :Arabic
205 :Syriac :Arabic-Supplement :Thaana :NKo :Samaritan :Mandaic
206 :Arabic-Extended-A :Devanagari :Bengali :Gurmukhi :Gujarati :Oriya :Tamil
207 :Telugu :Kannada :Malayalam :Sinhala :Thai :Lao :Tibetan :Myanmar :Georgian
208 :Hangul-Jamo :Ethiopic :Ethiopic-Supplement :Cherokee
209 :Unified-Canadian-Aboriginal-Syllabics :Ogham :Runic :Tagalog :Hanunoo
210 :Buhid :Tagbanwa :Khmer :Mongolian
211 :Unified-Canadian-Aboriginal-Syllabics-Extended :Limbu :Tai-Le :New-Tai-Lue
212 :Khmer-Symbols :Buginese :Tai-Tham :Combining-Diacritical-Marks-Extended
213 :Balinese :Sundanese :Batak :Lepcha :Ol-Chiki :Sundanese-Supplement
214 :Vedic-Extensions :Phonetic-Extensions :Phonetic-Extensions-Supplement
215 :Combining-Diacritical-Marks-Supplement :Latin-Extended-Additional
216 :Greek-Extended :General-Punctuation :Superscripts-and-Subscripts
217 :Currency-Symbols :Combining-Diacritical-Marks-for-Symbols
218 :Letterlike-Symbols :Number-Forms :Arrows :Mathematical-Operators
219 :Miscellaneous-Technical :Control-Pictures :Optical-Character-Recognition
220 :Enclosed-Alphanumerics :Box-Drawing :Block-Elements :Geometric-Shapes
221 :Miscellaneous-Symbols :Dingbats :Miscellaneous-Mathematical-Symbols-A
222 :Supplemental-Arrows-A :Braille-Patterns :Supplemental-Arrows-B
223 :Miscellaneous-Mathematical-Symbols-B :Supplemental-Mathematical-Operators
224 :Miscellaneous-Symbols-and-Arrows :Glagolitic :Latin-Extended-C :Coptic
225 :Georgian-Supplement :Tifinagh :Ethiopic-Extended :Cyrillic-Extended-A
226 :Supplemental-Punctuation :CJK-Radicals-Supplement :Kangxi-Radicals
227 :Ideographic-Description-Characters :CJK-Symbols-and-Punctuation :Hiragana
228 :Katakana :Bopomofo :Hangul-Compatibility-Jamo :Kanbun :Bopomofo-Extended
229 :CJK-Strokes :Katakana-Phonetic-Extensions :Enclosed-CJK-Letters-and-Months
230 :CJK-Compatibility :CJK-Unified-Ideographs-Extension-A
231 :Yijing-Hexagram-Symbols :CJK-Unified-Ideographs :Yi-Syllables :Yi-Radicals
232 :Lisu :Vai :Cyrillic-Extended-B :Bamum :Modifier-Tone-Letters
233 :Latin-Extended-D :Syloti-Nagri :Common-Indic-Number-Forms :Phags-pa
234 :Saurashtra :Devanagari-Extended :Kayah-Li :Rejang :Hangul-Jamo-Extended-A
235 :Javanese :Myanmar-Extended-B :Cham :Myanmar-Extended-A :Tai-Viet
236 :Meetei-Mayek-Extensions :Ethiopic-Extended-A :Latin-Extended-E
237 :Meetei-Mayek :Hangul-Syllables :Hangul-Jamo-Extended-B :High-Surrogates
238 :High-Private-Use-Surrogates :Low-Surrogates :Private-Use-Area
239 :CJK-Compatibility-Ideographs :Alphabetic-Presentation-Forms
240 :Arabic-Presentation-Forms-A :Variation-Selectors :Vertical-Forms
241 :Combining-Half-Marks :CJK-Compatibility-Forms :Small-Form-Variants
242 :Arabic-Presentation-Forms-B :Halfwidth-and-Fullwidth-Forms :Specials
243 :Linear-B-Syllabary :Linear-B-Ideograms :Aegean-Numbers
244 :Ancient-Greek-Numbers :Ancient-Symbols :Phaistos-Disc :Lycian :Carian
245 :Coptic-Epact-Numbers :Old-Italic :Gothic :Old-Permic :Ugaritic :Old-Persian
246 :Deseret :Shavian :Osmanya :Elbasan :Caucasian-Albanian :Linear-A
247 :Cypriot-Syllabary :Imperial-Aramaic :Palmyrene :Nabataean :Phoenician
248 :Lydian :Meroitic-Hieroglyphs :Meroitic-Cursive :Kharoshthi
249 :Old-South-Arabian :Old-North-Arabian :Manichaean :Avestan
250 :Inscriptional-Parthian :Inscriptional-Pahlavi :Psalter-Pahlavi :Old-Turkic
251 :Rumi-Numeral-Symbols :Brahmi :Kaithi :Sora-Sompeng :Chakma :Mahajani
252 :Sharada :Sinhala-Archaic-Numbers :Khojki :Khudawadi :Grantha :Tirhuta
253 :Siddham :Modi :Takri :Warang-Citi :Pau-Cin-Hau :Cuneiform
254 :Cuneiform-Numbers-and-Punctuation :Egyptian-Hieroglyphs :Bamum-Supplement
255 :Mro :Bassa-Vah :Pahawh-Hmong :Miao :Kana-Supplement :Duployan
256 :Shorthand-Format-Controls :Byzantine-Musical-Symbols :Musical-Symbols
257 :Ancient-Greek-Musical-Notation :Tai-Xuan-Jing-Symbols
258 :Counting-Rod-Numerals :Mathematical-Alphanumeric-Symbols :Mende-Kikakui
259 :Arabic-Mathematical-Alphabetic-Symbols :Mahjong-Tiles :Domino-Tiles
260 :Playing-Cards :Enclosed-Alphanumeric-Supplement
261 :Enclosed-Ideographic-Supplement :Miscellaneous-Symbols-and-Pictographs
262 :Emoticons :Ornamental-Dingbats :Transport-and-Map-Symbols
263 :Alchemical-Symbols :Geometric-Shapes-Extended :Supplemental-Arrows-C
264 :CJK-Unified-Ideographs-Extension-B :CJK-Unified-Ideographs-Extension-C
265 :CJK-Unified-Ideographs-Extension-D :CJK-Compatibility-Ideographs-Supplement
266 :Tags :Variation-Selectors-Supplement :Supplementary-Private-Use-Area-A
267 :Supplementary-Private-Use-Area-B))
269 (declaim (inline svref-or-null))
270 (defun svref-or-null (vector index)
271 (and (< index (length vector))
272 (svref vector index)))
274 (defun general-category (character)
275 #!+sb-doc
276 "Returns the general category of CHARACTER as it appears in UnicodeData.txt"
277 (svref-or-null *general-categories* (sb!impl::ucd-general-category character)))
279 (defun bidi-class (character)
280 #!+sb-doc
281 "Returns the bidirectional class of CHARACTER"
282 (if (and (eql (general-category character) :Cn)
283 (default-ignorable-p character))
285 (svref-or-null
286 *bidi-classes*
287 (aref **character-misc-database** (1+ (misc-index character))))))
289 (declaim (inline combining-class))
290 (defun combining-class (character)
291 #!+sb-doc
292 "Returns the canonical combining class (CCC) of CHARACTER"
293 (aref **character-misc-database** (+ 2 (misc-index character))))
295 (defun decimal-value (character)
296 #!+sb-doc
297 "Returns the decimal digit value associated with CHARACTER or NIL if
298 there is no such value.
300 The only characters in Unicode with a decimal digit value are those
301 that are part of a range of characters that encode the digits 0-9.
302 Because of this, `(decimal-digit c) <=> (digit-char-p c 10)` in
303 #+sb-unicode builds"
304 (sb!impl::ucd-decimal-digit character))
306 (defun digit-value (character)
307 #!+sb-doc
308 "Returns the Unicode digit value of CHARACTER or NIL if it doesn't exist.
310 Digit values are guaranteed to be integers between 0 and 9 inclusive.
311 All characters with decimal digit values have the same digit value,
312 but there are characters (such as digits of number systems without a 0 value)
313 that have a digit value but no decimal digit value"
314 (let ((%digit (clear-flag 6
315 (aref **character-misc-database**
316 (+ 3 (misc-index character))))))
317 (if (< %digit 10) %digit nil)))
319 (defun numeric-value (character)
320 #!+sb-doc
321 "Returns the numeric value of CHARACTER or NIL if there is no such value.
322 Numeric value is the most general of the Unicode numeric properties.
323 The only constraint on the numeric value is that it be a rational number."
324 (or (double-vector-binary-search (char-code character)
325 **special-numerics**)
326 (digit-value character)))
328 (defun mirrored-p (character)
329 #!+sb-doc
330 "Returns T if CHARACTER needs to be mirrored in bidirectional text.
331 Otherwise, returns NIL."
332 (logbitp 5 (aref **character-misc-database**
333 (+ 5 (misc-index character)))))
335 (defun bidi-mirroring-glyph (character)
336 #!+sb-doc
337 "Returns the mirror image of CHARACTER if it exists.
338 Otherwise, returns NIL."
339 (when (mirrored-p character)
340 (let ((ret (gethash (char-code character) **bidi-mirroring-glyphs**)))
341 (when ret (code-char ret)))))
343 (defun east-asian-width (character)
344 #!+sb-doc
345 "Returns the East Asian Width property of CHARACTER as
346 one of the keywords :N (Narrow), :A (Ambiguous), :H (Halfwidth),
347 :W (Wide), :F (Fullwidth), or :NA (Not applicable)"
348 (svref-or-null *east-asian-widths*
349 (ldb (byte 3 0)
350 (aref **character-misc-database**
351 (+ 5 (misc-index character))))))
353 (defun script (character)
354 #!+sb-doc
355 "Returns the Script property of CHARACTER as a keyword.
356 If CHARACTER does not have a known script, returns :UNKNOWN"
357 (svref-or-null *scripts*
358 (aref **character-misc-database** (+ 6 (misc-index character)))))
360 (defun char-block (character)
361 #!+sb-doc
362 "Returns the Unicode block in which CHARACTER resides as a keyword.
363 If CHARACTER does not have a known block, returns :NO-BLOCK"
364 (let* ((code (char-code character))
365 (block-index (ordered-ranges-position code **block-ranges**)))
366 (if block-index
367 (aref *blocks* block-index) :no-block)))
369 (defun unicode-1-name (character)
370 #!+sb-doc
371 "Returns the name assigned to CHARACTER in Unicode 1.0 if it is distinct
372 from the name currently assigned to CHARACTER. Otherwise, returns NIL.
373 This property has been officially obsoleted by the Unicode standard, and
374 is only included for backwards compatibility."
375 (let* ((char-code (char-code character))
376 (h-code (double-vector-binary-search char-code
377 **unicode-1-char-name-database**)))
378 (when h-code
379 (huffman-decode h-code **unicode-character-name-huffman-tree**))))
381 (defun age (character)
382 #!+sb-doc
383 "Returns the version of Unicode in which CHARACTER was assigned as a pair
384 of values, both integers, representing the major and minor version respectively.
385 If CHARACTER is not assigned in Unicode, returns NIL for both values."
386 (let* ((value (aref **character-misc-database** (+ 8 (misc-index character))))
387 (major (ash value -3))
388 (minor (ldb (byte 3 0) value)))
389 (if (zerop value) (values nil nil) (values major minor))))
391 (defun hangul-syllable-type (character)
392 #!+sb-doc
393 "Returns the Hangul syllable type of CHARACTER.
394 The syllable type can be one of :L, :V, :T, :LV, or :LVT.
395 If the character is not a Hangul syllable or Jamo, returns NIL"
396 (let ((cp (char-code character)))
397 (cond
398 ((or
399 (and (<= #x1100 cp) (<= cp #x115f))
400 (and (<= #xa960 cp) (<= cp #xa97c))) :L)
401 ((or
402 (and (<= #x1160 cp) (<= cp #x11a7))
403 (and (<= #xd7B0 cp) (<= cp #xd7C6))) :V)
404 ((or
405 (and (<= #x11a8 cp) (<= cp #x11ff))
406 (and (<= #xd7c8 cp) (<= cp #xd7fb))) :T)
407 ((and (<= #xac00 cp) (<= cp #xd7a3))
408 (if (= 0 (rem (- cp #xac00) 28)) :LV :LVT)))))
410 (defun line-break-class (character &key resolve)
411 #!+sb-doc
412 "Returns the line breaking class of CHARACTER, as specified in UAX #14.
413 If :RESOLVE is NIL, returns the character class found in the property file.
414 If :RESOLVE is non-NIL, centain line-breaking classes will be mapped to othec
415 classes as specified in the applicable standards. Addinionally, if :RESOLVE
416 is :EAST-ASIAN, Ambigious (class :AI) characters will be mapped to the
417 Ideographic (:ID) class instead of Alphabetic (:AL)."
418 (when (and resolve (listp character)) (setf character (car character)))
419 (when (and resolve (not character)) (return-from line-break-class :nil))
420 (let ((raw-class
421 (svref-or-null *line-break-classes*
422 (aref **character-misc-database** (+ 7 (misc-index character)))))
423 (syllable-type (hangul-syllable-type character)))
424 (when syllable-type
425 (setf raw-class
426 (cdr (assoc syllable-type
427 '((:l . :JL) (:v . :JV) (:t . :JT)
428 (:lv . :H2) (:lvt . :H3))))))
429 (when resolve
430 (setf raw-class
431 (case raw-class
432 (:ai (if (eql resolve :east-asion) :ID :AL))
433 ; If we see :CM when resolving, we have a CM that isn't subject
434 ; to LB9, so we do LB10
435 ((:xx :cm) :al)
436 (:sa (if (member (general-category character) '(:Mn :Mc))
437 :CM :AL))
438 (:cj :ns)
439 (:sg (error "The character ~S is a surrogate, which should not
440 appear in an SBCL string. The line-breaking behavior of surrogates is undefined."
441 character))
442 (t raw-class))))
443 raw-class))
445 (defun uppercase-p (character)
446 #!+sb-doc
447 "Returns T if CHARACTER has the Unicode property Uppercase and NIL otherwise"
448 (or (eql (general-category character) :Lu) (proplist-p character :other-uppercase)))
450 (defun lowercase-p (character)
451 #!+sb-doc
452 "Returns T if CHARACTER has the Unicode property Lowercase and NIL otherwise"
453 (or (eql (general-category character) :Ll) (proplist-p character :other-lowercase)))
455 (defun cased-p (character)
456 #!+sb-doc
457 "Returns T if CHARACTER has a (Unicode) case, and NIL otherwise"
458 (or (uppercase-p character) (lowercase-p character)
459 (eql (general-category character) :Lt)))
461 (defun case-ignorable-p (character)
462 #!+sb-doc
463 "Returns T if CHARACTER is Case Ignorable as defined in Unicode 6.3, Chapter
465 (or (member (general-category character)
466 '(:Mn :Me :Cf :Lm :Sk))
467 (member (word-break-class character)
468 '(:midletter :midnumlet :single-quote))))
470 (defun alphabetic-p (character)
471 #!+sb-doc
472 "Returns T if CHARACTER is Alphabetic according to the Unicode standard
473 and NIL otherwise"
474 (or (member (general-category character) '(:Lu :Ll :Lt :Lm :Lo :Nl))
475 (proplist-p character :other-alphabetic)))
477 (defun ideographic-p (character)
478 #!+sb-doc
479 "Returns T if CHARACTER has the Unicode property Ideographic,
480 which loosely corresponds to the set of \"Chinese characters\""
481 (proplist-p character :ideographic))
483 (defun math-p (character)
484 #!+sb-doc
485 "Returns T if CHARACTER is a mathematical symbol according to Unicode and
486 NIL otherwise"
487 (or (eql (general-category character) :sm) (proplist-p character :other-math)))
489 (defun whitespace-p (character)
490 #!+sb-doc
491 "Returns T if CHARACTER is whitespace according to Unicode
492 and NIL otherwise"
493 (proplist-p character :white-space))
495 (defun hex-digit-p (character &key ascii)
496 #!+sb-doc
497 "Returns T if CHARACTER is a hexadecimal digit and NIL otherwise.
498 If :ASCII is non-NIL, fullwidth equivalents of the Latin letters A through F
499 are excluded."
500 (proplist-p character (if ascii :ascii-hex-digit :hex-digit)))
502 (defun soft-dotted-p (character)
503 #!+sb-doc
504 "Returns T if CHARACTER has a soft dot (such as the dots on i and j) which
505 disappears when accents are placed on top of it. and NIL otherwise"
506 (proplist-p character :soft-dotted))
508 (defun default-ignorable-p (character)
509 #!+sb-doc
510 "Returns T if CHARACTER is a Default_Ignorable_Code_Point"
511 (and
512 (or (proplist-p character :other-default-ignorable-code-point)
513 (eql (general-category character) :cf)
514 (proplist-p character :variation-selector))
515 (not
516 (or (whitespace-p character)
517 (ordered-ranges-member
518 (char-code character)
519 #(#x0600 #x0604 #x06DD #x06DD #x070F #x070F #xFFF9 #xFFFB
520 #x110BD #x110BD))))))
523 ;;; Implements UAX#15: Normalization Forms
524 (declaim (inline char-decomposition-info))
525 (defun char-decomposition-info (char)
526 (let ((value (aref **character-misc-database**
527 (+ 4 (misc-index char)))))
528 (values (clear-flag 7 value) (logbitp 7 value))))
530 (defun char-decomposition (char length callback)
531 (declare (function callback))
532 ;; Caller should have gotten length from char-decomposition-info
533 (let* ((cp (char-code char))
534 (cp-high (ash cp -8))
535 (decompositions **character-decompositions**)
536 (high-page (aref **character-high-pages** cp-high))
537 (index (unless (logbitp 15 high-page) ;; Hangul syllable
538 (aref **character-low-pages**
539 (+ 1 (* 2 (+ (ldb (byte 8 0) cp) (ash high-page 8))))))))
540 (cond ((= length 1)
541 (funcall callback (code-char (aref decompositions index))))
542 ((<= #xac00 cp #xd7a3)
543 ;; see Unicode 6.2, section 3-12
544 (let* ((sbase #xac00)
545 (lbase #x1100)
546 (vbase #x1161)
547 (tbase #x11a7)
548 (vcount 21)
549 (tcount 28)
550 (ncount (* vcount tcount))
551 (sindex (- cp sbase))
552 (lindex (floor sindex ncount))
553 (vindex (floor (mod sindex ncount) tcount))
554 (tindex (mod sindex tcount)))
555 (funcall callback (code-char (+ lbase lindex)))
556 (funcall callback (code-char (+ vbase vindex)))
557 (when (> tindex 0)
558 (funcall callback (code-char (+ tbase tindex))))))
561 (loop for i below length
563 (funcall callback (code-char (aref decompositions (+ index i)))))))))
565 (defun decompose-char (char compatibility callback)
566 (declare (function callback))
567 (multiple-value-bind (info compat) (char-decomposition-info char)
568 (if (and (plusp info)
569 (or compatibility
570 (not compat)))
571 (if compatibility
572 (dx-flet ((callback (char)
573 (decompose-char char t callback)))
574 (char-decomposition char info #'callback))
575 (char-decomposition char info callback))
576 (funcall callback char))))
578 (defun decompose-string (string kind filter)
579 (let ((compatibility (ecase kind
580 (:compatibility t)
581 (:canonical nil))))
582 (let (chars
583 (length 0)
584 (previous-combining-class 0))
585 (declare (type index length))
586 (dx-flet ((callback (char)
587 (let ((combining-class (combining-class char)))
588 (incf length)
589 (cond ((< 0 combining-class previous-combining-class)
590 ;; Ensure it's sorted
591 (loop for cons on chars
592 for next-char = (cadr cons)
593 when (or (not next-char)
594 (<= 0 (combining-class next-char) combining-class))
595 do (setf (cdr cons)
596 (cons char (cdr cons)))
597 (return)))
599 (push char chars)
600 (setf previous-combining-class combining-class))))))
601 (sb!kernel:with-array-data ((string string) (start) (end))
602 (declare (ignore start))
603 (let ((calback (if filter
604 (let ((filter (sb!kernel:%coerce-callable-to-fun filter)))
605 (lambda (char)
606 (when (funcall filter char)
607 (callback char))))
608 #'callback)))
609 (loop for i below end
610 for char = (schar string i)
612 (decompose-char char compatibility calback))))
613 (let ((result (make-string length)))
614 (loop for char in chars
615 for i from (1- length) downto 0
616 do (setf (schar result i) char))
617 result)))))
619 (defun composition-hangul-syllable-type (cp)
620 (cond
621 ((and (<= #x1100 cp) (<= cp #x1112)) :L)
622 ((and (<= #x1161 cp) (<= cp #x1175)) :V)
623 ((and (<= #x11a8 cp) (<= cp #x11c2)) :T)
624 ((and (<= #xac00 cp) (<= cp #.(+ #xac00 11171)))
625 (if (= 0 (rem (- cp #xac00) 28)) :LV :LVT))))
627 (defun primary-composition (char1 char2)
628 (flet ((maybe (fn x) (when x (funcall fn x))))
629 (let ((c1 (char-code char1))
630 (c2 (char-code char2)))
631 (maybe
632 #'code-char
633 (cond
634 ((gethash (dpb c1 (byte 21 21) c2)
635 **character-primary-compositions**))
636 ((and (eql (composition-hangul-syllable-type c1) :L)
637 (eql (composition-hangul-syllable-type c2) :V))
638 (let ((lindex (- c1 #x1100))
639 (vindex (- c2 #x1161)))
640 (+ #xac00 (* lindex 588) (* vindex 28))))
641 ((and (eql (composition-hangul-syllable-type c1) :LV)
642 (eql (composition-hangul-syllable-type c2) :T))
643 (+ c1 (- c2 #x11a7))))))))
645 ;;; This implements a sequence data structure, specialized for
646 ;;; efficient deletion of characters at an index, along with tolerable
647 ;;; random access. The purpose is to support the canonical
648 ;;; composition algorithm from Unicode, which involves replacing (not
649 ;;; necessarily consecutive) pairs of code points with a single code
650 ;;; point (e.g. [#\e #\combining_acute_accent] with
651 ;;; #\latin_small_letter_e_with_acute). The data structure is a list
652 ;;; of three-element lists, each denoting a chunk of string data
653 ;;; starting at the first index and ending at the second.
655 ;;; Actually, the implementation isn't particularly efficient, and
656 ;;; would probably benefit from being rewritten in terms of displaced
657 ;;; arrays, which would substantially reduce copying.
659 ;;; (also, generic sequences. *sigh*.)
660 (defun lref (lstring index)
661 (dolist (l lstring)
662 (when (and (<= (first l) index)
663 (< index (second l)))
664 (return (aref (third l) (- index (first l)))))))
666 (defun (setf lref) (newchar lstring index)
667 (dolist (l lstring)
668 (when (and (<= (first l) index)
669 (< index (second l)))
670 (return (setf (aref (third l) (- index (first l))) newchar)))))
672 (defun llength (lstring)
673 (second (first (last lstring))))
675 (defun lstring (lstring)
676 (let ((result (make-string (llength lstring))))
677 (dolist (l lstring result)
678 (replace result (third l) :start1 (first l) :end1 (second l)))))
680 (defun ldelete (lstring index)
681 (do* ((ls lstring (cdr ls))
682 (l (car ls) (car ls))
683 so-fars)
684 ((and (<= (first l) index)
685 (< index (second l)))
686 (append
687 (nreverse so-fars)
688 (cond
689 ((= (first l) index)
690 (list (list (first l) (1- (second l)) (subseq (third l) 1))))
691 ((= index (1- (second l)))
692 (list (list (first l) (1- (second l)) (subseq (third l) 0 (1- (length (third l)))))))
694 (list
695 (list (first l) index
696 (subseq (third l) 0 (- index (first l))))
697 (list index (1- (second l))
698 (subseq (third l) (1+ (- index (first l))))))))
699 (mapcar (lambda (x) (list (1- (first x)) (1- (second x)) (third x)))
700 (cdr ls))))
701 (push l so-fars)))
703 (defun canonically-compose (string)
704 (let* ((result (list (list 0 (length string) string)))
705 (previous-starter-index (position 0 string :key #'combining-class))
706 (i (and previous-starter-index (1+ previous-starter-index))))
707 (when (or (not i) (= i (length string)))
708 (return-from canonically-compose string))
709 (tagbody
710 again
711 (when (and (>= (- i previous-starter-index) 2)
712 ;; test for Blocked (Unicode 3.11 para. D115)
714 ;; (assumes here that string has sorted combiners,
715 ;; so can look back just one step)
716 (>= (combining-class (lref result (1- i)))
717 (combining-class (lref result i))))
718 (when (= (combining-class (lref result i)) 0)
719 (setf previous-starter-index i))
720 (incf i)
721 (go next))
723 (let ((comp (primary-composition (lref result previous-starter-index)
724 (lref result i))))
725 (cond
726 (comp
727 (setf (lref result previous-starter-index) comp)
728 (setf result (ldelete result i)))
730 (when (= (combining-class (lref result i)) 0)
731 (setf previous-starter-index i))
732 (incf i))))
733 next
734 (unless (= i (llength result))
735 (go again)))
736 (if (= i (length string))
737 string
738 (lstring result))))
740 (defun normalize-string (string &optional (form :nfd)
741 filter)
742 #!+sb-doc
743 "Normalize STRING to the Unicode normalization form form.
744 Acceptable values for form are :NFD, :NFC, :NFKD, and :NFKC.
745 If FILTER is a function it is called on each decomposed character and
746 only characters for which it returns T are collected."
747 (declare (type (member :nfd :nfkd :nfc :nfkc) form))
748 #!-sb-unicode
749 (etypecase string
750 ((array nil (*)) string)
751 (string
752 (ecase form
753 ((:nfc :nfkc) string)
754 ((:nfd :nfkd) (error "Cannot normalize to ~A form in #-SB-UNICODE builds" form)))))
755 #!+sb-unicode
756 (etypecase string
757 (base-string string)
758 ((array character (*))
759 (ecase form
760 ((:nfc)
761 (canonically-compose (decompose-string string :canonical filter)))
762 ((:nfd)
763 (decompose-string string :canonical filter))
764 ((:nfkc)
765 (canonically-compose (decompose-string string :compatibility filter)))
766 ((:nfkd)
767 (decompose-string string :compatibility filter))))
768 ((array nil (*)) string)))
770 (defun normalized-p (string &optional (form :nfd))
771 #!+sb-doc
772 "Tests if STRING is normalized to FORM"
773 ;; FIXME: can be optimized
774 (string= string (normalize-string string form)))
777 ;;; Unicode case algorithms
778 ;; FIXME: Make these parts less redundant (macro?)
779 (defparameter **special-titlecases**
780 '#.(with-open-file (stream
781 (merge-pathnames
782 (make-pathname
783 :directory
784 '(:relative :up :up "output")
785 :name "titlecases" :type "lisp-expr")
786 sb!xc:*compile-file-truename*)
787 :direction :input
788 :element-type 'character)
789 (read stream)))
791 (defparameter **special-casefolds**
792 '#.(with-open-file (stream
793 (merge-pathnames
794 (make-pathname
795 :directory
796 '(:relative :up :up "output")
797 :name "foldcases" :type "lisp-expr")
798 sb!xc:*compile-file-truename*)
799 :direction :input
800 :element-type 'character)
801 (read stream)))
803 (defun has-case-p (char)
804 ;; Bit 6 is the Unicode case flag, as opposed to the Common Lisp one
805 (logbitp 6 (aref **character-misc-database** (+ 5 (misc-index char)))))
807 (defun char-uppercase (char)
808 (if (has-case-p char)
809 (let ((cp (car (char-case-info char))))
810 (if (atom cp) (list (code-char cp)) (mapcar #'code-char cp)))
811 (list char)))
813 (defun char-lowercase (char)
814 (if (has-case-p char)
815 (let ((cp (cdr (char-case-info char))))
816 (if (atom cp) (list (code-char cp)) (mapcar #'code-char cp)))
817 (list char)))
819 (defun char-titlecase (char)
820 (unless (has-case-p char) (return-from char-titlecase (list char)))
821 (let* ((cp (char-code char))
822 (value (assoc cp **special-titlecases**)))
823 (if value
824 (if (atom (cdr value))
825 (list (code-char (cdr value)))
826 (mapcar #'code-char (cdr value)))
827 (char-uppercase char))))
829 (defun char-foldcase (char)
830 (unless (has-case-p char) (return-from char-foldcase (list char)))
831 (let* ((cp (char-code char))
832 (value (assoc cp **special-casefolds**)))
833 (if value
834 (if (atom (cdr value))
835 (list (code-char (cdr value)))
836 (mapcar #'code-char (cdr value)))
837 (char-lowercase char))))
839 (defun string-somethingcase (fn string special-fn)
840 (let (result (len (length string)))
841 (loop for index from 0 below len
842 for char = (char string index)
843 for cased = (or (funcall special-fn char index len)
844 (funcall fn char))
845 do (loop for c in (remove :none cased) do (push c result)))
846 (setf result (nreverse result))
847 (coerce result 'string)))
849 (declaim (type function sb!unix::posix-getenv))
850 (defun get-user-locale ()
851 (let ((raw-locale
852 #!+(or win32 unix) (or (sb!unix::posix-getenv "LC_ALL")
853 (sb!unix::posix-getenv "LANG"))
854 #!-(or win32 unix) nil))
855 (when raw-locale
856 (let ((lang-code (string-upcase
857 (subseq raw-locale 0 (position #\_ raw-locale)))))
858 (when lang-code
859 (intern lang-code "KEYWORD"))))))
862 (defun uppercase (string &key locale)
863 #!+sb-doc
864 "Returns the full uppercase of STRING according to the Unicode standard.
865 The result is not guaranteed to have the same length as the input. If :LOCALE
866 is NIL, no language-specific case transformations are applied. If :LOCALE is a
867 keyword representing a two-letter ISO country code, the case transforms of that
868 locale are used. If :LOCALE is T, the user's current locale is used (Unix and
869 Win32 only)."
870 (when (eq locale t) (setf locale (get-user-locale)))
871 (string-somethingcase
872 #'char-uppercase string
873 #!-sb-unicode (constantly nil)
874 #!+sb-unicode ;; code-char with a constant > 255 breaks the build
875 #'(lambda (char index len)
876 (declare (ignore len))
877 (cond
878 ((and (eql locale :lt) (char= char (code-char #x0307))
879 (loop for i from (1- index) downto 0
880 for c = (char string i)
881 do (case (combining-class c)
882 (0 (return (soft-dotted-p c)))
883 (230 (return nil))
884 (t t))
885 finally (return nil)))
886 '(:none))
887 ((and (or (eql locale :tr) (eql locale :az))
888 (char= char #\i))
889 (list (code-char #x0130)))
890 (t nil)))))
892 (defun lowercase (string &key locale)
893 #!+sb-doc
894 "Returns the full lowercase of STRING according to the Unicode standard.
895 The result is not guaranteed to have the same length as the input.
896 :LOCALE has the same semantics as the :LOCALE argument to UPPERCASE."
897 (when (eq locale t) (setf locale (get-user-locale)))
898 (string-somethingcase
899 #'char-lowercase string
900 #!-sb-unicode (constantly nil)
901 #!+sb-unicode
902 #'(lambda (char index len)
903 (cond
904 ((and (char= char (code-char #x03A3))
905 (loop for i from (1- index) downto 0
906 for c = (char string i)
907 do (cond ((cased-p c) (return t))
908 ((case-ignorable-p c))
909 (t (return nil)))
910 finally (return nil))
911 (loop for i from (1+ index) below len
912 for c = (char string i)
913 do (cond ((cased-p c) (return nil))
914 ((case-ignorable-p c))
915 (t (return t)))
916 finally (return t)))
917 (list (code-char #x03C2)))
918 ((eql locale :lt)
919 (mapcar
920 #'code-char
921 (cdr (or
922 (assoc (char-code char)
923 '((#x00CC . (#x0069 #x0307 #x0300))
924 (#x00CD . (#x0069 #x0307 #x0301))
925 (#x0128 . (#x0069 #x0307 #x0303))))
926 (and (loop for i from (1+ index) below len
927 for c = (char string i)
928 do (case (combining-class c)
929 (230 (return t))
930 (0 (return nil))
931 (t t))
932 finally (return nil))
933 (assoc (char-code char)
934 '((#x0049 . (#x0069 #x0307))
935 (#x004A . (#x006A #x0307))
936 (#x012E . (#x012F #x0307)))))))))
937 ((or (eql locale :tr) (eql locale :az))
938 (cond
939 ((char= char (code-char #x0130)) (list #\i))
940 ((and (char= char (code-char #x0307))
941 (loop for i from (1- index) downto 0
942 for c = (char string i)
943 do (case (combining-class c)
944 (0 (return (char= c #\I)))
945 (230 (return nil))
946 (t t))
947 finally (return nil)))
948 '(:none))
949 ((and (char= char #\I)
950 (loop for i from (1+ index) below len
951 for c = (char string i)
952 do (case (combining-class c)
953 (0 (return t))
954 (230 (return (char/= c (code-char #x0307))))
955 (t t))
956 finally (return t)))
957 (list (code-char #x0131)))
958 (t nil)))
959 (t nil)))))
961 (defun titlecase (string &key locale)
962 #!+sb-doc
963 "Returns the titlecase of STRING. The resulting string can
964 be longer than the input.
965 :LOCALE has the same semantics as the :LOCALE argument to UPPERCASE."
966 (when (eq locale t) (setf locale (get-user-locale)))
967 (let ((words (words string))
968 (cased nil))
969 (loop for word in words
970 for first-cased = (or (position-if #'cased-p word) 0)
971 for pre = (subseq word 0 first-cased)
972 for initial = (char word first-cased)
973 for rest = (subseq word (1+ first-cased))
974 do (let ((up (char-titlecase initial)) (down (lowercase rest)))
975 #!+sb-unicode
976 (when (and (or (eql locale :tr) (eql locale :az))
977 (eql initial #\i))
978 (setf up (list (code-char #x0130))))
979 #!+sb-unicode
980 (when (and (eql locale :lt)
981 (soft-dotted-p initial)
982 (eql (char down
983 (position-if
984 #'(lambda (c)
985 (or (eql (combining-class c) 0)
986 (eql (combining-class c) 230))) down))
987 (code-char #x0307)))
988 (setf down (delete (code-char #x0307) down :count 1)))
989 (push (concatenate 'string pre up down) cased)))
990 (apply #'concatenate 'string (nreverse cased))))
992 (defun casefold (string)
993 #!+sb-doc
994 "Returns the full casefolding of STRING according to the Unicode standard.
995 Casefolding remove case information in a way that allaws the results to be used
996 for case-insensitive comparisons.
997 The result is not guaranteed to have the same length as the input."
998 (string-somethingcase #'char-foldcase string (constantly nil)))
1001 ;;; Unicode break algorithms
1002 ;;; In all the breaking methods:
1003 ;;; (brk) establishes a break between `first` and `second`
1004 ;;; (nobrk) prevents a break between `first` and `second`
1005 ;;; Setting flag=T/state=:nobrk-next prevents a break between `second` and `htird`
1007 ;; Word breaking sets this to make their algorithms less tricky
1008 (defvar *other-break-special-graphemes* nil)
1009 (defun grapheme-break-class (char)
1010 #!+sb-doc
1011 "Returns the grapheme breaking class of CHARACTER, as specified in UAX #29."
1012 (let ((cp (when char (char-code char)))
1013 (gc (when char (general-category char)))
1014 (not-spacing-mark
1015 #(#x102B #x102C #x1038 #x1062 #x1063 #x1064 #x1067 #x1068 #x1069
1016 #x106A #x106B #x106C #x106D #x1083 #x1087 #x1088 #x1089 #x108A
1017 #x108B #x108C #x108F #x109A #x109B #x109C #x19B0 #x19B1 #x19B2
1018 #x19B3 #x19B4 #x19B8 #x19B9 #x19BB #x19BC #x19BD #x19BE #x19BF
1019 #x19C0 #x19C8 #x19C9 #x1A61 #x1A63 #x1A64 #xAA7B #xAA7D)))
1020 (cond
1021 ((not char) nil)
1022 ((= cp 10) :LF)
1023 ((= cp 13) :CR)
1024 ((or (member gc '(:Mn :Me))
1025 (proplist-p char :other-grapheme-extend)
1026 (and *other-break-special-graphemes*
1027 (member gc '(:Mc :Cf)) (not (<= #x200B cp #x200D))))
1028 :extend)
1029 ((or (member gc '(:Zl :Zp :Cc :Cs :Cf))
1030 ;; From Cn and Default_Ignorable_Code_Point
1031 (eql cp #x2065) (eql cp #xE0000)
1032 (<= #xFFF0 cp #xFFF8)
1033 (<= #xE0002 cp #xE001F)
1034 (<= #xE0080 cp #xE00FF)
1035 (<= #xE01F0 cp #xE0FFF)) :control)
1036 ((<= #x1F1E6 cp #x1F1FF) :regional-indicator)
1037 ((and (or (eql gc :Mc)
1038 (eql cp #x0E33) (eql cp #x0EB3))
1039 (not (binary-search cp not-spacing-mark))) :spacing-mark)
1040 (t (hangul-syllable-type char)))))
1042 (defun graphemes (string)
1043 #!+sb-doc
1044 "Breaks STRING into graphemes acording to the default
1045 grapheme breaking rules specified in UAX #29, returning a list of strings."
1046 (let* ((chars (coerce string 'list)) clusters (cluster (list (car chars))))
1047 (do ((first (car chars) second)
1048 (tail (cdr chars) (when tail (cdr tail)))
1049 (second (cadr chars) (when tail (cadr tail))))
1050 ((not first) (nreverse (mapcar #'(lambda (l) (coerce l 'string)) clusters)))
1051 (flet ((brk () (push (nreverse cluster) clusters) (setf cluster (list second)))
1052 (nobrk () (push second cluster)))
1053 (let ((c1 (grapheme-break-class first))
1054 (c2 (grapheme-break-class second)))
1055 (cond
1056 ((and (eql c1 :cr) (eql c2 :lf)) (nobrk))
1057 ((or (member c1 '(:control :cr :lf))
1058 (member c2 '(:control :cr :lf))) (brk))
1059 ((or (and (eql c1 :l) (member c2 '(:l :v :lv :lvt)))
1060 (and (or (eql c1 :v) (eql c1 :lv))
1061 (or (eql c2 :v) (eql c2 :t)))
1062 (and (eql c2 :t) (or (eql c1 :lvt) (eql c1 :t))))
1063 (nobrk))
1064 ((and (eql c1 :regional-indicator) (eql c2 :regional-indicator)) (nobrk))
1065 ((or (eql c2 :extend) (eql c2 :spacing-mark) (eql c1 :prepend)) (nobrk))
1066 (t (brk))))))))
1068 (defun word-break-class (char)
1069 #!+sb-doc
1070 "Returns the word breaking class of CHARACTER, as specified in UAX #29."
1071 ;; Words use graphemes as characters to deal with the ignore rule
1072 (when (listp char) (setf char (car char)))
1073 (let ((cp (when char (char-code char)))
1074 (gc (when char (general-category char)))
1075 (newlines #(#xB #xC #x0085 #x0085 #x2028 #x2029))
1076 (also-katakana
1077 #(#x3031 #x3035 #x309B #x309C
1078 #x30A0 #x30A0 #x30FC #x30FC
1079 #xFF70 #xFF70))
1080 (midnumlet #(#x002E #x2018 #x2019 #x2024 #xFE52 #xFF07 #xFF0E))
1081 (midletter
1082 #(#x003A #x00B7 #x002D7 #x0387 #x05F4 #x2027 #xFE13 #xFE55 #xFF1A))
1083 (midnum
1084 ;; Grepping of Line_Break = IS adjusted per UAX #29
1085 #(#x002C #x003B #x037E #x0589 #x060C #x060D #x066C #x07F8 #x2044
1086 #xFE10 #xFE14 #xFE50 #xFE54 #xFF0C #xFF1B)))
1087 (cond
1088 ((not char) nil)
1089 ((= cp 10) :LF)
1090 ((= cp 13) :CR)
1091 ((= cp #x27) :single-quote)
1092 ((= cp #x22) :double-quote)
1093 ((ordered-ranges-member cp newlines) :newline)
1094 ((or (eql (grapheme-break-class char) :extend)
1095 (eql gc :mc)) :extend)
1096 ((<= #x1F1E6 cp #x1F1FF) :regional-indicator)
1097 ((and (eql gc :Cf) (not (<= #x200B cp #x200D))) :format)
1098 ((or (eql (script char) :katakana)
1099 (ordered-ranges-member cp also-katakana)) :katakana)
1100 ((and (eql (script char) :Hebrew) (eql gc :lo)) :hebrew-letter)
1101 ((and (or (alphabetic-p char) (= cp #x05F3))
1102 (not (or (ideographic-p char)
1103 (eql (line-break-class char) :sa)
1104 (eql (script char) :hiragana)))) :aletter)
1105 ((binary-search cp midnumlet) :midnumlet)
1106 ((binary-search cp midletter) :midletter)
1107 ((binary-search cp midnum) :midnum)
1108 ((or (and (eql gc :Nd) (not (<= #xFF10 cp #xFF19))) ;Fullwidth digits
1109 (eql cp #x066B)) :numeric)
1110 ((eql gc :Pc) :extendnumlet)
1111 (t nil))))
1113 (defmacro flatpush (thing list)
1114 (let ((%thing (gensym)) (%i (gensym)))
1115 `(let ((,%thing ,thing))
1116 (if (listp ,%thing)
1117 (dolist (,%i ,%thing)
1118 (push ,%i ,list))
1119 (push ,%thing ,list)))))
1121 (defun words (string)
1122 #!+sb-doc
1123 "Breaks STRING into words acording to the default
1124 word breaking rules specified in UAX #29. Returns a list of strings"
1125 (let ((chars (mapcar
1126 #'(lambda (s)
1127 (let ((l (coerce s 'list)))
1128 (if (cdr l) l (car l))))
1129 (let ((*other-break-special-graphemes* t)) (graphemes string))))
1130 words word flag)
1131 (flatpush (car chars) word)
1132 (do ((first (car chars) second)
1133 (tail (cdr chars) (cdr tail))
1134 (second (cadr chars) (cadr tail)))
1135 ((not first) (nreverse (mapcar #'(lambda (l) (coerce l 'string)) words)))
1136 (flet ((brk () (push (nreverse word) words) (setf word nil) (flatpush second word))
1137 (nobrk () (flatpush second word)))
1138 (let ((c1 (word-break-class first))
1139 (c2 (word-break-class second))
1140 (c3 (when (and tail (cdr tail)) (word-break-class (cadr tail)))))
1141 (cond
1142 (flag (nobrk) (setf flag nil))
1143 ;; CR+LF are bound together by the grapheme clustering
1144 ((or (eql c1 :newline) (eql c1 :cr) (eql c1 :lf)
1145 (eql c2 :newline) (eql c2 :cr) (eql c2 :lf)) (brk))
1146 ((or (eql c2 :format) (eql c2 :extend)) (nobrk))
1147 ((and (or (eql c1 :aletter) (eql c1 :hebrew-letter))
1148 (or (eql c2 :aletter) (eql c2 :hebrew-letter))) (nobrk))
1149 ((and (or (eql c1 :aletter) (eql c1 :hebrew-letter))
1150 (member c2 '(:midletter :midnumlet :single-quote))
1151 (or (eql c3 :aletter) (eql c3 :hebrew-letter)))
1152 (nobrk) (setf flag t)) ; Handle the multiple breaks from this rule
1153 ((and (eql c1 :hebrew-letter) (eql c2 :double-quote)
1154 (eql c3 :hebrew-letter))
1155 (nobrk) (setf flag t))
1156 ((and (eql c1 :hebrew-letter) (eql c2 :single-quote)) (nobrk))
1157 ((or (and (eql c1 :numeric) (member c2 '(:numeric :aletter :hebrew-letter)))
1158 (and (eql c2 :numeric) (member c1 '(:numeric :aletter :hebrew-letter))))
1159 (nobrk))
1160 ((and (eql c1 :numeric)
1161 (member c2 '(:midnum :midnumlet :single-quote))
1162 (eql c3 :numeric))
1163 (nobrk) (setf flag t))
1164 ((and (eql c1 :katakana) (eql c2 :katakana)) (nobrk))
1165 ((or (and (member c1
1166 '(:aletter :hebrew-letter :katakana
1167 :numeric :extendnumlet)) (eql c2 :extendnumlet))
1168 (and (member c2
1169 '(:aletter :hebrew-letter :katakana
1170 :numeric :extendnumlet)) (eql c1 :extendnumlet)))
1171 (nobrk))
1172 ((and (eql c1 :regional-indicator) (eql c2 :regional-indicator)) (nobrk))
1173 (t (brk))))))))
1175 (defun sentence-break-class (char)
1176 #!+sb-doc
1177 "Returns the sentence breaking class of CHARACTER, as specified in UAX #29."
1178 (when (listp char) (setf char (car char)))
1179 (let ((cp (when char (char-code char)))
1180 (gc (when char (general-category char)))
1181 (aterms #(#x002E #x2024 #xFE52 #xFF0E))
1182 (scontinues
1183 #(#x002C #x002D #x003A #x055D #x060C #x060D #x07F8 #x1802 #x1808
1184 #x2013 #x2014 #x3001 #xFE10 #xFE11 #xFE13 #xFE31 #xFE32 #xFE50
1185 #xFE51 #xFE55 #xFE58 #xFE63 #xFF0C #xFF0D #xFF1A #xFF64)))
1186 (cond
1187 ((not char) nil)
1188 ((= cp 10) :LF)
1189 ((= cp 13) :CR)
1190 ((or (eql (grapheme-break-class char) :extend)
1191 (eql gc :mc)) :extend)
1192 ((or (eql cp #x0085) (<= #x2028 cp #x2029)) :sep)
1193 ((and (eql gc :Cf) (not (<= #x200C cp #x200D))) :format)
1194 ((whitespace-p char) :sp)
1195 ((lowercase-p char) :lower)
1196 ((or (uppercase-p char) (eql gc :Lt)) :upper)
1197 ((or (alphabetic-p char) (eql cp #x00A0) (eql cp #x05F3)) :oletter)
1198 ((or (and (eql gc :Nd) (not (<= #xFF10 cp #xFF19))) ;Fullwidth digits
1199 (<= #x066B cp #x066C)) :numeric)
1200 ((binary-search cp aterms) :aterm)
1201 ((binary-search cp scontinues) :scontinue)
1202 ((proplist-p char :sterm) :sterm)
1203 ((and (or (member gc '(:Po :Ps :Pe :Pf :Pi))
1204 (eql (line-break-class char) :qu))
1205 (not (eql cp #x05F3))) :close)
1206 (t nil))))
1208 (defun sentence-prebreak (string)
1209 #!+sb-doc
1210 "Pre-combines some sequences of characters to make the sentence-break
1211 algorithm simpler..
1212 Specifically,
1213 - Combines any character with the following extend of format characters
1214 - Combines CR + LF into '(CR LF)
1215 - Combines any run of :cp*:close* into one character"
1216 (let ((chars (coerce string 'list))
1217 cluster clusters last-seen sp-run)
1218 (labels ((flush () (if (cdr cluster) (push (nreverse cluster) clusters)
1219 (if cluster (push (car cluster) clusters)))
1220 (setf cluster nil))
1221 (brk (x)
1222 (flush) (push x clusters))
1223 (nobrk (x) (push x cluster)))
1224 (loop for ch in chars
1225 for type = (sentence-break-class ch)
1226 do (cond
1227 ((and (eql last-seen :cr) (eql type :lf)) (nobrk ch) (flush) (setf last-seen nil))
1228 ((eql last-seen :cr) (brk ch) (setf last-seen nil))
1229 ((eql type :cr) (nobrk ch) (setf last-seen :cr))
1230 ((eql type :lf) (brk ch) (setf last-seen nil))
1231 ((eql type :sep) (brk ch) (setf last-seen nil))
1232 ((and last-seen (or (eql type :extend) (eql type :format)))
1233 (nobrk ch))
1234 ((eql type :close)
1235 (unless (eql last-seen :close) (flush))
1236 (nobrk ch) (setf last-seen :close sp-run nil))
1237 ((eql type :sp)
1238 (unless (or (and (not sp-run) (eql last-seen :close)) (eql last-seen :sp))
1239 (flush) (setf sp-run t))
1240 (nobrk ch) (setf last-seen :sp))
1241 (t (flush) (nobrk ch) (setf last-seen type sp-run nil))))
1242 (flush) (nreverse clusters))))
1244 (defun sentences (string)
1245 #!+sb-doc
1246 "Breaks STRING into sentences acording to the default
1247 sentence breaking rules specified in UAX #29"
1248 (let ((special-handling '(:close :sp :sep :cr :lf :scontinue :sterm :aterm))
1249 (chars (sentence-prebreak string))
1250 sentence sentences state)
1251 (flatpush (car chars) sentence)
1252 (do ((first (car chars) second)
1253 (tail (cdr chars) (cdr tail))
1254 (second (cadr chars) (cadr tail))
1255 (third (caddr chars) (caddr tail)))
1256 ((not first)
1257 (progn
1258 ; Shake off last sentence
1259 (when sentence (push (nreverse sentence) sentences))
1260 (nreverse (mapcar #'(lambda (l) (coerce l 'string)) sentences))))
1261 (flet ((brk () (push (nreverse sentence) sentences)
1262 (setf sentence nil) (flatpush second sentence))
1263 (nobrk () (flatpush second sentence)))
1264 (let ((c1 (sentence-break-class first))
1265 (c2 (sentence-break-class second))
1266 (c3 (sentence-break-class third)))
1267 (cond
1268 ((eql state :brk-next) (brk) (setf state nil))
1269 ((eql state :nobrk-next) (nobrk) (setf state nil))
1270 ((member c1 '(:sep :cr :lf)) (brk))
1271 ((and (eql c1 :aterm) (eql c2 :numeric)) (nobrk))
1272 ((and (eql c1 :upper) (eql c2 :aterm)
1273 (eql c3 :upper)) (nobrk) (setf state :nobrk-next))
1274 ((or (and (member c1 '(:sterm :aterm)) (member c2 '(:close :sp))
1275 (member c3 '(:scontinue :sterm :aterm)))
1276 (and (member c1 '(:sterm :aterm))
1277 (member c2 '(:scontinue :sterm :aterm))))
1278 (nobrk) (when (member c2 '(:close :sp)) (setf state :nobrk-next)))
1279 ((and (member c1 '(:sterm :aterm)) (member c2 '(:close :sp))
1280 (member c3 '(:sep :cr :lf)))
1281 (nobrk) (setf state :nobrk-next)) ;; Let the linebreak call (brk)
1282 ((and (member c1 '(:sterm :aterm)) (member c2 '(:sep :cr :lf)))
1283 (nobrk)) ; Doesn't trigger rule 8
1284 ((eql c1 :sterm) ; Not ambiguous anymore, rule 8a already handled
1285 (if (member c2 '(:close :sp))
1286 (progn (nobrk) (setf state :brk-next))
1287 (brk)))
1288 ((and (eql c2 :sterm) third (not (member c3 special-handling)))
1289 (nobrk) (setf state :brk-next)) ; STerm followed by nothing important
1290 ((or (eql c1 :aterm)
1291 (and (eql c2 :aterm) third
1292 (not (member c3 special-handling)) (not (eql c3 :numeric))))
1293 ; Finally handle rule 8
1294 (if (loop for c in
1295 (if (and third (not (or (member c3 special-handling)
1296 (eql c3 :numeric))))
1297 (cdr tail) tail)
1298 for type = (sentence-break-class c) do
1299 (when (member type '(:oletter :upper :sep :cr :lf
1300 :sterm :aterm))
1301 (return nil))
1302 (when (eql type :lower) (return t)) finally (return nil))
1303 ; Ambiguous case
1304 (progn (nobrk) (setf state :nobrk-next))
1305 ; Otherwise
1306 (if (member c2 '(:close :sp :aterm))
1307 (progn (nobrk) (setf state :brk-next))
1308 (brk))))
1309 (t (nobrk))))))))
1311 (defun line-prebreak (string)
1312 (let ((chars (coerce string 'list))
1313 cluster clusters last-seen)
1314 (loop for char in chars
1315 for type = (line-break-class char)
1317 (when
1318 (and cluster
1320 (not (eql type :cm))
1321 (and (eql type :cm)
1322 (member last-seen '(nil :BK :CR :LF :NL :SP :ZW)))))
1323 (if (cdr cluster)
1324 (push (nreverse cluster) clusters)
1325 (push (car cluster) clusters))
1326 (setf cluster nil))
1327 (unless (eql type :cm) (setf last-seen type))
1328 (push char cluster))
1329 (if (cdr cluster)
1330 (push (nreverse cluster) clusters)
1331 (push (car cluster) clusters))
1332 (nreverse clusters)))
1334 (defun line-break-annotate (string)
1335 (let ((chars (line-prebreak string))
1336 first second t1 t2 tail (ret (list :cant))
1337 state after-spaces)
1338 (macrolet ((cmpush (thing)
1339 (let ((gthing (gensym)))
1340 `(let ((,gthing ,thing))
1341 (if (listp ,gthing)
1342 (loop for (c next) on ,gthing do
1343 (push c ret)
1344 (when next (push :cant ret)))
1345 (push ,thing ret)))))
1346 (between (a b action)
1347 (let ((atest (if (eql a :any) t
1348 (if (listp a)
1349 `(member t1 ,a)
1350 `(eql t1 ,a))))
1351 (btest (if (eql b :any) t
1352 (if (listp b)
1353 `(member t2 ,b)
1354 `(eql t2 ,b)))))
1355 `(when (and ,atest ,btest)
1356 (cmpush ,action)
1357 (cmpush second)
1358 (go tail))))
1359 (after-spaces (a b action)
1360 (let ((atest (if (eql a :any) t
1361 (if (listp a)
1362 `(member t1 ,a)
1363 `(eql t1 ,a))))
1364 (btest (if (eql b :any) t
1365 (if (listp b)
1366 `(member type ,b)
1367 `(eql type ,b)))))
1368 `(when
1369 (and ,atest
1370 (loop for c in tail
1371 for type = (line-break-class c :resolve t)
1373 (when (not (eql type :sp))
1374 (return ,btest))))
1375 (if (eql t2 :sp)
1376 (progn (cmpush :cant)
1377 (cmpush second)
1378 (setf state :eat-spaces)
1379 (setf after-spaces ,action)
1380 (go tail))
1381 (progn (cmpush ,action)
1382 (cmpush second)
1383 (go tail)))))))
1385 (cmpush (car chars))
1386 (setf first (car chars))
1387 (setf tail (cdr chars))
1388 (setf second (car tail))
1389 (tagbody
1391 (when (not first) (go end))
1392 (setf t1 (line-break-class first :resolve t))
1393 (setf t2 (line-break-class second :resolve t))
1394 (between :any :nil :must)
1395 (when (and (eql state :eat-spaces) (eql t2 :sp))
1396 (cmpush :cant) (cmpush second) (go tail))
1397 (between :bk :any :must)
1398 (between :cr :lf :cant)
1399 (between '(:cr :lf :nl) :any :must)
1400 (between :any '(:zw :bk :cr :lf :nl) :cant)
1401 (when after-spaces (cmpush after-spaces) (cmpush second)
1402 (setf state nil after-spaces nil) (go tail))
1403 (after-spaces :zw :any :can)
1404 (between :any :wj :cant)
1405 (between :wj :any :cant)
1406 (between :gl :any :cant)
1407 (between '(:ZW :WJ :SY :SG :SA :RI :QU :PR :PO :OP :NU :NS :NL
1408 :LF :IS :IN :ID :HL :GL :EX :CR :CP :CM :CL :CJ :CB
1409 :BK :BB :B2 :AL :AI :JL :JV :JT :H2 :H3 :XX)
1410 :gl :cant)
1411 (between :any '(:cl :cp :ex :is :sy) :cant)
1412 (after-spaces :op :any :cant)
1413 (after-spaces :qu :op :cant)
1414 (after-spaces '(:cl :cp) :ns :cant)
1415 (after-spaces :b2 :b2 :cant)
1416 (between :any :sp :cant) ;; Goes here to deal with after-spaces
1417 (between :sp :any :can)
1418 (between :any :qu :cant)
1419 (between :qu :any :cant)
1420 (between :any :cb :can)
1421 (between :cb :any :can)
1422 (between :any '(:ba :hy :ns) :cant)
1423 (between :bb :any :cant)
1424 (when (and (eql t1 :hl) (eql t2 :hy))
1425 (cmpush :cant) (cmpush second)
1426 (setf after-spaces :can) (go tail))
1427 (between '(:al :hl :id :in :nu) :in :cant)
1428 (between :id :po :cant)
1429 (between '(:al :hl) :nu :cant)
1430 (between '(:nu :po) '(:al :hl) :cant)
1431 (between :pr '(:id :al :hl) :cant)
1432 (between '(:cl :cp :nu) '(:po :pr) :cant)
1433 (between :nu '(:po :pr :nu) :cant)
1434 (between '(:po :pr) :op :cant)
1435 (between '(:po :pr :hy :is :sy) :nu :cant)
1436 (between :jl '(:jl :jv :h2 :h3) :cant)
1437 (between '(:jv :h2) '(:jv :jt) :cant)
1438 (between '(:jt :h3) :jt :cant)
1439 (between '(:jl :jv :jt :h2 :h3) '(:in :po) :cant)
1440 (between :pr '(:jl :jv :jt :h2 :h3) :cant)
1441 (between '(:al :hl :is) '(:al :hl) :cant)
1442 (between '(:al :hl :nu) :op :cant)
1443 (between :cp '(:al :hl :nu) :cant)
1444 (between :ri :ri :cant)
1445 (between :any :any :can)
1446 tail
1447 (setf first second)
1448 (setf tail (cdr tail))
1449 (setf second (car tail))
1450 (go top)
1451 end)
1452 ;; LB3 satisfied by (:any :nil) -> :must
1453 (setf ret (nreverse ret))
1454 ret)))
1456 (defun break-list-at (list n)
1457 (let ((tail list) (pre-tail nil))
1458 (loop repeat n do (setf pre-tail tail) (setf tail (cdr tail)))
1459 (setf (cdr pre-tail) nil)
1460 (values list tail)))
1462 (defun lines (string &key (margin *print-right-margin*))
1463 #!+sb-doc
1464 "Breaks STRING into lines that are no wider than :MARGIN according to the
1465 line breaking rules outlined in UAX #14. Combining marks will awsays be kept
1466 together with their base characters, and spaces (but not other types of
1467 whitespace) will be removed from the end of lines. If :MARGIN is unspecified,
1468 it defaults to 80 characters"
1469 (when (string= string "") (return-from lines (list "")))
1470 (unless margin (setf margin 80))
1471 (do* ((chars (line-break-annotate string))
1472 line lines (filled 0) last-break-distance
1473 (break-type (car chars) (car tail))
1474 (char (cadr chars) (cadr tail))
1475 (tail (cddr chars) (cddr tail)))
1476 ((not break-type)
1477 (mapcar #'(lambda (s) (coerce s 'string)) (nreverse lines)))
1478 (ecase break-type
1479 (:cant
1480 (push char line)
1481 (unless (eql (line-break-class char) :CM)
1482 (incf filled))
1483 (when last-break-distance (incf last-break-distance)))
1484 (:can
1485 (push char line)
1486 (setf last-break-distance 1)
1487 (incf filled))
1488 (:must
1489 (push char line)
1490 (setf last-break-distance 1)
1491 (incf filled)
1492 (go break)))
1493 (if (> filled margin)
1494 (go break)
1495 (go next))
1496 break
1497 (when (not last-break-distance)
1498 ;; If we don't have any line breaks, remove the last thing we added that
1499 ;; takes up space, and all its combining marks
1500 (setf last-break-distance
1501 (1+ (loop for c in line while (eql (line-break-class c) :cm) summing 1))))
1502 (multiple-value-bind (next-line this-line) (break-list-at line last-break-distance)
1503 (loop while (eql (line-break-class (car this-line)) :sp)
1504 do (setf this-line (cdr this-line)))
1505 (push (nreverse this-line) lines)
1506 (setf line next-line)
1507 (setf filled (length line))
1508 (setf last-break-distance nil))
1509 next))
1512 ;;; Collation
1513 (defconstant +maximum-variable-primary-element+
1514 #.(with-open-file (stream
1515 (merge-pathnames
1516 (make-pathname
1517 :directory
1518 '(:relative :up :up "output")
1519 :name "other-collation-info" :type "lisp-expr")
1520 sb!xc:*compile-file-truename*)
1521 :direction :input
1522 :element-type 'character)
1523 (read stream)))
1525 (defun unpack-collation-key (key)
1526 (declare (type (simple-array (unsigned-byte 32) (*)) key))
1527 (loop for value across key
1528 collect
1529 (list (ldb (byte 16 16) value)
1530 (ldb (byte 11 5) value)
1531 (ldb (byte 5 0) value))))
1533 (declaim (inline variable-p))
1534 (defun variable-p (x)
1535 (<= 1 x +maximum-variable-primary-element+))
1537 (defun collation-key (string start end)
1538 (let (char1
1539 (char2 (code-char 0))
1540 (char3 (code-char 0)))
1541 (case (- end start)
1542 (1 (setf char1 (char string start)))
1543 (2 (setf char1 (char string start)
1544 char2 (char string (+ start 1))))
1545 (3 (setf char1 (char string start)
1546 char2 (char string (+ start 1))
1547 char3 (char string (+ start 2))))
1549 ;; There are never more than three characters in a contraction, right?
1550 (return-from collation-key nil)))
1551 (let ((packed-key (gethash (pack-3-codepoints
1552 (char-code char1)
1553 (char-code char2)
1554 (char-code char3))
1555 **character-collations**)))
1556 (if packed-key
1557 (unpack-collation-key packed-key)
1558 (when (char= (code-char 0) char2 char3)
1559 (let* ((cp (char-code char1))
1560 (base
1561 (cond ((not (proplist-p char1 :unified-ideograph))
1562 #xFBC0)
1563 ((or (<= #x4E00 cp #x9FFF)
1564 (<= #xF900 cp #xFAFF))
1565 #xFB40)
1567 #xFB80)))
1568 (a (+ base (ash cp -15)))
1569 (b (logior #.(ash 1 15) (logand cp #x7FFFF))))
1570 (list (list a #x20 #x2) (list b 0 0))))))))
1572 (defun sort-key (string)
1573 (let* ((str (normalize-string string :nfd))
1574 (i 0) (len (length str)) max-match new-i
1575 sort-key
1576 after-variable)
1577 (loop while (< i len)
1579 (loop for offset from 1 to 3
1580 for index = (+ i offset)
1581 while (<= index len)
1583 (let ((key (collation-key str i index)))
1584 (when key
1585 (setf max-match key
1586 new-i index))))
1587 (loop for index from new-i below len
1588 for char = (char str index)
1589 for previous-combining-class = combining-class
1590 for combining-class = (combining-class char)
1591 until (eql combining-class 0)
1592 unless (and (>= (- index new-i) 1)
1593 ;; Combiners are sorted, we only have to look back
1594 ;; one step (see canonically-compose)
1595 (>= (combining-class (char str (1- index)))
1596 combining-class))
1598 (rotatef (char str new-i) (char str index))
1599 (let ((key (collation-key str i (1+ new-i))))
1600 (if key
1601 (setf max-match key
1602 new-i (1+ new-i))
1603 (rotatef (char str new-i) (char str index)))))
1604 (loop for key in max-match do (push key sort-key))
1605 (setf i new-i))
1606 (macrolet ((push-non-zero (obj place)
1607 `(when (/= ,obj 0)
1608 (push ,obj ,place))))
1609 (let (primary secondary tertiary quatenary)
1610 (loop for (k1 k2 k3) in (nreverse sort-key)
1612 (cond
1613 ((= k1 k2 k3 0))
1614 ((variable-p k1)
1615 (setf after-variable t)
1616 (push k1 quatenary))
1617 ((/= k1 0)
1618 (setf after-variable nil)
1619 (push k1 primary)
1620 (push-non-zero k2 secondary)
1621 (push-non-zero k3 tertiary)
1622 (push #xFFFF quatenary))
1623 ((/= k3 0)
1624 (unless after-variable
1625 (push-non-zero k2 secondary)
1626 (push k3 tertiary)
1627 (push #xFFFF quatenary)))))
1628 (concatenate 'vector
1629 (nreverse primary) #(0) (nreverse secondary) #(0)
1630 (nreverse tertiary) #(0) (nreverse quatenary))))))
1632 (defun vector< (vector1 vector2)
1633 (loop for i across vector1
1634 for j across vector2
1636 (cond ((< i j) (return-from vector< t))
1637 ((> i j) (return-from vector< nil))))
1638 ;; If there's no differences, shortest vector wins
1639 (< (length vector1) (length vector2)))
1641 (defun unicode= (string1 string2 &key (start1 0) end1 (start2 0) end2 (strict t))
1642 #!+sb-doc
1643 "Determines whether STRING1 and STRING2 are canonically equivalent according
1644 to Unicode. The START and END arguments behave like the arguments to STRING=.
1645 If :STRICT is NIL, UNICODE= tests compatibility equavalence instead."
1646 (let ((str1 (normalize-string (subseq string1 start1 end1) (if strict :nfd :nfkd)))
1647 (str2 (normalize-string (subseq string2 start2 end2) (if strict :nfd :nfkd))))
1648 (string= str1 str2)))
1650 (defun unicode-equal (string1 string2 &key (start1 0) end1 (start2 0) end2 (strict t))
1651 #!+sb-doc
1652 "Determines whether STRING1 and STRING2 are canonically equivalent after
1653 casefoldin8 (that is, ignoring case differences) according to Unicode. The
1654 START and END arguments behave like the arguments to STRING=. If :STRICT is
1655 NIL, UNICODE= tests compatibility equavalence instead."
1656 (let ((str1 (normalize-string (subseq string1 start1 end1) (if strict :nfd :nfkd)))
1657 (str2 (normalize-string (subseq string2 start2 end2) (if strict :nfd :nfkd))))
1658 (string=
1659 (normalize-string (casefold str1) (if strict :nfd :nfkd))
1660 (normalize-string (casefold str2) (if strict :nfd :nfkd)))))
1662 (defun unicode< (string1 string2 &key (start1 0) end1 (start2 0) end2)
1663 #!+sb-doc
1664 "Determines whether STRING1 sorts before STRING2 using the Unicode Collation
1665 Algorithm, The function uses an untailored Default Unicode Collation Element Table
1666 to produce the sort keys. The function uses the Shifted method for dealing
1667 with variable-weight characters, as described in UTS #10"
1668 (let* ((s1 (subseq string1 start1 end1))
1669 (s2 (subseq string2 start2 end2))
1670 (k1 (sort-key s1)) (k2 (sort-key s2)))
1671 (if (equalp k1 k2)
1672 (string< (normalize-string s1 :nfd) (normalize-string s2 :nfd))
1673 (vector< k1 k2))))
1675 (defun unicode<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
1676 #!+sb-doc
1677 "Tests if STRING1 and STRING2 are either UNICODE< or UNICODE="
1679 (unicode= string1 string2 :start1 start1 :end1 end1
1680 :start2 start2 :end2 end2)
1681 (unicode< string1 string2 :start1 start1 :end1 end1
1682 :start2 start2 :end2 end2)))
1684 (defun unicode> (string1 string2 &key (start1 0) end1 (start2 0) end2)
1685 #!+sb-doc
1686 "Tests if STRING2 is UNICODE< STRING1."
1687 (unicode< string2 string1 :start1 start2 :end1 end2
1688 :start2 start1 :end2 end1))
1690 (defun unicode>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
1691 #!+sb-doc
1692 "Tests if STRING1 and STRING2 are either UNICODE= or UNICODE>"
1694 (unicode= string1 string2 :start1 start1 :end1 end1
1695 :start2 start2 :end2 end2)
1696 (unicode> string1 string2 :start1 start1 :end1 end1
1697 :start2 start2 :end2 end2)))
1700 ;;; Confusable detection
1702 (defun canonically-deconfuse (string)
1703 (let (ret (i 0) new-i (len (length string))
1704 best-node)
1705 (loop while (< i len) do
1706 (loop for offset from 1 to 5
1707 while (<= (+ i offset) len)
1709 (let ((node (gethash (subseq string i (+ i offset))
1710 **confusables**)))
1711 (when node (setf best-node node new-i (+ i offset)))))
1712 (cond
1713 (best-node (push best-node ret) (setf i new-i))
1714 (t (push (subseq string i (1+ i)) ret) (incf i)))
1715 (setf best-node nil new-i nil))
1716 (apply #'concatenate 'string (nreverse ret))))
1718 (defun confusable-p (string1 string2 &key (start1 0) end1 (start2 0) end2)
1719 #!+sb-doc
1720 "Determines whether STRING1 and STRING2 could be visually confusable
1721 according to the IDNA confusableSummary.txt table"
1722 (let* ((form #!+sb-unicode :nfd #!-sb-unicode :nfc)
1723 (str1 (normalize-string (subseq string1 start1 end1) form))
1724 (str2 (normalize-string (subseq string2 start2 end2) form))
1725 (skeleton1 (normalize-string (canonically-deconfuse str1) form))
1726 (skeleton2 (normalize-string (canonically-deconfuse str2) form)))
1727 (string= skeleton1 skeleton2)))