]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/collation/collation.factor
simple-flat-file: rename ``data`` to ``load-data-file``.
[factor.git] / basis / unicode / collation / collation.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit kernel locals make math math.order
5 math.parser namespaces sequences simple-flat-file splitting
6 strings unicode.data ;
7 IN: unicode.collation
8
9 <PRIVATE
10
11 SYMBOL: ducet
12
13 TUPLE: weight primary secondary tertiary ignorable? ;
14
15 : parse-weight ( string -- weight )
16     "]" split but-last [
17         weight new swap rest unclip CHAR: * = swapd >>ignorable?
18         swap "." split first3 [ hex> ] tri@
19         [ >>primary ] [ >>secondary ] [ >>tertiary ] tri*
20     ] map ;
21
22 : parse-keys ( string -- chars )
23     " " split [ hex> ] "" map-as ;
24
25 : parse-ducet ( file -- ducet )
26     load-data-file
27     [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;
28
29 "vocab:unicode/collation/allkeys.txt" parse-ducet ducet set-global
30
31 ! Fix up table for long contractions
32 : help-one ( assoc key -- )
33     ! Need to be more general? Not for DUCET, apparently
34     2 head 2dup swap key? [ 2drop ] [
35         [ [ 1string of ] with { } map-as concat ]
36         [ swap set-at ] 2bi
37     ] if ;
38
39 : insert-helpers ( assoc -- )
40     dup keys [ length 3 >= ] filter
41     [ help-one ] with each ;
42
43 ducet get-global insert-helpers
44
45 : base ( char -- base )
46     {
47         { [ dup 0x3400 0x4DB5 between? ] [ drop 0xFB80 ] } ! Extension A
48         { [ dup 0x20000 0x2A6D6 between? ] [ drop 0xFB80 ] } ! Extension B
49         { [ dup 0x4E00 0x9FC3 between? ] [ drop 0xFB40 ] } ! CJK
50         [ drop 0xFBC0 ] ! Other
51     } cond ;
52
53 : AAAA ( char -- weight )
54     [ base ] [ -15 shift ] bi + 0x20 2 f weight boa ;
55
56 : BBBB ( char -- weight )
57     0x7FFF bitand 0x8000 bitor 0 0 f weight boa ;
58
59 : illegal? ( char -- ? )
60     { [ "Noncharacter_Code_Point" property? ] [ category "Cs" = ] } 1|| ;
61
62 : derive-weight ( char -- weights )
63     first dup illegal?
64     [ drop { } ]
65     [ [ AAAA ] [ BBBB ] bi 2array ] if ;
66
67 : building-last ( -- char )
68     building get empty? [ 0 ] [ building get last last ] if ;
69
70 : blocked? ( char -- ? )
71     combining-class dup { 0 f } member?
72     [ drop building-last non-starter? ]
73     [ building-last combining-class = ] if ;
74
75 : possible-bases ( -- slice-of-building )
76     building get dup [ first non-starter? not ] find-last
77     drop [ 0 ] unless* tail-slice ;
78
79 :: ?combine ( char slice i -- ? )
80     i slice nth char suffix :> str
81     str ducet get-global key? dup
82     [ str i slice set-nth ] when ;
83
84 : add ( char -- )
85     dup blocked? [ 1string , ] [
86         dup possible-bases dup length iota
87         [ ?combine ] 2with any?
88         [ drop ] [ 1string , ] if
89     ] if ;
90
91 : string>graphemes ( string -- graphemes )
92     [ [ add ] each ] { } make ;
93
94 : graphemes>weights ( graphemes -- weights )
95     [
96         dup weight? [ 1array ] ! From tailoring
97         [ dup ducet get-global at [ ] [ derive-weight ] ?if ] if
98     ] { } map-as concat ;
99
100 : append-weights ( weights quot -- )
101     [ [ ignorable?>> ] reject ] dip
102     map [ zero? ] reject % 0 , ; inline
103
104 : variable-weight ( weight -- )
105     dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if , ;
106
107 : weights>bytes ( weights -- byte-array )
108     [
109         {
110             [ [ primary>> ] append-weights ]
111             [ [ secondary>> ] append-weights ]
112             [ [ tertiary>> ] append-weights ]
113             [ [ variable-weight ] each ]
114         } cleave
115     ] { } make ;
116
117 PRIVATE>
118
119 : completely-ignorable? ( weight -- ? )
120     [ primary>> ] [ secondary>> ] [ tertiary>> ] tri
121     [ zero? ] tri@ and and ;
122
123 : filter-ignorable ( weights -- weights' )
124     f swap [
125         [ nip ] [ primary>> zero? and ] 2bi
126         [ swap ignorable?>> or ]
127         [ swap completely-ignorable? or not ] 2bi
128     ] filter nip ;