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
20 : i-dot? ( locale -- ? )
21 { "tr" "az" } member? ; inline
23 : lithuanian? ( locale -- ? ) "lt" = ; inline
25 : lithuanian>upper ( string -- lower )
26 "i\u000307" "i" replace
27 "j\u000307" "j" replace ;
29 : mark-above? ( ch -- ? )
30 combining-class 230 = ;
32 :: with-rest ( seq quot: ( seq -- seq ) -- seq )
33 seq unclip quot dip prefix ; inline
35 : add-dots ( seq -- seq )
39 { [ mark-above? ] [ CHAR: combining-ogonek = ] } 1||
40 [ CHAR: combining-dot-above prefix ] when
42 ] if-empty ] with-rest ; inline
44 : lithuanian>lower ( string -- lower )
45 "I" split add-dots "I" join
46 "J" split add-dots "J" join ; inline
48 : turk>upper ( string -- upper-i )
49 "i" "I\u000307" replace ; inline
51 : turk>lower ( string -- lower-i )
52 "I\u000307" "i" replace
53 "I" "\u000131" replace ; inline
55 : fix-sigma-end ( string -- string )
57 dup last CHAR: greek-small-letter-sigma =
58 [ 1 head* CHAR: greek-small-letter-final-sigma suffix ] when
61 : sigma-map ( string -- string )
62 { CHAR: greek-capital-letter-sigma } split [ [
63 [ { CHAR: greek-small-letter-sigma } ] [
65 CHAR: greek-small-letter-final-sigma
66 CHAR: greek-small-letter-sigma ? prefix
68 ] map ] with-rest concat fix-sigma-end ; inline
70 : final-sigma ( string -- string )
71 CHAR: greek-capital-letter-sigma
72 over member? [ sigma-map ] when
75 :: map-case ( string string-quot char-quot -- case )
76 string length <sbuf> :> out
79 [ string-quot call out push-all ]
80 [ char-quot call out push ] ?if
81 ] each out "" like ; inline
83 : locale>lower ( string -- string' )
85 [ i-dot? [ turk>lower ] when ]
86 [ lithuanian? [ lithuanian>lower ] when ] bi ;
88 : locale>upper ( string -- string' )
90 [ i-dot? [ turk>upper ] when ]
91 [ lithuanian? [ lithuanian>upper ] when ] bi ;
95 : >lower ( string -- lower )
96 locale>lower final-sigma
97 [ lower>> ] [ ch>lower ] map-case ;
99 HINTS: >lower string ;
101 : >upper ( string -- upper )
103 [ upper>> ] [ ch>upper ] map-case ;
105 HINTS: >upper string ;
109 : (>title) ( string -- title )
111 [ title>> ] [ ch>title ] map-case ; inline
115 : capitalize ( string -- title )
116 unclip-slice 1string [ >lower ] [ (>title) ] bi*
117 "" prepend-as ; inline
119 : >title ( string -- title )
120 final-sigma >words [ capitalize ] map! concat ;
122 HINTS: >title string ;
124 : >case-fold ( string -- fold )
127 : lower? ( string -- ? ) dup >lower = ;
129 : upper? ( string -- ? ) dup >upper = ;
131 : title? ( string -- ? ) dup >title = ;
133 : case-fold? ( string -- ? ) dup >case-fold = ;