]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/data/data.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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: combinators.short-circuit assocs math kernel sequences
4 io.files hashtables quotations splitting grouping arrays io
5 math.parser hash2 math.order byte-arrays namespaces
6 compiler.units parser io.encodings.ascii values interval-maps
7 ascii sets combinators locals math.ranges sorting make
8 strings.parser io.encodings.utf8 memoize simple-flat-file ;
9 IN: unicode.data
10
11 <PRIVATE
12
13 VALUE: simple-lower
14 VALUE: simple-upper
15 VALUE: simple-title
16 VALUE: canonical-map
17 VALUE: combine-map
18 VALUE: class-map
19 VALUE: compatibility-map
20 VALUE: category-map
21 VALUE: special-casing
22 VALUE: properties
23
24 PRIVATE>
25
26 VALUE: name-map
27
28 : canonical-entry ( char -- seq ) canonical-map at ; inline
29 : combine-chars ( a b -- char/f ) combine-map hash2 ; inline
30 : compatibility-entry ( char -- seq ) compatibility-map at ; inline
31 : combining-class ( char -- n ) class-map at ; inline
32 : non-starter? ( char -- ? ) combining-class { 0 f } member? not ; inline
33 : name>char ( name -- char ) name-map at ; inline
34 : char>name ( char -- name ) name-map value-at ; inline
35 : property? ( char property -- ? ) properties at interval-key? ; inline
36 : ch>lower ( ch -- lower ) simple-lower ?at drop ; inline
37 : ch>upper ( ch -- upper ) simple-upper ?at drop ; inline
38 : ch>title ( ch -- title ) simple-title ?at drop ; inline
39 : special-case ( ch -- casing-tuple ) special-casing at ; inline
40
41 ! For non-existent characters, use Cn
42 CONSTANT: categories
43     { "Cn"
44       "Lu" "Ll" "Lt" "Lm" "Lo"
45       "Mn" "Mc" "Me"
46       "Nd" "Nl" "No"
47       "Pc" "Pd" "Ps" "Pe" "Pi" "Pf" "Po"
48       "Sm" "Sc" "Sk" "So"
49       "Zs" "Zl" "Zp"
50       "Cc" "Cf" "Cs" "Co" }
51
52 <PRIVATE
53
54 MEMO: categories-map ( -- hashtable )
55     categories <enum> [ swap ] H{ } assoc-map-as ;
56
57 CONSTANT: num-chars HEX: 2FA1E
58
59 PRIVATE>
60
61 : category# ( char -- n )
62     ! There are a few characters that should be Cn
63     ! that this gives Cf or Mn
64     ! Cf = 26; Mn = 5; Cn = 29
65     ! Use a compressed array instead?
66     dup category-map ?nth [ ] [
67         dup HEX: E0001 HEX: E007F between?
68         [ drop 26 ] [
69             HEX: E0100 HEX: E01EF between?  5 29 ?
70         ] if
71     ] ?if ;
72
73 : category ( char -- category )
74     category# categories nth ;
75
76 <PRIVATE
77
78 ! Loading data from UnicodeData.txt
79
80 : load-data ( -- data )
81     "vocab:unicode/data/UnicodeData.txt" data ;
82
83 : (process-data) ( index data -- newdata )
84     [ [ nth ] keep first swap ] with { } map>assoc
85     [ [ hex> ] dip ] assoc-map ;
86
87 : process-data ( index data -- hash )
88     (process-data) [ hex> ] assoc-map [ nip ] assoc-filter >hashtable ;
89
90 : (chain-decomposed) ( hash value -- newvalue )
91     [
92         2dup swap at
93         [ (chain-decomposed) ] [ 1array nip ] ?if
94     ] with map concat ;
95
96 : chain-decomposed ( hash -- newhash )
97     dup [ swap (chain-decomposed) ] curry assoc-map ;
98
99 : first* ( seq -- ? )
100     second { [ empty? ] [ first ] } 1|| ;
101
102 : (process-decomposed) ( data -- alist )
103     5 swap (process-data)
104     [ " " split [ hex> ] map ] assoc-map ;
105
106 : exclusions-file ( -- filename )
107     "vocab:unicode/data/CompositionExclusions.txt" ;
108
109 : exclusions ( -- set )
110     exclusions-file utf8 file-lines
111     [ "#" split1 drop [ blank? ] trim-tail hex> ] map
112     [ 0 = not ] filter ;
113
114 : remove-exclusions ( alist -- alist )
115     exclusions [ dup ] H{ } map>assoc assoc-diff ;
116
117 : process-canonical ( data -- hash2 hash )
118     (process-decomposed) [ first* ] filter
119     [
120         [ second length 2 = ] filter remove-exclusions
121         ! using 1009 as the size, the maximum load is 4
122         [ first2 first2 rot 3array ] map 1009 alist>hash2
123     ] [ >hashtable chain-decomposed ] bi ;
124
125 : process-compatibility ( data -- hash )
126     (process-decomposed)
127     [ dup first* [ first2 rest 2array ] unless ] map
128     [ second empty? not ] filter
129     >hashtable chain-decomposed ;
130
131 : process-combining ( data -- hash )
132     3 swap (process-data)
133     [ string>number ] assoc-map
134     [ nip zero? not ] assoc-filter
135     >hashtable ;
136
137 ! the maximum unicode char in the first 3 planes
138
139 : ?set-nth ( val index seq -- )
140     2dup bounds-check? [ set-nth ] [ 3drop ] if ;
141
142 :: fill-ranges ( table -- table )
143     name-map >alist 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 { { 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 set ;
175
176 ! Extra properties
177 : parse-properties ( -- {{[a,b],prop}} )
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 prune [ 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-assoc ;
198
199 load-data {
200     [ process-names to: name-map ]
201     [ 13 swap process-data to: simple-lower ]
202     [ 12 swap process-data to: simple-upper ]
203     [ 14 swap process-data simple-upper assoc-union to: simple-title ]
204     [ process-combining to: class-map ]
205     [ process-canonical to: canonical-map to: combine-map ]
206     [ process-compatibility to: compatibility-map ]
207     [ process-category to: category-map ]
208 } cleave
209
210 : postprocess-class ( -- )
211     combine-map [ [ second ] map ] map concat
212     [ combining-class not ] filter
213     [ 0 swap class-map set-at ] each ;
214
215 postprocess-class
216
217 load-special-casing to: special-casing
218
219 load-properties to: properties
220
221 [ name>char [ "Invalid character" throw ] unless* ]
222 name>char-hook set-global