1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-arrays strings sequences sequences.private
4 fry kernel words parser lexer assocs math math.order summary ;
10 drop "TR: can only be used with ASCII characters" ;
14 : ascii? ( ch -- ? ) 0 127 between? ; inline
16 : tr-nth ( n mapping -- ch ) nth-unsafe 127 bitand ; inline
18 : check-tr ( from to -- )
19 [ [ ascii? ] all? ] both? [ bad-tr ] unless ;
21 : compute-tr ( quot from to -- mapping )
22 zip [ 128 ] 2dip '[ [ @ _ at ] keep or ] B{ } map-as ; inline
24 : tr-hints ( word -- )
25 { { byte-array } { string } } "specializer" set-word-prop ;
27 : create-tr ( token -- word )
28 create-in dup tr-hints ;
30 : tr-quot ( mapping -- quot )
31 '[ [ dup ascii? [ _ tr-nth ] when ] map ] ;
33 : define-tr ( word mapping -- )
34 tr-quot (( seq -- translated )) define-declared ;
36 : fast-tr-quot ( mapping -- quot )
37 '[ [ _ tr-nth ] change-each ] ;
39 : define-fast-tr ( word mapping -- )
40 fast-tr-quot (( seq -- )) define-declared ;
46 unclip-last [ unclip-last ] dip compute-tr
48 [ [ create-tr ] dip define-tr ]
49 [ [ "-fast" append create-tr ] dip define-fast-tr ] 2tri ;