Debian Codegraph @

scheme/unicode/extract-char-cases.ss

raw
1 ;;; Copyright (C) 2008 Abdulaziz Ghuloum, R. Kent Dybvig
2 ;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum
3 ;;;
4 ;;; Permission is hereby granted, free of charge, to any person obtaining a
5 ;;; copy of this software and associated documentation files (the "Software"),
6 ;;; to deal in the Software without restriction, including without limitation
7 ;;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 ;;; and/or sell copies of the Software, and to permit persons to whom the
9 ;;; Software is furnished to do so, subject to the following conditions:
10 ;;;
11 ;;; The above copyright notice and this permission notice shall be included in
12 ;;; all copies or substantial portions of the Software.
13 ;;;
14 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 ;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 ;;; DEALINGS IN THE SOFTWARE.
21
22 (import
23 (ikarus)
24 (unicode-data))
25
26 ; dropping support for s16 inner vectors for now
27 (include "extract-common.ss")
28 (define (iota n)
29 (let f ([n n] [ac '()])
30 (cond
31 [(= n 0) ac]
32 [else
33 (let ([n (- n 1)])
34 (f n (cons n ac)))])))
35 (define ptr-bytes 4)
36
37 (define code-point-limit #x110000) ; as of Unicode 5.1
38 #;(define table-limit #x30000)
39 (define table-limit code-point-limit)
40 (define-table (make-table table-ref table-set! table-ref-code)
41 (make-vector vector-ref vector-set!)
42 table-limit #x40 #x40)
43
44 (define-record-type chardata
45 (fields (immutable ucchar)
46 (immutable lcchar)
47 (immutable tcchar)
48 (mutable fcchar)
49 (mutable ucstr)
50 (mutable lcstr)
51 (mutable tcstr)
52 (mutable fcstr)
53 (immutable decomp-canon)
54 (immutable decomp-compat))
55 (protocol
56 (lambda (new)
57 (lambda (ucchar lcchar tcchar decomp-canon decomp-compat)
58 (new ucchar lcchar tcchar 0 ucchar lcchar tcchar 0
59 decomp-canon decomp-compat)))))
60
61 (define (find-cdrec idx ls)
62 (cond
63 [(assq idx ls) => cdr]
64 [else (error 'find-cdrec "~s is missing" idx)]))
65
66 (define data-case
67 (lambda (fields)
68 (let ([n (hex->num (car fields))]
69 [uc (list-ref fields 12)]
70 [lc (list-ref fields 13)]
71 [tc (list-ref fields 14)])
72 (define (f x) (if (string=? x "") 0 (- (hex->num x) n)))
73 (cons n (make-chardata (f uc) (f lc) (f tc)
74 (parse-decomp n (list-ref fields 5) #f)
75 (parse-decomp n (list-ref fields 5) #t))))))
76
77 (define (split str)
78 (remove ""
79 (let f ([i 0] [n (string-length str)])
80 (cond
81 [(= i n) (list (substring str 0 n))]
82 [(char=? (string-ref str i) #\space)
83 (cons (substring str 0 i)
84 (split (substring str (+ i 1) n)))]
85 [else (f (add1 i) n)]))))
86
87 (define (improperize ls)
88 (cond
89 [(null? (cdr ls)) (car ls)]
90 [else (cons (car ls) (improperize (cdr ls)))]))
91
92 (define (c*->off c* n)
93 (if (= (length c*) 1)
94 (- (car c*) n)
95 (improperize (map integer->char c*))))
96
97 (define (codes->off str n)
98 (c*->off (map hex->num (split str)) n))
99
100 ;;; decomposition field looks like:
101 ;;; hex-value*
102 ;;; <tag> hex-value*
103 ;;; latter appear to be for compatibility decomposition only
104 (define (parse-decomp n str compat?)
105 (let f ([ls (split str)])
106 (cond
107 [(null? ls) 0]
108 [(char=? (string-ref (car ls) 0) #\<)
109 (if compat? (c*->off (map hex->num (cdr ls)) n) 0)]
110 [else (c*->off (map hex->num ls) n)])))
111
112 (define (insert-foldcase-data! ls data)
113 (for-each
114 (lambda (fields)
115 (let ([n (hex->num (car fields))])
116 (let ([cdrec (find-cdrec n ls)]
117 [offset (codes->off (caddr fields) n)])
118 (chardata-fcchar-set! cdrec offset)
119 (chardata-fcstr-set! cdrec offset))))
120 (filter (lambda (fields) (equal? (cadr fields) "C")) data))
121 (for-each
122 (lambda (fields)
123 (let ([n (hex->num (car fields))])
124 (chardata-fcstr-set!
125 (find-cdrec n ls)
126 (codes->off (caddr fields) n))))
127 (filter (lambda (fields) (equal? (cadr fields) "F")) data)))
128
129 (define (insert-specialcase-data! ls data)
130 (for-each
131 (lambda (fields)
132 (let ([n (hex->num (car fields))])
133 (let ([cdrec (find-cdrec n ls)])
134 (chardata-lcstr-set! cdrec (codes->off (list-ref fields 1) n))
135 (chardata-tcstr-set! cdrec (codes->off (list-ref fields 2) n))
136 (chardata-ucstr-set! cdrec (codes->off (list-ref fields 3) n)))))
137 (filter
138 (lambda (fields) (= 0 (string-length (list-ref fields 4))))
139 data)))
140
141 (define verify-identity!
142 (lambda (n cdrec)
143 (define (zeros? . args) (andmap (lambda (x) (eqv? x 0)) args))
144 (unless (zeros? (chardata-ucchar cdrec)
145 (chardata-lcchar cdrec)
146 (chardata-tcchar cdrec)
147 (chardata-fcchar cdrec)
148 (chardata-ucstr cdrec)
149 (chardata-lcstr cdrec)
150 (chardata-tcstr cdrec)
151 (chardata-fcstr cdrec)
152 (chardata-decomp-canon cdrec)
153 (chardata-decomp-compat cdrec))
154 (error 'verify-identity "failed for ~x, ~s" n cdrec))))
155
156 (define build-table
157 (lambda (acc ls)
158 (let ([table (make-table 0)])
159 (for-each
160 (lambda (x)
161 (let ([n (car x)] [cdrec (cdr x)])
162 (unless (< n code-point-limit)
163 (error 'build-table
164 "code point value ~s is at or above declared limit ~s"
165 n code-point-limit))
166 (if (>= n table-limit)
167 (verify-identity! n cdrec)
168 (table-set! table n (acc cdrec)))))
169 ls)
170 (commonize* table)
171 table)))
172
173 (define (get-composition-pairs decomp-canon-table)
174 (define ($str-decomp-canon c)
175 (define (strop tbl c)
176 (let ([n (char->integer c)])
177 (if (and (fx< table-limit code-point-limit)
178 (fx>= n table-limit))
179 c
180 (let ([x (table-ref tbl n)])
181 (if (fixnum? x)
182 (integer->char (fx+ x n))
183 x)))))
184 (strop decomp-canon-table c))
185 (let ([exclusions
186 (map hex->num
187 (map car (get-unicode-data
188 "UNIDATA/CompositionExclusions.txt")))]
189 [pairs '()])
190 (for-each
191 (lambda (i)
192 (unless (and (fx<= #xD800 i) (fx<= i #xDFFF))
193 (unless (memv i exclusions)
194 (let* ([c (integer->char i)] [c* ($str-decomp-canon c)])
195 (when (pair? c*)
196 (set! pairs (cons (cons c* c) pairs)))))))
197 (iota #x110000))
198 (cons (list->vector (map car pairs))
199 (list->vector (map cdr pairs)))))
200
201
202
203
204 (let ([ls (map data-case (get-unicode-data "UNIDATA/UnicodeData.txt"))])
205 (insert-foldcase-data! ls (get-unicode-data "UNIDATA/CaseFolding.txt"))
206 (insert-specialcase-data! ls (get-unicode-data "UNIDATA/SpecialCasing.txt"))
207 ; insert final sigma flag for char-downcase conversion
208 (chardata-lcstr-set! (find-cdrec #x3a3 ls) 'sigma)
209 (with-output-to-file* "unicode-char-cases.ss"
210 (lambda ()
211 (parameterize ([print-graph #t] [print-unicode #f])
212 (pretty-print
213 `(module ($char-upcase $char-downcase $char-titlecase $char-foldcase
214 $str-upcase $str-downcase $str-titlecase $str-foldcase
215 $str-decomp-canon $str-decomp-compat
216 $composition-pairs)
217 (define char-upcase-table ',(build-table chardata-ucchar ls))
218 (define char-downcase-table ',(build-table chardata-lcchar ls))
219 (define char-titlecase-table ',(build-table chardata-tcchar ls))
220 (define char-foldcase-table ',(build-table chardata-fcchar ls))
221 (define string-upcase-table ',(build-table chardata-ucstr ls))
222 (define string-downcase-table ',(build-table chardata-lcstr ls))
223 (define string-titlecase-table ',(build-table chardata-tcstr ls))
224 (define string-foldcase-table ',(build-table chardata-fcstr ls))
225 (define decomp-canon-table ',(build-table chardata-decomp-canon ls))
226 (define decomp-compat-table ',(build-table chardata-decomp-compat ls))
227 (define table-limit ,table-limit)
228 (define code-point-limit ,code-point-limit)
229 (define table-ref ,table-ref-code)
230 (define (charop tbl c)
231 (let ([n (char->integer c)])
232 (if (and (fx< table-limit code-point-limit)
233 (fx>= n table-limit))
234 c
235 (integer->char (fx+ (table-ref tbl n) n)))))
236 (define (strop tbl c)
237 (let ([n (char->integer c)])
238 (if (and (fx< table-limit code-point-limit)
239 (fx>= n table-limit))
240 c
241 (let ([x (table-ref tbl n)])
242 (if (fixnum? x)
243 (integer->char (fx+ x n))
244 x)))))
245 (define ($char-upcase c) (charop char-upcase-table c))
246 (define ($char-downcase c) (charop char-downcase-table c))
247 (define ($char-titlecase c) (charop char-titlecase-table c))
248 (define ($char-foldcase c) (charop char-foldcase-table c))
249 (define ($str-upcase c) (strop string-upcase-table c))
250 (define ($str-downcase c) (strop string-downcase-table c))
251 (define ($str-titlecase c) (strop string-titlecase-table c))
252 (define ($str-foldcase c) (strop string-foldcase-table c))
253 (define ($str-decomp-canon c) (strop decomp-canon-table c))
254 (define ($str-decomp-compat c) (strop decomp-compat-table c))
255 (define ($composition-pairs)
256 ',(get-composition-pairs
257 (build-table chardata-decomp-canon ls)))))))))
258
259 (printf "Happy Happy Joy Joy ~a\n" (sizeof cache))