]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/case/case.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / unicode / case / case.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: unicode.data sequences sequences.next namespaces
4 sbufs make unicode.syntax unicode.normalize math hints
5 unicode.categories combinators unicode.syntax assocs
6 strings splitting kernel accessors unicode.breaks fry locals ;
7 QUALIFIED: ascii
8 IN: unicode.case
9
10 <PRIVATE
11 : ch>lower ( ch -- lower ) simple-lower at-default ; inline
12 : ch>upper ( ch -- upper ) simple-upper at-default ; inline
13 : ch>title ( ch -- title ) simple-title at-default ; inline
14 PRIVATE>
15
16 SYMBOL: locale ! Just casing locale, or overall?
17
18 <PRIVATE
19
20 : split-subseq ( string sep -- strings )
21     [ dup ] swap '[ _ split1-slice swap ] [ ] produce nip ;
22
23 : replace ( old new str -- newstr )
24     [ split-subseq ] dip join ; inline
25
26 : i-dot? ( -- ? )
27     locale get { "tr" "az" } member? ;
28
29 : lithuanian? ( -- ? ) locale get "lt" = ;
30
31 : dot-over ( -- ch ) HEX: 307 ;
32
33 : lithuanian>upper ( string -- lower )
34     "i\u000307" "i" replace
35     "j\u000307" "j" replace ;
36
37 : mark-above? ( ch -- ? )
38     combining-class 230 = ;
39
40 : with-rest ( seq quot: ( seq -- seq ) -- seq )
41     [ unclip ] dip swap slip prefix ; inline
42
43 : add-dots ( seq -- seq )
44     [ [ "" ] [
45         dup first mark-above?
46         [ CHAR: combining-dot-above prefix ] when
47     ] if-empty ] with-rest ; inline
48
49 : lithuanian>lower ( string -- lower )
50     "i" split add-dots "i" join
51     "j" split add-dots "i" join ; inline
52
53 : turk>upper ( string -- upper-i )
54     "i" "I\u000307" replace ; inline
55
56 : turk>lower ( string -- lower-i )
57     "I\u000307" "i" replace
58     "I" "\u000131" replace ; inline
59
60 : fix-sigma-end ( string -- string )
61     [ "" ] [
62         dup peek CHAR: greek-small-letter-sigma =
63         [ 1 head* CHAR: greek-small-letter-final-sigma suffix ] when
64     ] if-empty ; inline
65
66 : sigma-map ( string -- string )
67     { CHAR: greek-capital-letter-sigma } split [ [
68         [ { CHAR: greek-small-letter-sigma } ] [
69             dup first uncased?
70             CHAR: greek-small-letter-final-sigma
71             CHAR: greek-small-letter-sigma ? prefix
72         ] if-empty
73     ] map ] with-rest concat fix-sigma-end ; inline
74
75 : final-sigma ( string -- string )
76     CHAR: greek-capital-letter-sigma
77     over member? [ sigma-map ] when
78     "" like ; inline
79
80 :: map-case ( string string-quot char-quot -- case )
81     string length <sbuf> :> out
82     string [
83         dup special-casing at
84         [ string-quot call out push-all ]
85         [ char-quot call out push ] ?if
86     ] each out "" like ; inline
87
88 PRIVATE>
89
90 : >lower ( string -- lower )
91     i-dot? [ turk>lower ] when final-sigma
92     [ lower>> ] [ ch>lower ] map-case ;
93
94 HINTS: >lower string ;
95
96 : >upper ( string -- upper )
97     i-dot? [ turk>upper ] when
98     [ upper>> ] [ ch>upper ] map-case ;
99
100 HINTS: >upper string ;
101
102 <PRIVATE
103
104 : (>title) ( string -- title )
105     i-dot? [ turk>upper ] when
106     [ title>> ] [ ch>title ] map-case ; inline
107
108 : title-word ( string -- title )
109     unclip 1string [ >lower ] [ (>title) ] bi* prepend ; inline
110
111 PRIVATE>
112
113 : >title ( string -- title )
114     final-sigma >words [ title-word ] map concat ;
115
116 HINTS: >title string ;
117
118 : >case-fold ( string -- fold )
119     >upper >lower ;
120
121 : lower? ( string -- ? ) dup >lower = ;
122
123 : upper? ( string -- ? ) dup >upper = ;
124
125 : title? ( string -- ? ) dup >title = ;
126
127 : case-fold? ( string -- ? ) dup >case-fold = ;