1 ! Copyright (C) 2008, 2009 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: unicode.data sequences namespaces
4 sbufs make unicode.normalize math hints
5 unicode.categories combinators assocs combinators.short-circuit
6 strings splitting kernel accessors unicode.breaks fry locals ;
10 SYMBOL: locale ! Just casing locale, or overall?
14 : split-subseq ( string sep -- strings )
15 [ dup ] swap '[ _ split1-slice swap ] produce nip ;
17 : replace ( old new str -- newstr )
18 [ split-subseq ] dip join ; inline
21 locale get { "tr" "az" } member? ;
26 : lithuanian? ( -- ? ) locale get "lt" = ;
28 : dot-over ( -- ch ) HEX: 307 ;
30 : lithuanian>upper ( string -- lower )
31 "i\u000307" "i" replace
32 "j\u000307" "j" replace ;
34 : mark-above? ( ch -- ? )
35 combining-class 230 = ;
37 :: with-rest ( seq quot: ( seq -- seq ) -- seq )
38 seq unclip quot dip prefix ; inline
40 : add-dots ( seq -- seq )
44 { [ mark-above? ] [ CHAR: combining-ogonek = ] } 1||
45 [ CHAR: combining-dot-above prefix ] when
47 ] if-empty ] with-rest ; inline
49 : lithuanian>lower ( string -- lower )
50 "I" split add-dots "I" join
51 "J" split add-dots "J" join ; inline
53 : turk>upper ( string -- upper-i )
54 "i" "I\u000307" replace ; inline
56 : turk>lower ( string -- lower-i )
57 "I\u000307" "i" replace
58 "I" "\u000131" replace ; inline
60 : fix-sigma-end ( string -- string )
62 dup peek CHAR: greek-small-letter-sigma =
63 [ 1 head* CHAR: greek-small-letter-final-sigma suffix ] when
66 : sigma-map ( string -- string )
67 { CHAR: greek-capital-letter-sigma } split [ [
68 [ { CHAR: greek-small-letter-sigma } ] [
70 CHAR: greek-small-letter-final-sigma
71 CHAR: greek-small-letter-sigma ? prefix
73 ] map ] with-rest concat fix-sigma-end ; inline
75 : final-sigma ( string -- string )
76 CHAR: greek-capital-letter-sigma
77 over member? [ sigma-map ] when
80 :: map-case ( string string-quot char-quot -- case )
81 string length <sbuf> :> out
84 [ string-quot call out push-all ]
85 [ char-quot call out push ] ?if
86 ] each out "" like ; inline
90 : >lower ( string -- lower )
91 i-dot? [ turk>lower ] when
92 lt? [ lithuanian>lower ] when
94 [ lower>> ] [ ch>lower ] map-case ;
96 HINTS: >lower string ;
98 : >upper ( string -- upper )
99 i-dot? [ turk>upper ] when
100 lt? [ lithuanian>upper ] when
101 [ upper>> ] [ ch>upper ] map-case ;
103 HINTS: >upper string ;
107 : (>title) ( string -- title )
108 i-dot? [ turk>upper ] when
109 lt? [ lithuanian>upper ] when
110 [ title>> ] [ ch>title ] map-case ; inline
112 : title-word ( string -- title )
113 unclip 1string [ >lower ] [ (>title) ] bi* prepend ; inline
117 : >title ( string -- title )
118 final-sigma >words [ title-word ] map concat ;
120 HINTS: >title string ;
122 : >case-fold ( string -- fold )
125 : lower? ( string -- ? ) dup >lower = ;
127 : upper? ( string -- ? ) dup >upper = ;
129 : title? ( string -- ? ) dup >title = ;
131 : case-fold? ( string -- ? ) dup >case-fold = ;