Debian Codegraph @

src/trivial-utf-16.lisp

raw
1 ;;; -*- Mode: LISP; Syntax: Ansi-Common-Lisp; Base: 10; Package: UAX-15 -*-
2
3 (in-package :uax-15)
4
5 (eval-when (:compile-toplevel :load-toplevel :execute)
6 (case char-code-limit
7 (#x10000 (pushnew :utf-16 *features*))
8 (#x110000 (pushnew :utf-32 *features*))
9 (t (error "Unexpected char-code-limit for utf-16 or utf-32; Unicode support for those systems seems unlikely."))))
10
11 (defun unicode-point-p (p)
12 (and (integerp p)
13 (>= p 0)
14 (<= p #x10FFFF)))
15
16 (deftype high-surrogate ()
17 "A Unicode High Surrogate."
18 '(integer #xD800 #xDBFF))
19
20 (deftype low-surrogate ()
21 "A Unicode Low Surrogate."
22 '(integer #xDC00 #xDFFF))
23
24 (deftype unicode-point ()
25 "A Unicode code point."
26 '(and (integer 0 #x10FFFF)
27 (satisfies unicode-point-p)))
28
29 (deftype unicode-string ()
30 "A vector of Unicode code points."
31 '(vector unicode-point))
32
33 (defun codepoint-to-unicode-point (int)
34 "Translates an integer to a unicode-point"
35 (coerce int 'unicode-point))
36
37 (defun codepoint-as-utf-16 (codepoint)
38 "Translate a Unicode code point to its UTF-16 representation. Returns a list of one or two codepoints. Passes surrogate code points straight through."
39 (check-type codepoint unicode-point "a Unicode code point")
40 (if (< codepoint #x10000)
41 (list codepoint) ; If it's BMP, just return it.
42 (let ((twentybits (- codepoint #x10000)))
43 (list (dpb #b110110 (byte 6 10) (ldb (byte 10 10) twentybits))
44 (dpb #b110111 (byte 6 10) (ldb (byte 10 0) twentybits))))))
45
46 (defun surrogates-to-codepoint (high-surrogate low-surrogate)
47 "Translate a pair of surrogate codepoints to a non-BMP codepoint. Returns the codepoint as an integer."
48 (check-type high-surrogate high-surrogate "a Unicode high-surrogate")
49 (check-type low-surrogate low-surrogate "a Unicode low-surrogate")
50 (let ((low-bits (ldb (byte 10 0) low-surrogate))
51 (high-bits (ldb (byte 10 0) high-surrogate)))
52 (+ #x10000 (dpb high-bits (byte 10 10) low-bits))))
53
54 (defun encode-utf-16 (unicode-string)
55 "Turn a vector of Unicode code points into a vector of UTF-16 code units. Indifferent to unpaired surrogates."
56 (coerce
57 (mapcan #'codepoint-as-utf-16 (coerce unicode-string 'list))
58 '(vector (unsigned-byte 16))))
59
60 (defun decode-utf-16 (utf-16-string)
61 "Turn a vector of UTF-16 code units into a vector of Unicode code points. Passes unpaired surrogate codepoints straight through."
62 (let ((result '()))
63 (dotimes (i (length utf-16-string))
64 (let ((codepoint (elt utf-16-string i)))
65 ;; The first branch converts high-surrogate followed by low-surrogate.
66 ;; The second branch ignores low-surrogate preceded by high-surrogate.
67 ;; FIXME: I'm sure this can be done better.
68 (cond ((and (typep codepoint 'high-surrogate)
69 (< i (1- (length utf-16-string)))
70 (typep (elt utf-16-string (1+ i)) 'low-surrogate))
71 (push (surrogates-to-codepoint codepoint (elt utf-16-string (1+ i))) result))
72 ((and (typep codepoint 'low-surrogate)
73 (> i 0)
74 (typep (elt utf-16-string (1- i)) 'high-surrogate)))
75 (t (push codepoint result)))))
76 (coerce (nreverse result) 'unicode-string)))
77
78 (declaim (inline from-unicode-string))
79 (defun from-unicode-string (unicode-string)
80 "Take a vector of Unicode code points and turn it into a Lisp string."
81 #+utf-16 (setf unicode-string (encode-utf-16 unicode-string))
82 (let* ((string-size (length unicode-string))
83 (lisp-string (make-array string-size :element-type 'character)))
84 (dotimes (i string-size)
85 (setf (aref lisp-string i) (code-char (aref unicode-string i))))
86 lisp-string))
87
88 (declaim (inline to-unicode-string))
89 (defun to-unicode-string (lisp-string)
90 "Take a Lisp string and turn it into a vector of Unicode code points."
91 (let* ((string-size (length lisp-string))
92 (unicode-string (make-array string-size)))
93 (dotimes (i string-size)
94 (setf (aref unicode-string i) (char-code (aref lisp-string i))))
95 #+utf-16 (setf unicode-string (decode-utf-16 unicode-string))
96 unicode-string))
97
98 (defun char-from-hexstring (hexpoint-str)
99 "Translating the first char from *unicode-data* hex codepoint string to a lisp character."
100 (let ((char #+utf-32 (parse-hex-string-to-char hexpoint-str)
101 #+utf-16 (aref
102 (from-unicode-string
103 (coerce
104 (codepoint-as-utf-16
105 (parse-integer hexpoint-str :radix 16))
106 'vector))
107 0)))
108 char))