1 #!/usr/local/bin/clisp -C
3 ;;; Creation of gnulib's uninames.h from the UnicodeData.txt table.
4 ;;; Bruno Haible 2000-12-28
6 (defparameter add-comments nil)
8 (defstruct unicode-char
9 (code nil :type integer)
10 (name nil :type string)
16 (hashed nil :type hash-table)
17 (sorted nil :type list)
18 size ; number of characters total
19 length ; number of words
22 (defun main (inputfile outputfile)
23 (declare (type string inputfile outputfile))
24 #+UNICODE (setq *default-file-encoding* charset:utf-8)
25 (let ((all-chars '()))
26 ;; Read all characters and names from the input file.
27 (with-open-file (istream inputfile :direction :input)
29 (let ((line (read-line istream nil nil)))
30 (unless line (return))
31 (let* ((i1 (position #\; line))
32 (i2 (position #\; line :start (1+ i1)))
33 (code-string (subseq line 0 i1))
34 (code (parse-integer code-string :radix 16))
35 (name-string (subseq line (1+ i1) i2)))
36 ; Ignore characters whose name starts with "<".
37 (unless (eql (char name-string 0) #\<)
38 ; Also ignore Hangul syllables; they are treated specially.
39 (unless (<= #xAC00 code #xD7A3)
40 ; Also ignore CJK compatibility ideographs; they are treated
42 (unless (or (<= #xF900 code #xFA2D) (<= #xFA30 code #xFA6A)
43 (<= #xFA70 code #xFAD9) (<= #x2F800 code #x2FA1D))
44 ; Transform the code so that it fits in 16 bits. In
45 ; Unicode 3.1 the following ranges are used.
46 ; 0x00000..0x04DFF >>12= 0x00..0x04 -> 0x0..0x4
47 ; 0x0A000..0x0A4FF >>12= 0x0A -> 0x5
48 ; 0x0F900..0x0FFFF >>12= 0x0F -> 0x6
49 ; 0x10300..0x104FF >>12= 0x10 -> 0x7
50 ; 0x12000..0x12473 >>12= 0x12 -> 0x8
51 ; 0x1D000..0x1D7DD >>12= 0x1D -> 0x9
52 ; 0x2F800..0x2FAFF >>12= 0x2F -> 0xA
53 ; 0xE0000..0xE00FF >>12= 0xE0 -> 0xB
57 ((#x00 #x01 #x02 #x03 #x04) (ash x -12))
65 (t (error "Update the transform function for 0x~5,'0X" x))
70 (push (make-unicode-char :code (transform code)
75 (setq all-chars (nreverse all-chars))
77 (let ((words-by-length (make-array 0 :adjustable t)))
78 (dolist (name (list* "HANGUL SYLLABLE" "CJK COMPATIBILITY" (mapcar #'unicode-char-name all-chars)))
81 (when (>= i1 (length name)) (return))
82 (let ((i2 (or (position #\Space name :start i1) (length name))))
83 (let* ((word (subseq name i1 i2))
85 (when (>= len (length words-by-length))
86 (adjust-array words-by-length (1+ len))
88 (unless (aref words-by-length len)
89 (setf (aref words-by-length len)
91 :hashed (make-hash-table :test #'equal)
94 (let ((word-list (aref words-by-length len)))
95 (unless (gethash word (word-list-hashed word-list))
96 (setf (gethash word (word-list-hashed word-list)) t)
97 (push word (word-list-sorted word-list))
102 ;; Sort the word lists.
103 (dotimes (len (length words-by-length))
104 (unless (aref words-by-length len)
105 (setf (aref words-by-length len)
107 :hashed (make-hash-table :test #'equal)
110 (let ((word-list (aref words-by-length len)))
111 (setf (word-list-sorted word-list)
112 (sort (word-list-sorted word-list) #'string<)
114 (setf (word-list-size word-list)
115 (reduce #'+ (mapcar #'length (word-list-sorted word-list)))
117 (setf (word-list-length word-list)
118 (length (word-list-sorted word-list))
120 ;; Output the tables.
121 (with-open-file (ostream outputfile :direction :output
122 #+UNICODE :external-format #+UNICODE charset:ascii)
123 (format ostream "/*~%")
124 (format ostream " * ~A~%" (file-namestring outputfile))
125 (format ostream " *~%")
126 (format ostream " * Unicode character name table.~%")
127 (format ostream " * Generated automatically by the gen-uninames utility.~%")
128 (format ostream " */~%")
129 (format ostream "~%")
130 (format ostream "static const char unicode_name_words[~D] = {~%"
132 (dotimes (len (length words-by-length))
133 (let ((word-list (aref words-by-length len)))
134 (incf sum (word-list-size word-list))
138 (dotimes (len (length words-by-length))
139 (let ((word-list (aref words-by-length len)))
140 (dolist (word (word-list-sorted word-list))
141 (format ostream " ~{ '~C',~}~%" (coerce word 'list))
143 (format ostream "};~%")
144 (format ostream "#define UNICODE_CHARNAME_NUM_WORDS ~D~%"
146 (dotimes (len (length words-by-length))
147 (let ((word-list (aref words-by-length len)))
148 (incf sum (word-list-length word-list))
153 (format ostream "static const uint16_t unicode_name_word_offsets[~D] = {~%"
155 (dotimes (len (length words-by-length))
156 (let ((word-list (aref words-by-length len)))
157 (incf sum (word-list-length word-list))
161 (dotimes (len (length words-by-length))
162 (let ((word-list (aref words-by-length len)))
163 (when (word-list-sorted word-list)
165 (do ((l (word-list-sorted word-list) (cdr l))
166 (offset 0 (+ offset (length (car l)))))
168 (format ostream "~<~% ~0,79:; ~D,~>" offset)
170 (format ostream "~%")
172 (format ostream "};~%")
174 (format ostream "static const struct { uint16_t extra_offset; uint16_t ind_offset; } unicode_name_by_length[~D] = {~%"
175 (1+ (length words-by-length))
177 (let ((extra-offset 0)
179 (dotimes (len (length words-by-length))
180 (let ((word-list (aref words-by-length len)))
181 (format ostream " { ~D, ~D },~%" extra-offset ind-offset)
182 (incf extra-offset (word-list-size word-list))
183 (incf ind-offset (word-list-length word-list))
185 (format ostream " { ~D, ~D }~%" extra-offset ind-offset)
187 (format ostream "};~%")
188 (let ((ind-offset 0))
189 (dotimes (len (length words-by-length))
190 (let ((word-list (aref words-by-length len)))
191 (dolist (word (word-list-sorted word-list))
192 (setf (gethash word (word-list-hashed word-list)) ind-offset)
195 (dolist (word '("HANGUL" "SYLLABLE" "CJK" "COMPATIBILITY"))
196 (format ostream "#define UNICODE_CHARNAME_WORD_~A ~D~%" word
197 (gethash word (word-list-hashed (aref words-by-length (length word))))
199 ;; Compute the word-indices for every unicode-char.
200 (dolist (uc all-chars)
201 (let ((name (unicode-char-name uc))
205 (when (>= i1 (length name)) (return))
206 (let ((i2 (or (position #\Space name :start i1) (length name))))
207 (let* ((word (subseq name i1 i2))
209 (push (gethash word (word-list-hashed (aref words-by-length len)))
215 (setf (unicode-char-word-indices uc)
216 (coerce (nreverse indices) 'vector)
219 ;; Sort the list of unicode-chars by word-indices.
223 (let ((len1 (length vec1))
224 (len2 (length vec2)))
229 (cond ((< (aref vec1 i) (aref vec2 i)) (return t))
230 ((> (aref vec1 i) (aref vec2 i)) (return nil))
236 :key #'unicode-char-word-indices
238 ;; Output the word-indices.
239 (format ostream "static const uint16_t unicode_names[~D] = {~%"
240 (reduce #'+ (mapcar (lambda (uc) (length (unicode-char-word-indices uc))) all-chars))
243 (dolist (uc all-chars)
244 (format ostream " ~{ ~D,~}"
245 (maplist (lambda (r) (+ (* 2 (car r)) (if (cdr r) 1 0)))
246 (coerce (unicode-char-word-indices uc) 'list)
250 (format ostream "~40T/* ~A */" (unicode-char-name uc))
252 (format ostream "~%")
253 (setf (unicode-char-word-indices-index uc) i)
254 (incf i (length (unicode-char-word-indices uc)))
256 (format ostream "};~%")
257 (format ostream "static const struct { uint16_t code; uint32_t name:24; }~%")
258 (format ostream "#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)~%__attribute__((__packed__))~%#endif~%")
259 (format ostream "unicode_name_to_code[~D] = {~%"
262 (dolist (uc all-chars)
263 (format ostream " { 0x~4,'0X, ~D },"
264 (unicode-char-code uc)
265 (unicode-char-word-indices-index uc)
268 (format ostream "~21T/* ~A */" (unicode-char-name uc))
270 (format ostream "~%")
272 (format ostream "};~%")
273 (format ostream "static const struct { uint16_t code; uint32_t name:24; }~%")
274 (format ostream "#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)~%__attribute__((__packed__))~%#endif~%")
275 (format ostream "unicode_code_to_name[~D] = {~%"
278 (dolist (uc (sort (copy-list all-chars) #'< :key #'unicode-char-code))
279 (format ostream " { 0x~4,'0X, ~D },"
280 (unicode-char-code uc)
281 (unicode-char-word-indices-index uc)
284 (format ostream "~21T/* ~A */" (unicode-char-name uc))
286 (format ostream "~%")
288 (format ostream "};~%")
289 (format ostream "#define UNICODE_CHARNAME_MAX_LENGTH ~D~%"
290 (reduce #'max (mapcar (lambda (uc) (length (unicode-char-name uc))) all-chars))
292 (format ostream "#define UNICODE_CHARNAME_MAX_WORDS ~D~%"
293 (reduce #'max (mapcar (lambda (uc) (length (unicode-char-word-indices uc))) all-chars))
298 (main (first *args*) (second *args*))