]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/cxtm/unicode.lisp
Implement CTM isa and ako with atomic core subject protection
[topic-maps.git] / src / cxtm / unicode.lisp
1 (in-package #:topic-maps.cxtm)
2
3 (defun unicode-assigned-p (n)
4 (loop with low = 0 with high = (1- (/ (length *assigned-ranges*) 2))
5 while (<= low high) for mid = (floor (+ low high) 2)
6 for begin = (aref *assigned-ranges* (* mid 2))
7 for end = (aref *assigned-ranges* (1+ (* mid 2))) do
8 (cond ((< n begin) (setf high (1- mid)))
9 ((> n end) (setf low (1+ mid))) (t (return t)))))
10
11 (defun combining-class (n) (or (first (gethash n *combining-classes*)) 0))
12 (defun decomposition (n)
13 (cond ((<= #xac00 n #xd7a3)
14 (let ((index (- n #xac00)))
15 (append (list (+ #x1100 (floor index 588))
16 (+ #x1161 (floor (mod index 588) 28)))
17 (unless (zerop (mod index 28)) (list (+ #x11a7 (mod index 28)))))))
18 ((gethash n *decompositions*)
19 (mapcan #'decomposition (copy-list (gethash n *decompositions*))))
20 (t (list n))))
21 (defun composition (a b)
22 (cond ((and (<= #x1100 a #x1112) (<= #x1161 b #x1175))
23 (+ #xac00 (* (- a #x1100) 588) (* (- b #x1161) 28)))
24 ((and (<= #xac00 a #xd7a3) (zerop (mod (- a #xac00) 28))
25 (<= #x11a8 b #x11c2)) (+ a (- b #x11a7)))
26 (t (first (gethash (+ (* a #x110000) b) *compositions*)))))
27 (defun nfc (text)
28 "UAX #15 canonical decomposition, stable CCC ordering, blocked composition."
29 (let ((ordered (make-array 0 :adjustable t :fill-pointer 0)))
30 (loop for c across text do
31 (unless (unicode-assigned-p (char-code c))
32 (fail :unsupported-unicode-version
33 (format nil "U+~X is unassigned in the pinned Unicode 15.1.0 data." (char-code c)) t))
34 (dolist (n (decomposition (char-code c)))
35 (vector-push-extend n ordered)
36 (let ((i (1- (length ordered))) (ccc (combining-class n)))
37 (when (plusp ccc)
38 (loop while (and (plusp i) (> (combining-class (aref ordered (1- i))) ccc)) do
39 (rotatef (aref ordered i) (aref ordered (1- i))) (decf i))))))
40 (let ((result (make-array 0 :adjustable t :fill-pointer 0))
41 (starter nil) (last-class 0))
42 (loop for n across ordered for ccc = (combining-class n)
43 for combined = (and starter (or (< last-class ccc) (zerop last-class))
44 (composition (aref result starter) n)) do
45 (if combined
46 (setf (aref result starter) combined)
47 (progn (vector-push-extend n result)
48 (when (zerop ccc) (setf starter (1- (length result))))
49 (setf last-class ccc))))
50 (map 'string (lambda (n) (or (code-char n)
51 (fail :unsupported-character "Lisp character repertoire is insufficient." t))) result))))