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? ;
23 : lithuanian? ( -- ? ) locale get "lt" = ;
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
85 : >lower ( string -- lower )
86 i-dot? [ turk>lower ] when
87 lithuanian? [ lithuanian>lower ] when
89 [ lower>> ] [ ch>lower ] map-case ;
91 HINTS: >lower string ;
93 : >upper ( string -- upper )
94 i-dot? [ turk>upper ] when
95 lithuanian? [ lithuanian>upper ] when
96 [ upper>> ] [ ch>upper ] map-case ;
98 HINTS: >upper string ;
102 : (>title) ( string -- title )
103 i-dot? [ turk>upper ] when
104 lithuanian? [ lithuanian>upper ] when
105 [ title>> ] [ ch>title ] map-case ; inline
109 : capitalize ( string -- title )
110 unclip-slice 1string [ >lower ] [ (>title) ] bi*
111 "" prepend-as ; inline
113 : >title ( string -- title )
114 final-sigma >words [ capitalize ] map! concat ;
116 HINTS: >title string ;
118 : >case-fold ( string -- fold )
121 : lower? ( string -- ? ) dup >lower = ;
123 : upper? ( string -- ? ) dup >upper = ;
125 : title? ( string -- ? ) dup >title = ;
127 : case-fold? ( string -- ? ) dup >case-fold = ;