]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/case/case.factor
unicode.case: slightly cleaner locale case handling.
[factor.git] / basis / unicode / case / case.factor
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 ;
7 QUALIFIED: ascii
8 IN: unicode.case
9
10 SYMBOL: locale ! Just casing locale, or overall?
11
12 <PRIVATE
13
14 : split-subseq ( string sep -- strings )
15     [ dup ] swap '[ _ split1-slice swap ] produce nip ;
16
17 : replace ( old new str -- newstr )
18     [ split-subseq ] dip join ; inline
19
20 : i-dot? ( locale -- ? )
21     { "tr" "az" } member? ; inline
22
23 : lithuanian? ( locale -- ? ) "lt" = ; inline
24
25 : lithuanian>upper ( string -- lower )
26     "i\u000307" "i" replace
27     "j\u000307" "j" replace ;
28
29 : mark-above? ( ch -- ? )
30     combining-class 230 = ;
31
32 :: with-rest ( seq quot: ( seq -- seq ) -- seq )
33     seq unclip quot dip prefix ; inline
34
35 : add-dots ( seq -- seq )
36     [ [ { } ] [
37         [
38             dup first
39             { [ mark-above? ] [ CHAR: combining-ogonek = ] } 1||
40             [ CHAR: combining-dot-above prefix ] when
41         ] map
42     ] if-empty ] with-rest ; inline
43
44 : lithuanian>lower ( string -- lower )
45     "I" split add-dots "I" join
46     "J" split add-dots "J" join ; inline
47
48 : turk>upper ( string -- upper-i )
49     "i" "I\u000307" replace ; inline
50
51 : turk>lower ( string -- lower-i )
52     "I\u000307" "i" replace
53     "I" "\u000131" replace ; inline
54
55 : fix-sigma-end ( string -- string )
56     [ "" ] [
57         dup last CHAR: greek-small-letter-sigma =
58         [ 1 head* CHAR: greek-small-letter-final-sigma suffix ] when
59     ] if-empty ; inline
60
61 : sigma-map ( string -- string )
62     { CHAR: greek-capital-letter-sigma } split [ [
63         [ { CHAR: greek-small-letter-sigma } ] [
64             dup first uncased?
65             CHAR: greek-small-letter-final-sigma
66             CHAR: greek-small-letter-sigma ? prefix
67         ] if-empty
68     ] map ] with-rest concat fix-sigma-end ; inline
69
70 : final-sigma ( string -- string )
71     CHAR: greek-capital-letter-sigma
72     over member? [ sigma-map ] when
73     "" like ; inline
74
75 :: map-case ( string string-quot char-quot -- case )
76     string length <sbuf> :> out
77     string [
78         dup special-case
79         [ string-quot call out push-all ]
80         [ char-quot call out push ] ?if
81     ] each out "" like ; inline
82
83 : locale>lower ( string -- string' )
84     locale get
85     [ i-dot? [ turk>lower ] when ]
86     [ lithuanian? [ lithuanian>lower ] when ] bi ;
87
88 : locale>upper ( string -- string' )
89     locale get
90     [ i-dot? [ turk>upper ] when ]
91     [ lithuanian? [ lithuanian>upper ] when ] bi ;
92
93 PRIVATE>
94
95 : >lower ( string -- lower )
96     locale>lower final-sigma
97     [ lower>> ] [ ch>lower ] map-case ;
98
99 HINTS: >lower string ;
100
101 : >upper ( string -- upper )
102     locale>upper
103     [ upper>> ] [ ch>upper ] map-case ;
104
105 HINTS: >upper string ;
106
107 <PRIVATE
108
109 : (>title) ( string -- title )
110     locale>upper
111     [ title>> ] [ ch>title ] map-case ; inline
112
113 PRIVATE>
114
115 : capitalize ( string -- title )
116     unclip-slice 1string [ >lower ] [ (>title) ] bi*
117     "" prepend-as ; inline
118
119 : >title ( string -- title )
120     final-sigma >words [ capitalize ] map! concat ;
121
122 HINTS: >title string ;
123
124 : >case-fold ( string -- fold )
125     >upper >lower ;
126
127 : lower? ( string -- ? ) dup >lower = ;
128
129 : upper? ( string -- ? ) dup >upper = ;
130
131 : title? ( string -- ? ) dup >title = ;
132
133 : case-fold? ( string -- ? ) dup >case-fold = ;