1 USING: accessors arrays assocs combinators.short-circuit fry
2 hints interval-maps kernel math math.order sequences sorting
3 strings unicode.breaks.private unicode.case.private
4 unicode.categories unicode.collation unicode.collation.private
5 unicode.data unicode.data.private unicode.normalize.private
9 CATEGORY: blank Zs Zl Zp | "\r\n\t" member? ;
11 CATEGORY: letter Ll | "Other_Lowercase" property? ;
13 CATEGORY: LETTER Lu | "Other_Uppercase" property? ;
15 CATEGORY: Letter Lu Ll Lt Lm Lo Nl ;
17 CATEGORY: digit Nd Nl No ;
19 CATEGORY-NOT: printable Cc Cf Cs Co Cn ;
21 CATEGORY: alpha Lu Ll Lt Lm Lo Nd Nl No | "Other_Alphabetic" property? ;
23 CATEGORY: control Cc ;
25 CATEGORY-NOT: uncased Lu Ll Lt Lm Mn Me ;
27 CATEGORY-NOT: character Cn ;
29 CATEGORY: math Sm | "Other_Math" property? ;
31 : script-of ( char -- script )
32 script-table interval-at ;
34 : name>char ( name -- char ) name-map at ; inline
36 : char>name ( char -- name ) name-map value-at ; inline
38 : ch>lower ( ch -- lower ) simple-lower ?at drop ; inline
40 : ch>upper ( ch -- upper ) simple-upper ?at drop ; inline
42 : ch>title ( ch -- title ) simple-title ?at drop ; inline
44 : first-grapheme ( str -- i )
45 unclip-slice grapheme-class over
46 [ grapheme-class [ nip ] [ grapheme-break? ] 2bi ] find drop
47 nip swap length or 1 + ;
49 : first-grapheme-from ( start str -- i )
50 over tail-slice first-grapheme + ;
52 : last-grapheme ( str -- i )
53 unclip-last-slice grapheme-class swap
54 [ grapheme-class dup rot grapheme-break? ] find-last drop ?1+ nip ;
56 : last-grapheme-from ( end str -- i )
57 swap head-slice last-grapheme ;
61 : >pieces ( str quot: ( str -- i ) -- graphemes )
62 [ dup empty? not ] swap '[ dup @ cut-slice swap ] produce nip ; inline
66 : >graphemes ( str -- graphemes )
67 [ first-grapheme ] >pieces ;
69 : string-reverse ( str -- rts )
70 >graphemes reverse! concat ;
72 : first-word ( str -- i )
73 [ [ length ] [ first word-break-prop ] bi ] keep
74 1 swap dup '[ _ word-break-next ] find-index-from
77 : >words ( str -- words )
78 [ first-word ] >pieces ;
82 : nth-next ( i str -- str[i-1] str[i] )
83 [ [ 1 - ] keep ] dip '[ _ nth ] bi@ ;
87 : word-break-at? ( i str -- ? )
92 [ nth-next [ word-break-prop ] dip ] 2keep
97 : first-word-from ( start str -- i )
98 over tail-slice first-word + ;
100 : last-word ( str -- i )
101 [ length <iota> ] keep '[ _ word-break-at? ] find-last drop 0 or ;
103 : last-word-from ( end str -- i )
104 swap head-slice last-word ;
106 : >lower ( string -- lower )
107 locale>lower final-sigma
108 [ lower>> ] [ ch>lower ] map-case ;
110 HINTS: >lower string ;
112 : >upper ( string -- upper )
114 [ upper>> ] [ ch>upper ] map-case ;
116 HINTS: >upper string ;
120 : (>title) ( string -- title )
122 [ title>> ] [ ch>title ] map-case ; inline
126 : capitalize ( string -- title )
127 unclip-slice 1string [ >lower ] [ (>title) ] bi*
128 "" prepend-as ; inline
130 : >title ( string -- title )
131 final-sigma >words [ capitalize ] map! concat ;
133 HINTS: >title string ;
135 : >case-fold ( string -- fold )
138 : lower? ( string -- ? ) dup >lower sequence= ;
140 : upper? ( string -- ? ) dup >upper sequence= ;
142 : title? ( string -- ? ) dup >title sequence= ;
144 : case-fold? ( string -- ? ) dup >case-fold sequence= ;
146 : nfd ( string -- nfd )
147 [ (nfd) ] with-string ;
149 : nfkd ( string -- nfkd )
150 [ (nfkd) ] with-string ;
152 : string-append ( s1 s2 -- string )
154 0 over ?nth non-starter?
155 [ length dupd reorder-back ] [ drop ] if ;
157 HINTS: string-append string string ;
159 : nfc ( string -- nfc )
160 [ (nfd) combine ] with-string ;
162 : nfkc ( string -- nfkc )
163 [ (nfkd) combine ] with-string ;
165 : collation-key/nfd ( string -- key nfd )
167 string>graphemes graphemes>weights
168 filter-ignorable weights>bytes
173 : insensitive= ( str1 str2 levels-removed -- ? )
175 [ collation-key/nfd drop ] dip
176 [ [ 0 = not ] trim-tail but-last ] times
181 : primary= ( str1 str2 -- ? )
184 : secondary= ( str1 str2 -- ? )
187 : tertiary= ( str1 str2 -- ? )
190 : quaternary= ( str1 str2 -- ? )
193 : sort-strings ( strings -- sorted )
194 [ collation-key/nfd 2array ] map natural-sort values ;
196 : string<=> ( str1 str2 -- <=> )
197 [ collation-key/nfd 2array ] compare ;
199 CONSTANT: unicode-supported {
203 CONSTANT: unicode-unsupported {
207 CONSTANT: unicode-version "10.0"