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 : lithuanian>upper ( string -- lower )
29 "i\u000307" "i" replace
30 "j\u000307" "j" replace ;
32 : mark-above? ( ch -- ? )
33 combining-class 230 = ;
35 :: with-rest ( seq quot: ( seq -- seq ) -- seq )
36 seq unclip quot dip prefix ; inline
38 : add-dots ( seq -- seq )
42 { [ mark-above? ] [ CHAR: combining-ogonek = ] } 1||
43 [ CHAR: combining-dot-above prefix ] when
45 ] if-empty ] with-rest ; inline
47 : lithuanian>lower ( string -- lower )
48 "I" split add-dots "I" join
49 "J" split add-dots "J" join ; inline
51 : turk>upper ( string -- upper-i )
52 "i" "I\u000307" replace ; inline
54 : turk>lower ( string -- lower-i )
55 "I\u000307" "i" replace
56 "I" "\u000131" replace ; inline
58 : fix-sigma-end ( string -- string )
60 dup last CHAR: greek-small-letter-sigma =
61 [ 1 head* CHAR: greek-small-letter-final-sigma suffix ] when
64 : sigma-map ( string -- string )
65 { CHAR: greek-capital-letter-sigma } split [ [
66 [ { CHAR: greek-small-letter-sigma } ] [
68 CHAR: greek-small-letter-final-sigma
69 CHAR: greek-small-letter-sigma ? prefix
71 ] map ] with-rest concat fix-sigma-end ; inline
73 : final-sigma ( string -- string )
74 CHAR: greek-capital-letter-sigma
75 over member? [ sigma-map ] when
78 :: map-case ( string string-quot char-quot -- case )
79 string length <sbuf> :> out
82 [ string-quot call out push-all ]
83 [ char-quot call out push ] ?if
84 ] each out "" like ; inline
88 : >lower ( string -- lower )
89 i-dot? [ turk>lower ] when
90 lt? [ lithuanian>lower ] when
92 [ lower>> ] [ ch>lower ] map-case ;
94 HINTS: >lower string ;
96 : >upper ( string -- upper )
97 i-dot? [ turk>upper ] when
98 lt? [ lithuanian>upper ] when
99 [ upper>> ] [ ch>upper ] map-case ;
101 HINTS: >upper string ;
105 : (>title) ( string -- title )
106 i-dot? [ turk>upper ] when
107 lt? [ lithuanian>upper ] when
108 [ title>> ] [ ch>title ] map-case ; inline
112 : capitalize ( string -- title )
113 unclip 1string [ >lower ] [ (>title) ] bi* prepend ; inline
115 : >title ( string -- title )
116 final-sigma >words [ capitalize ] map concat ;
118 HINTS: >title string ;
120 : >case-fold ( string -- fold )
123 : lower? ( string -- ? ) dup >lower = ;
125 : upper? ( string -- ? ) dup >upper = ;
127 : title? ( string -- ? ) dup >title = ;
129 : case-fold? ( string -- ? ) dup >case-fold = ;