1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii combinators
4 combinators.short-circuit hints kernel make math math.order
5 sbufs sequences sorting.insertion strings unicode.data vectors ;
9 ! Conjoining Jamo behavior
10 CONSTANT: hangul-base 0xAC00
11 CONSTANT: hangul-end 0xD7AF
12 CONSTANT: initial-base 0x1100
13 CONSTANT: medial-base 0x1161
14 CONSTANT: final-base 0x11A7
16 CONSTANT: initial-count 19
17 CONSTANT: medial-count 21
18 CONSTANT: final-count 28
20 : ?between? ( n/f from to -- ? )
21 pick [ between? ] [ 3drop f ] if ; inline
24 hangul-base hangul-end ?between? ; inline
28 [ 0x1100 0x11FF ?between? ] ! Hangul Jamo
29 [ 0xA960 0xA97F ?between? ] ! Hangul Jamo Extended-A
30 [ 0xD7B0 0xD7FF ?between? ] ! Hangul Jamo Extended-B
33 ! These numbers come from UAX 29
34 : initial? ( ch -- ? )
36 [ 0x1100 0x115F ?between? ]
37 [ 0xA960 0xA97C ?between? ]
42 [ 0x1160 0x11A7 ?between? ]
43 [ 0xD7B0 0xD7C6 ?between? ]
48 [ 0x11A8 0x11FF ?between? ]
49 [ 0xD7CB 0xD7FB ?between? ]
52 : hangul>jamo ( hangul -- jamo-string )
53 hangul-base - final-count /mod final-base +
55 medial-count /mod medial-base +
56 [ initial-base + ] dip
58 dup final-base = [ drop 2array ] [ 3array ] if ;
60 : jamo>hangul ( initial medial final -- hangul )
62 [ initial-base - medial-count * ] dip
63 medial-base - + final-count *
64 ] dip final-base - + hangul-base + ;
66 ! Normalization -- Decomposition
68 : reorder-slice ( string start -- slice done? )
69 2dup swap [ non-starter? not ] find-from drop
70 [ [ over length ] unless* rot <slice> ] keep not ; inline
72 : reorder-next ( string i -- new-i done? )
73 over [ non-starter? ] find-from drop [
75 [ dup [ combining-class ] insertion-sort to>> ] dip
76 ] [ length t ] if* ; inline
78 : reorder-loop ( string start -- )
79 dupd reorder-next [ 2drop ] [ reorder-loop ] if ; inline recursive
81 : reorder ( string -- )
84 : reorder-back ( string i -- )
85 over [ non-starter? not ] find-last-from drop ?1+ reorder-next 2drop ;
87 :: decompose ( string quot -- decomposed )
88 string length <sbuf> :> out
90 >fixnum dup ascii? [ out push ] [
91 dup hangul? [ hangul>jamo out push-all ]
92 [ dup quot call or* [ out push-all ] [ out push ] if ] if
95 out "" like dup reorder ; inline
97 : with-string ( str quot -- str )
98 over aux>> [ call ] [ drop ] if ; inline
100 : (nfd) ( string -- nfd )
101 [ canonical-entry ] decompose ;
103 HINTS: (nfd) string ;
105 : (nfkd) ( string -- nfkd )
106 [ compatibility-entry ] decompose ;
108 HINTS: (nfkd) string ;
110 ! Normalization -- Composition
112 : initial-medial? ( str i -- ? )
113 { [ swap nth initial? ] [ 1 + swap ?nth medial? ] } 2&& ;
115 : --final? ( str i -- ? )
116 2 + swap ?nth final? ;
118 : imf% ( str i -- str i )
119 [ tail-slice first3 jamo>hangul , ]
122 : im% ( str i -- str i )
123 [ tail-slice first2 final-base jamo>hangul , ]
126 : compose-jamo ( str i -- str i )
127 2dup initial-medial? [
128 2dup --final? [ imf% ] [ im% ] if
129 ] [ 2dup swap nth , 1 + ] if ;
131 : pass-combining ( str -- str i )
132 dup [ non-starter? not ] find drop
133 [ dup length ] unless*
136 TUPLE: compose-state i str char after last-class ;
138 : get-str ( state i -- ch )
139 swap [ i>> + ] [ str>> ] bi ?nth ; inline
140 : current ( state -- ch ) 0 get-str ; inline
141 : to ( state -- state ) [ 1 + ] change-i ; inline
142 : push-after ( ch state -- state ) [ ?push ] change-after ; inline
144 :: try-compose ( state new-char current-class -- state )
145 state last-class>> current-class =
146 [ new-char state push-after ] [
147 state char>> new-char combine-chars
148 [ state swap >>char ] [
149 new-char state push-after
150 current-class >>last-class
156 : try-noncombining ( state char -- state )
157 [ drop ] [ [ char>> ] dip combine-chars ] 2bi
158 [ >>char to f >>last-class compose-iter ] when* ; inline recursive
160 : compose-iter ( state -- state )
162 dup combining-class {
166 [ drop ] [ try-noncombining ] if ] }
167 [ try-compose to compose-iter ]
169 ] when* ; inline recursive
171 : compose-combining ( ch str i -- str i )
177 { [ char>> , ] [ after>> % ] [ str>> ] [ i>> ] } cleave ; inline
179 :: (compose) ( str i -- )
181 dup jamo? [ drop str i compose-jamo ] [
182 i 1 + str ?nth combining-class
183 [ str i 1 + compose-combining ] [ , str i 1 + ] if
185 ] when* ; inline recursive
187 : combine ( str -- comp )
188 [ pass-combining (compose) ] "" make ;
190 HINTS: combine string ;