factor/basis/unicode/case/case.factor

128 lines
3.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2009 Daniel Ehrenberg.
2008-10-05 19:36:56 -04:00
! See http://factorcode.org/license.txt for BSD license.
2009-02-05 21:28:36 -05:00
USING: unicode.data sequences namespaces
sbufs make unicode.normalize math hints
unicode.categories combinators assocs combinators.short-circuit
2009-01-09 15:03:33 -05:00
strings splitting kernel accessors unicode.breaks fry locals ;
QUALIFIED: ascii
2008-01-09 14:44:07 -05:00
IN: unicode.case
SYMBOL: locale ! Just casing locale, or overall?
2009-01-07 22:45:33 -05:00
2009-01-07 18:59:01 -05:00
<PRIVATE
: i-dot? ( locale -- ? )
{ "tr" "az" } member? ; inline
2008-01-09 14:44:07 -05:00
: lithuanian? ( locale -- ? ) "lt" = ; inline
2008-01-09 14:44:07 -05:00
: lithuanian>upper ( string -- lower )
"i\u000307" "i" replace
"j\u000307" "j" replace ;
2008-01-09 14:44:07 -05:00
: mark-above? ( ch -- ? )
combining-class 230 = ;
:: with-rest ( seq quot: ( seq -- seq ) -- seq )
seq unclip quot dip prefix ; inline
2008-01-09 14:44:07 -05:00
: add-dots ( seq -- seq )
[ [ { } ] [
[
dup first
{ [ mark-above? ] [ CHAR: combining-ogonek = ] } 1||
[ CHAR: combining-dot-above prefix ] when
] map
2009-01-09 15:03:33 -05:00
] if-empty ] with-rest ; inline
2008-01-09 14:44:07 -05:00
: lithuanian>lower ( string -- lower )
"I" split add-dots "I" join
"J" split add-dots "J" join ; inline
2008-01-09 14:44:07 -05:00
: turk>upper ( string -- upper-i )
2009-01-09 15:03:33 -05:00
"i" "I\u000307" replace ; inline
2008-01-09 14:44:07 -05:00
: turk>lower ( string -- lower-i )
"I\u000307" "i" replace
2009-01-09 15:03:33 -05:00
"I" "\u000131" replace ; inline
2008-01-09 14:44:07 -05:00
: fix-sigma-end ( string -- string )
[ "" ] [
dup last CHAR: greek-small-letter-sigma =
[ 1 head* CHAR: greek-small-letter-final-sigma suffix ] when
2009-01-09 15:03:33 -05:00
] if-empty ; inline
2008-01-09 14:44:07 -05:00
: sigma-map ( string -- string )
{ CHAR: greek-capital-letter-sigma } split [ [
[ { CHAR: greek-small-letter-sigma } ] [
dup first uncased?
CHAR: greek-small-letter-final-sigma
CHAR: greek-small-letter-sigma ? prefix
] if-empty
2009-01-09 15:03:33 -05:00
] map ] with-rest concat fix-sigma-end ; inline
2008-01-09 14:44:07 -05:00
: final-sigma ( string -- string )
CHAR: greek-capital-letter-sigma
2009-01-09 15:03:33 -05:00
over member? [ sigma-map ] when
"" like ; inline
2008-01-09 14:44:07 -05:00
2009-01-09 15:03:33 -05:00
:: map-case ( string string-quot char-quot -- case )
string length <sbuf> :> out
string [
dup special-case
2009-01-09 15:03:33 -05:00
[ string-quot call out push-all ]
[ char-quot call out push ] ?if
] each out "" like ; inline
2009-01-08 00:54:19 -05:00
: locale>lower ( string -- string' )
locale get
[ i-dot? [ turk>lower ] when ]
[ lithuanian? [ lithuanian>lower ] when ] bi ;
: locale>upper ( string -- string' )
locale get
[ i-dot? [ turk>upper ] when ]
[ lithuanian? [ lithuanian>upper ] when ] bi ;
PRIVATE>
: >lower ( string -- lower )
locale>lower final-sigma
2009-01-08 00:54:19 -05:00
[ lower>> ] [ ch>lower ] map-case ;
2009-01-09 15:03:33 -05:00
HINTS: >lower string ;
: >upper ( string -- upper )
locale>upper
[ upper>> ] [ ch>upper ] map-case ;
2009-01-09 15:03:33 -05:00
HINTS: >upper string ;
<PRIVATE
2009-01-08 00:54:19 -05:00
: (>title) ( string -- title )
locale>upper
2009-01-09 15:03:33 -05:00
[ title>> ] [ ch>title ] map-case ; inline
2009-01-08 00:54:19 -05:00
2009-01-07 18:59:01 -05:00
PRIVATE>
2009-01-08 00:54:19 -05:00
: capitalize ( string -- title )
2012-08-24 18:14:48 -04:00
unclip-slice 1string [ >lower ] [ (>title) ] bi*
"" prepend-as ; inline
2008-01-09 14:44:07 -05:00
: >title ( string -- title )
2012-08-24 18:14:48 -04:00
final-sigma >words [ capitalize ] map! concat ;
2008-01-09 14:44:07 -05:00
2009-01-09 15:03:33 -05:00
HINTS: >title string ;
2008-01-09 14:44:07 -05:00
: >case-fold ( string -- fold )
>upper >lower ;
: lower? ( string -- ? ) dup >lower = ;
: upper? ( string -- ? ) dup >upper = ;
: title? ( string -- ? ) dup >title = ;
: case-fold? ( string -- ? ) dup >case-fold = ;