1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#!/usr/local/bin/clisp -C
;;; Creation of CLISP's uni_names.h from the UnicodeData.txt table.
;;; Bruno Haible 2000-12-28
(defparameter add-comments nil)
(defstruct unicode-char
(code nil :type integer)
(name nil :type string)
word-indices
word-indices-index
)
(defstruct word-list
(hashed nil :type hash-table)
(sorted nil :type list)
size ; number of characters total
length ; number of words
)
(defun main (inputfile outputfile)
(declare (type string inputfile outputfile))
#+UNICODE (setq *default-file-encoding* charset:utf-8)
(let ((all-chars '()))
;; Read all characters and names from the input file.
(with-open-file (istream inputfile :direction :input)
(loop
(let ((line (read-line istream nil nil)))
(unless line (return))
(let* ((i1 (position #\; line))
(i2 (position #\; line :start (1+ i1)))
(code-string (subseq line 0 i1))
(code (parse-integer code-string :radix 16))
(name-string (subseq line (1+ i1) i2)))
; Ignore characters whose name starts with "<".
(unless (eql (char name-string 0) #\<)
; Also ignore Hangul syllables; they are treated specially.
(unless (<= #xAC00 code #xD7A3)
(push (make-unicode-char :code code :name name-string)
all-chars
) ) )
) ) ) )
(setq all-chars (nreverse all-chars))
;; Split into words.
(let ((words-by-length (make-array 0 :adjustable t)))
(dolist (name (cons "HANGUL SYLLABLE" (mapcar #'unicode-char-name all-chars)))
(let ((i1 0))
(loop
(when (>= i1 (length name)) (return))
(let ((i2 (or (position #\Space name :start i1) (length name))))
(let* ((word (subseq name i1 i2))
(len (length word)))
(when (>= len (length words-by-length))
(adjust-array words-by-length (1+ len))
)
(unless (aref words-by-length len)
(setf (aref words-by-length len)
(make-word-list
:hashed (make-hash-table :test #'equal)
:sorted '()
) ) )
(let ((word-list (aref words-by-length len)))
(unless (gethash word (word-list-hashed word-list))
(setf (gethash word (word-list-hashed word-list)) t)
(push word (word-list-sorted word-list))
) )
)
(setq i1 (1+ i2))
) ) ) )
;; Sort the word lists.
(dotimes (len (length words-by-length))
(unless (aref words-by-length len)
(setf (aref words-by-length len)
(make-word-list
:hashed (make-hash-table :test #'equal)
:sorted '()
) ) )
(let ((word-list (aref words-by-length len)))
(setf (word-list-sorted word-list)
(sort (word-list-sorted word-list) #'string<)
)
(setf (word-list-size word-list)
(reduce #'+ (mapcar #'length (word-list-sorted word-list)))
)
(setf (word-list-length word-list)
(length (word-list-sorted word-list))
) ) )
;; Output the tables.
(with-open-file (ostream outputfile :direction :output
#+UNICODE :external-format #+UNICODE charset:ascii)
(format ostream "/*~%")
(format ostream " * ~A~%" (file-namestring outputfile))
(format ostream " *~%")
(format ostream " * Unicode character name table.~%")
(format ostream " * Generated automatically by the gen-uninames utility.~%")
(format ostream " */~%")
(format ostream "~%")
(format ostream "static const char unicode_name_words[~D] = {~%"
(let ((sum 0))
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(incf sum (word-list-size word-list))
) )
sum
) )
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(dolist (word (word-list-sorted word-list))
(format ostream " ~{ '~C',~}~%" (coerce word 'list))
) ) )
(format ostream "};~%")
(format ostream "#define UNICODE_CHARNAME_NUM_WORDS ~D~%"
(let ((sum 0))
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(incf sum (word-list-length word-list))
) )
sum
) )
#| ; Redundant data
(format ostream "static const uint16 unicode_name_word_offsets[~D] = {~%"
(let ((sum 0))
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(incf sum (word-list-length word-list))
) )
sum
) )
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(when (word-list-sorted word-list)
(format ostream " ")
(do ((l (word-list-sorted word-list) (cdr l))
(offset 0 (+ offset (length (car l)))))
((endp l))
(format ostream "~<~% ~0,79:; ~D,~>" offset)
)
(format ostream "~%")
) ) )
(format ostream "};~%")
|#
(format ostream "static const struct { uintL extra_offset; uint16 ind_offset; } unicode_name_by_length[~D] = {~%"
(1+ (length words-by-length))
)
(let ((extra-offset 0)
(ind-offset 0))
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(format ostream " { ~D, ~D },~%" extra-offset ind-offset)
(incf extra-offset (word-list-size word-list))
(incf ind-offset (word-list-length word-list))
) )
(format ostream " { ~D, ~D }~%" extra-offset ind-offset)
)
(format ostream "};~%")
(let ((ind-offset 0))
(dotimes (len (length words-by-length))
(let ((word-list (aref words-by-length len)))
(dolist (word (word-list-sorted word-list))
(setf (gethash word (word-list-hashed word-list)) ind-offset)
(incf ind-offset)
) ) ) )
(dolist (word '("HANGUL" "SYLLABLE"))
(format ostream "#define UNICODE_CHARNAME_WORD_~A ~D~%" word
(gethash word (word-list-hashed (aref words-by-length (length word))))
) )
;; Compute the word-indices for every unicode-char.
(dolist (uc all-chars)
(let ((name (unicode-char-name uc))
(indices '()))
(let ((i1 0))
(loop
(when (>= i1 (length name)) (return))
(let ((i2 (or (position #\Space name :start i1) (length name))))
(let* ((word (subseq name i1 i2))
(len (length word)))
(push (gethash word (word-list-hashed (aref words-by-length len)))
indices
)
)
(setq i1 (1+ i2))
) ) )
(setf (unicode-char-word-indices uc)
(coerce (nreverse indices) 'vector)
)
) )
;; Sort the list of unicode-chars by word-indices.
(setq all-chars
(sort all-chars
(lambda (vec1 vec2)
(let ((len1 (length vec1))
(len2 (length vec2)))
(do ((i 0 (1+ i)))
(nil)
(if (< i len2)
(if (< i len1)
(cond ((< (aref vec1 i) (aref vec2 i)) (return t))
((> (aref vec1 i) (aref vec2 i)) (return nil))
)
(return t)
)
(return nil)
) ) ) )
:key #'unicode-char-word-indices
) )
;; Output the word-indices.
(format ostream "static const uint16 unicode_names[~D] = {~%"
(reduce #'+ (mapcar (lambda (uc) (length (unicode-char-word-indices uc))) all-chars))
)
(let ((i 0))
(dolist (uc all-chars)
(format ostream " ~{ ~D,~}"
(maplist (lambda (r) (+ (* 2 (car r)) (if (cdr r) 1 0)))
(coerce (unicode-char-word-indices uc) 'list)
)
)
(when add-comments
(format ostream "~40T/* ~A */" (unicode-char-name uc))
)
(format ostream "~%")
(setf (unicode-char-word-indices-index uc) i)
(incf i (length (unicode-char-word-indices uc)))
) )
(format ostream "};~%")
(format ostream "static const struct { uint16 code; uint16 name; } unicode_name_to_code[~D] = {~%"
(length all-chars)
)
(dolist (uc all-chars)
(format ostream " { 0x~4,'0X, ~D },"
(unicode-char-code uc)
(unicode-char-word-indices-index uc)
)
(when add-comments
(format ostream "~21T/* ~A */" (unicode-char-name uc))
)
(format ostream "~%")
)
(format ostream "};~%")
(format ostream "static const struct { uint16 code; uint16 name; } unicode_code_to_name[~D] = {~%"
(length all-chars)
)
(dolist (uc (sort (copy-list all-chars) #'< :key #'unicode-char-code))
(format ostream " { 0x~4,'0X, ~D },"
(unicode-char-code uc)
(unicode-char-word-indices-index uc)
)
(when add-comments
(format ostream "~21T/* ~A */" (unicode-char-name uc))
)
(format ostream "~%")
)
(format ostream "};~%")
(format ostream "#define UNICODE_CHARNAME_MAX_LENGTH ~D~%"
(reduce #'max (mapcar (lambda (uc) (length (unicode-char-name uc))) all-chars))
)
(format ostream "#define UNICODE_CHARNAME_MAX_WORDS ~D~%"
(reduce #'max (mapcar (lambda (uc) (length (unicode-char-word-indices uc))) all-chars))
)
)
) ) )
(main (first *args*) (second *args*))
|