]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/unicode.lisp
Support explicitly typed Topic Names with merge and CXTM evidence
[topic-maps.git] / src / ctm / unicode.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defun scalar-character (code position)
4 (unless (and (<= 0 code #x10ffff) (not (<= #xd800 code #xdfff)))
5 (problem 'ctm-rejection :invalid-unicode position "Not a Unicode scalar value."))
6 (or (code-char code)
7 (problem 'ctm-unsupported :character-repertoire position
8 "The Common Lisp implementation cannot represent this Unicode character.")))
9
10 (defun decode-utf8 (octets &key (position 0) (strip-bom nil))
11 "Strict UTF-8 decoding without implementation-specific external-format flags."
12 (let* ((size (length octets))
13 (i (if (and strip-bom (>= size 3)
14 (= (aref octets 0) #xef) (= (aref octets 1) #xbb)
15 (= (aref octets 2) #xbf)) 3 0)))
16 (with-output-to-string (output)
17 (loop while (< i size) do
18 (let* ((first (aref octets i))
19 (width (cond ((< first #x80) 1) ((<= #xc2 first #xdf) 2)
20 ((<= #xe0 first #xef) 3) ((<= #xf0 first #xf4) 4)
21 (t 0))))
22 (when (or (zerop width) (> (+ i width) size))
23 (problem 'ctm-rejection :invalid-utf8 (+ position i)
24 "Invalid or incomplete UTF-8 sequence." :byte))
25 (let ((code (logand first (ecase width (1 #x7f) (2 #x1f) (3 #x0f) (4 #x07)))))
26 (loop for j from 1 below width for byte = (aref octets (+ i j)) do
27 (unless (<= #x80 byte #xbf)
28 (problem 'ctm-rejection :invalid-utf8 (+ position i j)
29 "Expected UTF-8 continuation byte." :byte))
30 (setf code (+ (ash code 6) (logand byte #x3f))))
31 (unless (and (>= code (ecase width (1 0) (2 #x80) (3 #x800) (4 #x10000)))
32 (<= code #x10ffff) (not (<= #xd800 code #xdfff)))
33 (problem 'ctm-rejection :invalid-utf8 (+ position i)
34 "Overlong or invalid Unicode UTF-8 sequence." :byte))
35 (write-char (scalar-character code (+ position i)) output))
36 (incf i width))))))
37
38 (defun unescape-iri (text position)
39 "CTM 3.3.3: decode consecutive percent octets as UTF-8, preserving other chars."
40 (with-output-to-string (out)
41 (loop for i from 0 below (length text) do
42 (if (char= (char text i) #\%)
43 (let ((bytes nil) (start i))
44 (loop while (and (< i (length text)) (char= (char text i) #\%)) do
45 (unless (< (+ i 2) (length text))
46 (problem 'ctm-rejection :invalid-percent-escape (+ position i) "Incomplete IRI escape."))
47 (let ((a (digit-char-p (char text (1+ i)) 16))
48 (b (digit-char-p (char text (+ i 2)) 16)))
49 (unless (and a b)
50 (problem 'ctm-rejection :invalid-percent-escape (+ position i) "IRI escape needs two hex digits."))
51 (push (+ (* 16 a) b) bytes))
52 (incf i 3))
53 (write-string (decode-utf8 (coerce (nreverse bytes) '(vector (unsigned-byte 8)))
54 :position (+ position start)) out)
55 (decf i))
56 (write-char (char text i) out)))))