1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: ascii assocs byte-arrays fry hints kernel lexer math
4 parser sequences sequences.private strings summary words ;
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 )
20 [ 128 <iota> ] 3dip zip
21 '[ [ _ call( x -- y ) _ at ] keep or ] B{ } map-as ; inline
23 : tr-hints ( word -- )
24 { { byte-array } { string } } set-specializer ;
26 : create-tr ( token -- word )
27 create-word-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 ;
44 scan-token parse-definition
45 unclip-last [ unclip-last ] dip compute-tr
47 [ [ create-tr ] dip define-tr ]
48 [ [ "-fast" append create-tr ] dip define-fast-tr ] 2tri ;