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 ;
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{ }
23 : >2ch ( a b -- c ) [ 21 shift ] dip + ;
24 : 2ch> ( c -- a b ) [ -21 shift ] [ 21 on-bits mask ] bi ;
28 CONSTANT: name-map H{ }
30 : canonical-entry ( char -- seq ) canonical-map at ; inline
31 : compatibility-entry ( char -- seq ) compatibility-map at ; inline
32 : combine-chars ( a b -- char/f ) >2ch combine-map at ; inline
33 : combining-class ( char -- n ) class-map at ; inline
34 : non-starter? ( char -- ? ) combining-class { 0 f } member? not ; inline
35 : property ( property -- interval-map ) properties at ; foldable
36 : property? ( char property -- ? ) property interval-sets:in? ; inline
37 : special-case ( ch -- casing-tuple ) special-casing at ; inline
39 ! For non-existent characters, use Cn
40 CONSTANT: categories {
42 "Lu" "Ll" "Lt" "Lm" "Lo"
45 "Pc" "Pd" "Ps" "Pe" "Pi" "Pf" "Po"
53 MEMO: categories-map ( -- hashtable )
54 categories <enum> [ swap ] H{ } assoc-map-as ;
56 CONSTANT: num-chars 0x2FA1E
60 : category# ( char -- n )
61 ! There are a few characters that should be Cn
62 ! that this gives Cf or Mn
63 ! Cf = 26; Mn = 5; Cn = 29
64 ! Use a compressed array instead?
65 dup category-map ?nth [ ] [
66 dup 0xE0001 0xE007F between?
68 0xE0100 0xE01EF between? 5 29 ?
72 : category ( char -- category )
73 category# categories nth ;
77 ! Loading data from UnicodeData.txt
79 : load-data ( -- data )
80 "vocab:unicode/data/UnicodeData.txt" data ;
82 : (process-data) ( index data -- newdata )
83 [ [ nth ] keep first swap ] with { } map>assoc
84 [ [ hex> ] dip ] assoc-map ;
86 : process-data ( index data -- hash )
87 (process-data) [ hex> ] assoc-map [ nip ] H{ } assoc-filter-as ;
89 : (chain-decomposed) ( hash value -- newvalue )
92 [ (chain-decomposed) ] [ 1array nip ] ?if
95 : chain-decomposed ( hash -- newhash )
96 dup [ swap (chain-decomposed) ] curry assoc-map ;
99 second { [ empty? ] [ first ] } 1|| ;
101 : (process-decomposed) ( data -- alist )
102 5 swap (process-data)
103 [ " " split [ hex> ] map ] assoc-map ;
105 : exclusions-file ( -- filename )
106 "vocab:unicode/data/CompositionExclusions.txt" ;
108 : exclusions ( -- set )
109 exclusions-file utf8 file-lines
110 [ "#" split1 drop [ blank? ] trim-tail hex> ] map
113 : remove-exclusions ( alist -- alist )
114 exclusions unique assoc-diff ;
116 : process-canonical ( data -- hash hash )
117 (process-decomposed) [ first* ] filter
119 [ second length 2 = ] filter remove-exclusions
120 [ first2 >2ch swap ] H{ } assoc-map-as
121 ] [ >hashtable chain-decomposed ] bi ;
123 : process-compatibility ( data -- hash )
125 [ dup first* [ first2 rest 2array ] unless ] map
126 [ second empty? ] reject
127 >hashtable chain-decomposed ;
129 : process-combining ( data -- hash )
130 3 swap (process-data)
131 [ string>number ] assoc-map
132 [ nip zero? ] assoc-reject
135 ! the maximum unicode char in the first 3 planes
137 :: fill-ranges ( table -- table )
138 name-map sort-values keys
139 [ { [ "first>" tail? ] [ "last>" tail? ] } 1|| ] filter
141 [ name-map at ] bi@ [ [a,b] ] [ table ?nth ] bi
142 [ swap table ?set-nth ] curry each
145 :: process-category ( data -- category-listing )
146 num-chars <byte-array> :> table
147 2 data (process-data) [| char cat |
148 cat categories-map at char table ?set-nth
149 ] assoc-each table fill-ranges ;
151 : process-names ( data -- names-hash )
152 1 swap (process-data) [
153 >lower H{ { CHAR: \s CHAR: - } } substitute swap
154 ] H{ } assoc-map-as ;
156 : multihex ( hexstring -- string )
157 " " split [ hex> ] map sift ;
161 TUPLE: code-point lower title upper ;
163 C: <code-point> code-point
167 : set-code-point ( seq -- )
168 4 head [ multihex ] map first4
169 <code-point> swap first ,, ;
171 ! Extra properties {{[a,b],prop}}
172 : parse-properties ( -- assoc )
173 "vocab:unicode/data/PropList.txt" data [
175 ".." split1 [ dup ] unless*
180 : properties>intervals ( properties -- assoc[str,interval] )
181 dup values members [ f ] H{ } map>assoc
182 [ [ push-at ] curry assoc-each ] keep
183 [ <interval-set> ] assoc-map ;
185 : load-properties ( -- assoc )
186 parse-properties properties>intervals ;
188 ! Special casing data
189 : load-special-casing ( -- special-casing )
190 "vocab:unicode/data/SpecialCasing.txt" data
191 [ length 5 = ] filter
192 [ [ set-code-point ] each ] H{ } make ;
195 [ process-names name-map swap assoc-union! drop ]
196 [ 13 swap process-data simple-lower swap assoc-union! drop ]
197 [ 12 swap process-data simple-upper swap assoc-union! drop ]
198 [ 14 swap process-data simple-upper assoc-union simple-title swap assoc-union! drop ]
199 [ process-combining class-map swap assoc-union! drop ]
200 [ process-canonical canonical-map swap assoc-union! drop combine-map swap assoc-union! drop ]
201 [ process-compatibility compatibility-map swap assoc-union! drop ]
202 [ process-category category-map push-all ]
205 combine-map keys [ 2ch> nip ] map
206 [ class-map at ] reject
207 [ 0 swap class-map set-at ] each
209 load-special-casing special-casing swap assoc-union! drop
211 load-properties properties swap assoc-union! drop
216 name-map at [ "Invalid character" throw ] unless*
217 ] name>char-hook set-global