1 ! Copyright (C) 2008 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
5 math.parser hash2 math.order byte-arrays words namespaces words
6 compiler.units parser io.encodings.ascii values interval-maps
7 ascii sets combinators locals math.ranges sorting ;
16 VALUE: compatibility-map
22 : canonical-entry ( char -- seq ) canonical-map at ;
23 : combine-chars ( a b -- char/f ) combine-map hash2 ;
24 : compatibility-entry ( char -- seq ) compatibility-map at ;
25 : combining-class ( char -- n ) class-map at ;
26 : non-starter? ( char -- ? ) class-map key? ;
27 : name>char ( string -- char ) name-map at ;
28 : char>name ( char -- string ) name-map value-at ;
29 : property? ( char property -- ? ) properties at interval-key? ;
31 ! Convenience functions
32 : ?between? ( n/f from to -- ? )
33 pick [ between? ] [ 3drop f ] if ;
35 ! Loading data from UnicodeData.txt
37 : split-; ( line -- array )
38 ";" split [ [ blank? ] trim ] map ;
40 : data ( filename -- data )
41 ascii file-lines [ split-; ] map ;
43 : load-data ( -- data )
44 "resource:basis/unicode/data/UnicodeData.txt" data ;
46 : filter-comments ( lines -- lines )
47 [ "#@" split first ] map harvest ;
49 : (process-data) ( index data -- newdata )
51 [ [ nth ] keep first swap ] with { } map>assoc
52 [ [ hex> ] dip ] assoc-map ;
54 : process-data ( index data -- hash )
55 (process-data) [ hex> ] assoc-map [ nip ] assoc-filter >hashtable ;
57 : (chain-decomposed) ( hash value -- newvalue )
60 [ (chain-decomposed) ] [ 1array nip ] ?if
63 : chain-decomposed ( hash -- newhash )
64 dup [ swap (chain-decomposed) ] curry assoc-map ;
67 second { [ empty? ] [ first ] } 1|| ;
69 : (process-decomposed) ( data -- alist )
71 [ " " split [ hex> ] map ] assoc-map ;
73 : process-canonical ( data -- hash2 hash )
74 (process-decomposed) [ first* ] filter
76 [ second length 2 = ] filter
77 ! using 1009 as the size, the maximum load is 4
78 [ first2 first2 rot 3array ] map 1009 alist>hash2
79 ] [ >hashtable chain-decomposed ] bi ;
81 : process-compatibility ( data -- hash )
83 [ dup first* [ first2 rest 2array ] unless ] map
84 [ second empty? not ] filter
85 >hashtable chain-decomposed ;
87 : process-combining ( data -- hash )
89 [ string>number ] assoc-map
90 [ nip zero? not ] assoc-filter
93 : categories ( -- names )
94 ! For non-existent characters, use Cn
96 "Lu" "Ll" "Lt" "Lm" "Lo"
99 "Pc" "Pd" "Ps" "Pe" "Pi" "Pf" "Po"
102 "Cc" "Cf" "Cs" "Co" } ;
104 : num-chars HEX: 2FA1E ;
105 ! the maximum unicode char in the first 3 planes
107 : ?set-nth ( val index seq -- )
108 2dup bounds-check? [ set-nth ] [ 3drop ] if ;
110 :: fill-ranges ( table -- table )
111 name-map >alist sort-values keys
112 [ { [ "first>" tail? ] [ "last>" tail? ] } 1|| ] filter
114 [ name>char ] bi@ [ [a,b] ] [ table ?nth ] bi
115 [ swap table ?set-nth ] curry each
118 :: process-category ( data -- category-listing )
119 [let | table [ num-chars <byte-array> ] |
120 2 data (process-data) [| char cat |
121 cat categories index char table ?set-nth
122 ] assoc-each table fill-ranges ] ;
124 : ascii-lower ( string -- lower )
125 [ dup CHAR: A CHAR: Z between? [ HEX: 20 + ] when ] map ;
127 : process-names ( data -- names-hash )
128 1 swap (process-data) [
129 ascii-lower { { CHAR: \s CHAR: - } } substitute swap
130 ] H{ } assoc-map-as ;
132 : multihex ( hexstring -- string )
133 " " split [ hex> ] map sift ;
135 TUPLE: code-point lower title upper ;
137 C: <code-point> code-point
139 : set-code-point ( seq -- )
140 4 head [ multihex ] map first4
141 <code-point> swap first set ;
144 : properties-lines ( -- lines )
145 "resource:basis/unicode/data/PropList.txt"
148 : parse-properties ( -- {{[a,b],prop}} )
149 properties-lines filter-comments [
151 [ ".." split1 [ dup ] unless* [ hex> ] bi@ 2array ] dip
154 : properties>intervals ( properties -- assoc[str,interval] )
155 dup values prune [ f ] H{ } map>assoc
156 [ [ push-at ] curry assoc-each ] keep
157 [ <interval-set> ] assoc-map ;
159 : load-properties ( -- assoc )
160 parse-properties properties>intervals ;
162 ! Special casing data
163 : load-special-casing ( -- special-casing )
164 "resource:basis/unicode/data/SpecialCasing.txt" data
165 [ length 5 = ] filter
166 [ [ set-code-point ] each ] H{ } make-assoc ;
169 [ process-names to: name-map ]
170 [ 13 swap process-data to: simple-lower ]
171 [ 12 swap process-data to: simple-upper ]
172 [ 14 swap process-data simple-upper assoc-union to: simple-title ]
173 [ process-combining to: class-map ]
174 [ process-canonical to: canonical-map to: combine-map ]
175 [ process-compatibility to: compatibility-map ]
176 [ process-category to: category-map ]
179 load-special-casing to: special-casing
181 load-properties to: properties