]> gitweb.factorcode.org Git - factor.git/blob - basis/tr/tr.factor
Fix conflict
[factor.git] / basis / tr / tr.factor
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 ;
5 IN: tr
6
7 ERROR: bad-tr ;
8
9 M: bad-tr summary
10     drop "TR: can only be used with ASCII characters" ;
11
12 <PRIVATE
13
14 : ascii? ( ch -- ? ) 0 127 between? ; inline
15
16 : tr-nth ( n mapping -- ch ) nth-unsafe 127 bitand ; inline
17
18 : check-tr ( from to -- )
19     [ [ ascii? ] all? ] both? [ bad-tr ] unless ;
20
21 : compute-tr ( quot from to -- mapping )
22     zip [ 128 ] 2dip '[ [ @ _ at ] keep or ] B{ } map-as ; inline
23
24 : tr-hints ( word -- )
25     { { byte-array } { string } } "specializer" set-word-prop ;
26
27 : create-tr ( token -- word )
28     create-in dup tr-hints ;
29
30 : tr-quot ( mapping -- quot )
31     '[ [ dup ascii? [ _ tr-nth ] when ] map ] ;
32
33 : define-tr ( word mapping -- )
34     tr-quot (( seq -- translated )) define-declared ;
35
36 : fast-tr-quot ( mapping -- quot )
37     '[ [ _ tr-nth ] change-each ] ;
38
39 : define-fast-tr ( word mapping -- )
40     fast-tr-quot (( seq -- )) define-declared ;
41
42 PRIVATE>
43
44 : TR:
45     scan parse-definition
46     unclip-last [ unclip-last ] dip compute-tr
47     [ check-tr ]
48     [ [ create-tr ] dip define-tr ]
49     [ [ "-fast" append create-tr ] dip define-fast-tr ] 2tri ;
50     parsing