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