[e86079]: tools-for-build / ucd.lisp Maximize Restore History

Download this file

ucd.lisp    460 lines (416 with data), 20.0 kB

  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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
(in-package "SB-COLD")
;;; Common
(defparameter *output-directory*
(merge-pathnames
(make-pathname :directory '(:relative :up "output"))
(make-pathname :directory (pathname-directory *load-truename*))))
(defparameter *page-size-exponent* 8)
(defun cp-high (cp)
(ash cp (- *page-size-exponent*)))
(defun cp-low (cp)
(ldb (byte *page-size-exponent* 0) cp))
;;; Generator
(defparameter *unicode-character-database*
(make-pathname :directory (pathname-directory *load-truename*)))
(defparameter *ucd-base* nil)
(defparameter *last-uppercase* nil)
(defparameter *uppercase-transition-count* 0)
(defparameter *different-titlecases* nil)
(defparameter *different-numerics* nil)
(defparameter *name-size* 0)
(defparameter *misc-hash* (make-hash-table :test #'equal))
(defparameter *misc-index* -1)
(defparameter *misc-table* nil)
(defparameter *misc-mapping* nil)
(defparameter *both-cases* nil)
(defparameter *decompositions* nil)
(defparameter *decomposition-length-max* nil)
(defparameter *decomposition-types* nil)
(defparameter *decomposition-base* nil)
(defun hash-misc (gc-index bidi-index ccc-index decimal-digit digit
bidi-mirrored cl-both-case-p)
(let* ((list (list gc-index bidi-index ccc-index decimal-digit digit
bidi-mirrored cl-both-case-p))
(index (gethash list *misc-hash*)))
(or index
(progn
(vector-push list *misc-table*)
(setf (gethash list *misc-hash*)
(incf *misc-index*))))))
(defun compare-misc-entry (left right)
(destructuring-bind (left-gc-index left-bidi-index left-ccc-index
left-decimal-digit left-digit left-bidi-mirrored
left-cl-both-case-p)
left
(destructuring-bind (right-gc-index right-bidi-index right-ccc-index
right-decimal-digit right-digit right-bidi-mirrored
right-cl-both-case-p)
right
(or (and left-cl-both-case-p (not right-cl-both-case-p))
(and (or left-cl-both-case-p (not right-cl-both-case-p))
(or (< left-gc-index right-gc-index)
(and (= left-gc-index right-gc-index)
(or (< left-bidi-index right-bidi-index)
(and (= left-bidi-index right-bidi-index)
(or (< left-ccc-index right-ccc-index)
(and (= left-ccc-index right-ccc-index)
(or (string< left-decimal-digit
right-decimal-digit)
(and (string= left-decimal-digit
right-decimal-digit)
(or (string< left-digit right-digit)
(and (string= left-digit
right-digit)
(string< left-bidi-mirrored
right-bidi-mirrored))))))))))))))))
(defun build-misc-table ()
(sort *misc-table* #'compare-misc-entry)
(setq *misc-mapping* (make-array (1+ *misc-index*)))
(loop for i from 0 to *misc-index*
do (setf (aref *misc-mapping*
(gethash (aref *misc-table* i) *misc-hash*))
i)))
(defun slurp-ucd ()
(setq *last-uppercase* nil)
(setq *uppercase-transition-count* 0)
(setq *different-titlecases* nil)
(setq *different-numerics* nil)
(setq *name-size* 0)
(setq *misc-hash* (make-hash-table :test #'equal))
(setq *misc-index* -1)
(setq *misc-table* (make-array 256 :fill-pointer 0))
(setq *both-cases* nil)
(setq *decompositions* 0)
(setq *decomposition-types* (make-hash-table :test #'equal))
(setq *decomposition-length-max* 0)
(setq *decomposition-base* (make-array (ash #x110000
(- *page-size-exponent*))
:initial-element nil))
(setq *ucd-base* (make-array (ash #x110000 (- *page-size-exponent*))
:initial-element nil))
(with-open-file (*standard-input*
(make-pathname :name "UnicodeData"
:type "txt"
:defaults *unicode-character-database*)
:direction :input)
(loop for line = (read-line nil nil)
while line
do (slurp-ucd-line line)))
(second-pass)
(build-misc-table)
*decompositions*)
(defun split-string (line character)
(loop for prev-position = 0 then (1+ position)
for position = (position character line :start prev-position)
collect (subseq line prev-position position)
do (unless position
(loop-finish))))
(defun init-indices (strings)
(let ((hash (make-hash-table :test #'equal)))
(loop for string in strings
for index from 0
do (setf (gethash string hash) index))
hash))
(defparameter *general-categories*
(init-indices '("Lu" "Ll" "Lt" "Lm" "Lo" "Cc" "Cf" "Co" "Cs" "Mc"
"Me" "Mn" "Nd" "Nl" "No" "Pc" "Pd" "Pe" "Pf" "Pi"
"Po" "Ps" "Sc" "Sk" "Sm" "So" "Zl" "Zp" "Zs")))
(defparameter *bidi-classes*
(init-indices '("AL" "AN" "B" "BN" "CS" "EN" "ES" "ET" "L" "LRE" "LRO"
"NSM" "ON" "PDF" "R" "RLE" "RLO" "S" "WS")))
(defparameter *block-first* nil)
;;; 3400 -- 4DB5 : cjk ideograph extension a ;Lo;0;L;;;;;N;;;;;
;;; AC00 -- D7A3 : hangul syllables ;Lo;0;L;;;;;N;;;;;
;;; D800 -- F8FF : surrogates and private use
;;; 20000 -- 2A6D6 : cjk ideograph extension b ;Lo;0;L;;;;;N;;;;;
;;; F0000 -- FFFFD : private use
;;; 100000 -- 10FFFD: private use
(defun encode-ucd-line (line code-point)
(destructuring-bind (name general-category canonical-combining-class
bidi-class decomposition-type-and-mapping
decimal-digit digit numeric bidi-mirrored
unicode-1-name iso-10646-comment simple-uppercase
simple-lowercase simple-titlecase)
line
(declare (ignore unicode-1-name iso-10646-comment))
(if (and (> (length name) 8)
(string= ", First>" name :start2 (- (length name) 8)))
(progn
(setq *block-first* code-point)
nil)
(let* ((gc-index (or (gethash general-category *general-categories*)
(error "unknown general category ~A"
general-category)))
(bidi-index (or (gethash bidi-class *bidi-classes*)
(error "unknown bidirectional class ~A"
bidi-class)))
(ccc-index (parse-integer canonical-combining-class))
(digit-index (unless (string= "" decimal-digit)
(parse-integer decimal-digit)))
(upper-index (unless (string= "" simple-uppercase)
(parse-integer simple-uppercase :radix 16)))
(lower-index (unless (string= "" simple-lowercase)
(parse-integer simple-lowercase :radix 16)))
(title-index (unless (string= "" simple-titlecase)
(parse-integer simple-titlecase :radix 16)))
(cl-both-case-p
(not (null (or (and (= gc-index 0) lower-index)
(and (= gc-index 1) upper-index)))))
(misc-index (hash-misc gc-index bidi-index ccc-index
decimal-digit digit bidi-mirrored
cl-both-case-p)))
(declare (ignore digit-index))
(incf *name-size* (length name))
(when (string/= "" decomposition-type-and-mapping)
(let ((split (split-string decomposition-type-and-mapping
#\Space)))
(when (char= #\< (aref (first split) 0))
(setf (gethash (pop split) *decomposition-types*) t))
(unless (aref *decomposition-base* (cp-high code-point))
(setf (aref *decomposition-base* (cp-high code-point))
(make-array (ash 1 *page-size-exponent*)
:initial-element nil)))
(setf (aref (aref *decomposition-base* (cp-high code-point))
(cp-low code-point))
(mapcar #'(lambda (string)
(parse-integer string :radix 16))
split))
(setq *decomposition-length-max*
(max *decomposition-length-max* (length split)))
(incf *decompositions* (length split))))
(when (and (string/= "" simple-uppercase)
(string/= "" simple-lowercase))
(push (list code-point upper-index lower-index) *both-cases*))
(when (string/= simple-uppercase simple-titlecase)
(push (cons code-point title-index) *different-titlecases*))
(when (string/= digit numeric)
(push (cons code-point numeric) *different-numerics*))
(cond
((= gc-index 8)
(unless *last-uppercase*
(incf *uppercase-transition-count*))
(setq *last-uppercase* t))
(t
(when *last-uppercase*
(incf *uppercase-transition-count*))
(setq *last-uppercase* nil)))
(when (> ccc-index 255)
(error "canonical combining class too large ~A" ccc-index))
(let ((result (vector misc-index (or upper-index lower-index 0))))
(when (and (> (length name) 7)
(string= ", Last>" name :start2 (- (length name) 7)))
(let ((page-start (ash (+ *block-first*
(ash 1 *page-size-exponent*)
-1)
(- *page-size-exponent*)))
(page-end (ash code-point (- *page-size-exponent*))))
(loop for point from *block-first*
below (ash page-start *page-size-exponent*)
do (setf (aref (aref *ucd-base* (cp-high point))
(cp-low point))
result))
(loop for page from page-start below page-end
do (setf (aref *ucd-base* page)
(make-array (ash 1 *page-size-exponent*)
:initial-element result)))
(loop for point from (ash page-end *page-size-exponent*)
below code-point
do (setf (aref (aref *ucd-base* (cp-high point))
(cp-low point))
result))))
result)))))
(defun slurp-ucd-line (line)
(let* ((split-line (split-string line #\;))
(code-point (parse-integer (first split-line) :radix 16))
(code-high (ash code-point (- *page-size-exponent*)))
(code-low (ldb (byte *page-size-exponent* 0) code-point)))
(unless (aref *ucd-base* code-high)
(setf (aref *ucd-base* code-high)
(make-array (ash 1 *page-size-exponent*)
:initial-element nil)))
(setf (aref (aref *ucd-base* code-high) code-low)
(encode-ucd-line (cdr split-line) code-point))))
(defun second-pass ()
(loop for i from 0 below (length *ucd-base*)
when (aref *ucd-base* i)
do (loop for j from 0 below (length (aref *ucd-base* i))
for result = (aref (aref *ucd-base* i) j)
when result
when (let* ((transform-point (aref result 1))
(transform-high (ash transform-point
(- *page-size-exponent*)))
(transform-low (ldb (byte *page-size-exponent* 0)
transform-point)))
(and (plusp transform-point)
(/= (aref (aref (aref *ucd-base* transform-high)
transform-low)
1)
(+ (ash i *page-size-exponent*) j))))
do (destructuring-bind (gc-index bidi-index ccc-index
decimal-digit digit bidi-mirrored
cl-both-case-p)
(aref *misc-table* (aref result 0))
(declare (ignore cl-both-case-p))
(format t "~A~%" (+ (ash i *page-size-exponent*) j))
(setf (aref result 0)
(hash-misc gc-index bidi-index ccc-index
decimal-digit digit bidi-mirrored
nil))))))
(defun write-3-byte (triplet stream)
(write-byte (ldb (byte 8 0) triplet) stream)
(write-byte (ldb (byte 8 8) triplet) stream)
(write-byte (ldb (byte 8 16) triplet) stream))
(defun digit-to-byte (digit)
(if (string= "" digit)
255
(parse-integer digit)))
(defun output ()
(let ((hash (make-hash-table :test #'equalp))
(index 0))
(loop for page across *ucd-base*
do (when page
(unless (gethash page hash)
(setf (gethash page hash)
(incf index)))))
(let ((array (make-array (1+ index))))
(maphash #'(lambda (key value)
(setf (aref array value) key))
hash)
(setf (aref array 0)
(make-array (ash 1 *page-size-exponent*) :initial-element nil))
(with-open-file (stream (make-pathname :name "ucd"
:type "dat"
:defaults *output-directory*)
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede
:if-does-not-exist :create)
(loop for (gc-index bidi-index ccc-index decimal-digit digit
bidi-mirrored)
across *misc-table*
do (write-byte gc-index stream)
do (write-byte bidi-index stream)
do (write-byte ccc-index stream)
do (write-byte (digit-to-byte decimal-digit) stream)
do (write-byte (digit-to-byte digit) stream)
do (write-byte (if (string= "N" bidi-mirrored) 0 1) stream)
do (write-byte 0 stream)
do (write-byte 0 stream))
(loop for page across *ucd-base*
do (write-byte (if page (gethash page hash) 0) stream))
(loop for page across array
do (loop for entry across page
do (write-byte (if entry
(aref *misc-mapping* (aref entry 0))
255)
stream)
do (write-3-byte (if entry (aref entry 1) 0)
stream))))))
(with-open-file (*standard-output*
(make-pathname :name "numerics"
:type "lisp-expr"
:defaults *output-directory*)
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(let ((*print-pretty* t))
(prin1 (mapcar #'(lambda (x) (cons (car x) (read-from-string (cdr x))))
*different-numerics*))))
(with-open-file (*standard-output*
(make-pathname :name "titlecases"
:type "lisp-expr"
:defaults *output-directory*)
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(let ((*print-pretty* t))
(prin1 *different-titlecases*)))
(with-open-file (*standard-output*
(make-pathname :name "misc"
:type "lisp-expr"
:defaults *output-directory*)
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(let ((*print-pretty* t))
(prin1 `(:length ,(length *misc-table*)
:uppercase ,(loop for (gc-index) across *misc-table*
for i from 0
when (= gc-index 0)
collect i)
:lowercase ,(loop for (gc-index) across *misc-table*
for i from 0
when (= gc-index 1)
collect i)
:titlecase ,(loop for (gc-index) across *misc-table*
for i from 0
when (= gc-index 2)
collect i)))))
(values))
;;; Use of the generated files
(defparameter *compiled-ucd* nil)
(defun read-compiled-ucd ()
(with-open-file (stream (make-pathname :name "ucd"
:type "dat"
:defaults *output-directory*)
:direction :input
:element-type '(unsigned-byte 8))
(let ((length (file-length stream)))
(setq *compiled-ucd*
(make-array length :element-type '(unsigned-byte 8)))
(read-sequence *compiled-ucd* stream)))
(values))
;;; The stuff below is dependent on misc.lisp-expr being
;;; (:LENGTH 186 :UPPERCASE (0) :LOWERCASE (1) :TITLECASE (2))
(defparameter *length* 186)
(defun cp-index (cp)
(let* ((cp-high (cp-high cp))
(page (aref *compiled-ucd* (+ (* 8 *length*) cp-high))))
(+ (* 8 *length*)
(ash #x110000 (- *page-size-exponent*))
(* (ash 4 *page-size-exponent*) page)
(* 4 (cp-low cp)))))
(defun cp-value-0 (cp)
(aref *compiled-ucd* (cp-index cp)))
(defun cp-value-1 (cp)
(let ((index (cp-index cp)))
(dpb (aref *compiled-ucd* (+ index 3)) (byte 8 16)
(dpb (aref *compiled-ucd* (+ index 2)) (byte 8 8)
(aref *compiled-ucd* (1+ index))))))
(defun cp-general-category (cp)
(aref *compiled-ucd* (* 8 (cp-value-0 cp))))
(defun cp-decimal-digit (cp)
(let ((decimal-digit (aref *compiled-ucd* (+ 3 (* 8 (cp-value-0 cp))))))
(and (< decimal-digit 10)
decimal-digit)))
(defun cp-alpha-char-p (cp)
(< (cp-general-category cp) 5))
(defun cp-alphanumericp (cp)
(let ((gc (cp-general-category cp)))
(or (< gc 5)
(= gc 12))))
(defun cp-digit-char-p (cp &optional (radix 10))
(let ((number (or (cp-decimal-digit cp)
(and (<= 65 cp 90)
(- cp 55))
(and (<= 97 cp 122)
(- cp 87)))))
(when (and number (< number radix))
number)))
(defun cp-graphic-char-p (cp)
(or (<= 32 cp 127)
(<= 160 cp)))
(defun cp-char-upcase (cp)
(if (= (cp-value-0 cp) 1)
(cp-value-1 cp)
cp))
(defun cp-char-downcase (cp)
(if (= (cp-value-0 cp) 0)
(cp-value-1 cp)
cp))
(defun cp-upper-case-p (cp)
(= (cp-value-0 cp) 0))
(defun cp-lower-case-p (cp)
(= (cp-value-0 cp) 1))
(defun cp-both-case-p (cp)
(< (cp-value-0 cp) 2))