]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/data/data.factor
ebc3f6d9ef03b4898949f1f4ea61904f92d16033
[factor.git] / basis / unicode / data / data.factor
1 ! Copyright (C) 2008, 2009 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays ascii assocs byte-arrays combinators
4 combinators.short-circuit grouping hashtables interval-sets
5 io.encodings.utf8 io.files kernel locals make math math.bitwise
6 math.order math.parser math.ranges memoize namespaces sequences
7 sets simple-flat-file sorting splitting strings.parser ;
8 IN: unicode.data
9
10 <PRIVATE
11
12 CONSTANT: simple-lower H{ }
13 CONSTANT: simple-upper H{ }
14 CONSTANT: simple-title H{ }
15 CONSTANT: canonical-map H{ }
16 CONSTANT: combine-map H{ }
17 CONSTANT: class-map H{ }
18 CONSTANT: compatibility-map H{ }
19 CONSTANT: category-map BV{ }
20 CONSTANT: special-casing H{ }
21 CONSTANT: properties H{ }
22
23 : >2ch ( a b -- c ) [ 21 shift ] dip + ;
24 : 2ch> ( c -- a b ) [ -21 shift ] [ 21 on-bits mask ] bi ;
25
26 PRIVATE>
27
28 CONSTANT: name-map H{ }
29
30 : canonical-entry ( char -- seq ) canonical-map at ; inline
31 : combine-chars ( a b -- char/f ) >2ch combine-map at ; inline
32 : compatibility-entry ( char -- seq ) compatibility-map at ; inline
33 : combining-class ( char -- n ) class-map at ; inline
34 : non-starter? ( char -- ? ) combining-class { 0 f } member? not ; inline
35 : name>char ( name -- char ) name-map at ; inline
36 : char>name ( char -- name ) name-map value-at ; inline
37 : property ( property -- interval-map ) properties at ; foldable
38 : property? ( char property -- ? ) property interval-sets:in? ; inline
39 : ch>lower ( ch -- lower ) simple-lower ?at drop ; inline
40 : ch>upper ( ch -- upper ) simple-upper ?at drop ; inline
41 : ch>title ( ch -- title ) simple-title ?at drop ; inline
42 : special-case ( ch -- casing-tuple ) special-casing at ; inline
43
44 ! For non-existent characters, use Cn
45 CONSTANT: categories {
46     "Cn"
47     "Lu" "Ll" "Lt" "Lm" "Lo"
48     "Mn" "Mc" "Me"
49     "Nd" "Nl" "No"
50     "Pc" "Pd" "Ps" "Pe" "Pi" "Pf" "Po"
51     "Sm" "Sc" "Sk" "So"
52     "Zs" "Zl" "Zp"
53     "Cc" "Cf" "Cs" "Co"
54 }
55
56 <PRIVATE
57
58 MEMO: categories-map ( -- hashtable )
59     categories <enum> [ swap ] H{ } assoc-map-as ;
60
61 CONSTANT: num-chars 0x2FA1E
62
63 PRIVATE>
64
65 : category# ( char -- n )
66     ! There are a few characters that should be Cn
67     ! that this gives Cf or Mn
68     ! Cf = 26; Mn = 5; Cn = 29
69     ! Use a compressed array instead?
70     dup category-map ?nth [ ] [
71         dup 0xE0001 0xE007F between?
72         [ drop 26 ] [
73             0xE0100 0xE01EF between?  5 29 ?
74         ] if
75     ] ?if ; inline
76
77 : category ( char -- category )
78     category# categories nth ;
79
80 <PRIVATE
81
82 ! Loading data from UnicodeData.txt
83
84 : load-data ( -- data )
85     "vocab:unicode/data/UnicodeData.txt" data ;
86
87 : (process-data) ( index data -- newdata )
88     [ [ nth ] keep first swap ] with { } map>assoc
89     [ [ hex> ] dip ] assoc-map ;
90
91 : process-data ( index data -- hash )
92     (process-data) [ hex> ] assoc-map [ nip ] H{ } assoc-filter-as ;
93
94 : (chain-decomposed) ( hash value -- newvalue )
95     [
96         2dup of
97         [ (chain-decomposed) ] [ 1array nip ] ?if
98     ] with map concat ;
99
100 : chain-decomposed ( hash -- newhash )
101     dup [ swap (chain-decomposed) ] curry assoc-map ;
102
103 : first* ( seq -- ? )
104     second { [ empty? ] [ first ] } 1|| ;
105
106 : (process-decomposed) ( data -- alist )
107     5 swap (process-data)
108     [ " " split [ hex> ] map ] assoc-map ;
109
110 : exclusions-file ( -- filename )
111     "vocab:unicode/data/CompositionExclusions.txt" ;
112
113 : exclusions ( -- set )
114     exclusions-file utf8 file-lines
115     [ "#" split1 drop [ blank? ] trim-tail hex> ] map
116     [ 0 = ] reject ;
117
118 : remove-exclusions ( alist -- alist )
119     exclusions unique assoc-diff ;
120
121 : process-canonical ( data -- hash hash )
122     (process-decomposed) [ first* ] filter
123     [
124         [ second length 2 = ] filter remove-exclusions
125         [ first2 >2ch swap ] H{ } assoc-map-as
126     ] [ >hashtable chain-decomposed ] bi ;
127
128 : process-compatibility ( data -- hash )
129     (process-decomposed)
130     [ dup first* [ first2 rest 2array ] unless ] map
131     [ second empty? ] reject
132     >hashtable chain-decomposed ;
133
134 : process-combining ( data -- hash )
135     3 swap (process-data)
136     [ string>number ] assoc-map
137     [ nip zero? ] assoc-reject
138     >hashtable ;
139
140 ! the maximum unicode char in the first 3 planes
141
142 :: fill-ranges ( table -- table )
143     name-map sort-values keys
144     [ { [ "first>" tail? ] [ "last>" tail? ] } 1|| ] filter
145     2 group [
146         [ name>char ] bi@ [ [a,b] ] [ table ?nth ] bi
147         [ swap table ?set-nth ] curry each
148     ] assoc-each table ;
149
150 :: process-category ( data -- category-listing )
151     num-chars <byte-array> :> table
152     2 data (process-data) [| char cat |
153         cat categories-map at char table ?set-nth
154     ] assoc-each table fill-ranges ;
155
156 : process-names ( data -- names-hash )
157     1 swap (process-data) [
158         >lower H{ { CHAR: \s CHAR: - } } substitute swap
159     ] H{ } assoc-map-as ;
160
161 : multihex ( hexstring -- string )
162     " " split [ hex> ] map sift ;
163
164 PRIVATE>
165
166 TUPLE: code-point lower title upper ;
167
168 C: <code-point> code-point
169
170 <PRIVATE
171
172 : set-code-point ( seq -- )
173     4 head [ multihex ] map first4
174     <code-point> swap first ,, ;
175
176 ! Extra properties {{[a,b],prop}}
177 : parse-properties ( -- assoc )
178     "vocab:unicode/data/PropList.txt" data [
179         [
180             ".." split1 [ dup ] unless*
181             [ hex> ] bi@ 2array
182         ] dip
183     ] assoc-map ;
184
185 : properties>intervals ( properties -- assoc[str,interval] )
186     dup values members [ f ] H{ } map>assoc
187     [ [ push-at ] curry assoc-each ] keep
188     [ <interval-set> ] assoc-map ;
189
190 : load-properties ( -- assoc )
191     parse-properties properties>intervals ;
192
193 ! Special casing data
194 : load-special-casing ( -- special-casing )
195     "vocab:unicode/data/SpecialCasing.txt" data
196     [ length 5 = ] filter
197     [ [ set-code-point ] each ] H{ } make ;
198
199 load-data {
200     [ process-names name-map swap assoc-union! drop ]
201     [ 13 swap process-data simple-lower swap assoc-union! drop ]
202     [ 12 swap process-data simple-upper swap assoc-union! drop ]
203     [ 14 swap process-data simple-upper assoc-union simple-title swap assoc-union! drop ]
204     [ process-combining class-map swap assoc-union! drop ]
205     [ process-canonical canonical-map swap assoc-union! drop combine-map swap assoc-union! drop ]
206     [ process-compatibility compatibility-map swap assoc-union! drop ]
207     [ process-category category-map push-all ]
208 } cleave
209
210 combine-map keys [ 2ch> nip ] map
211 [ combining-class ] reject
212 [ 0 swap class-map set-at ] each
213
214 load-special-casing special-casing swap assoc-union! drop
215
216 load-properties properties swap assoc-union! drop
217
218 [ name>char [ "Invalid character" throw ] unless* ]
219 name>char-hook set-global
220
221 PRIVATE>