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