File: tr.tf

package info (click to toggle)
tf5 5.0beta8-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,800 kB
  • sloc: ansic: 25,492; perl: 103; makefile: 82; sh: 79
file content (28 lines) | stat: -rw-r--r-- 883 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
;;;; Character translation
;;; usage:  /tr <domain> <range> <string>
;;; <domain> and <range> are lists of charcters.  Each character in <string>
;;; that appears in <domain> will be translated to the corresponding
;;; character in <range>.

;;; Example:
;;; command: /def biff = /tr OIS. 01Z! $[toupper({*})]
;;; command: /biff TinyFugue is cool wares, dude.
;;; output:  T1NYFUGUE 1Z C00L WAREZ, DUDE!

/loaded __TFLIB__/tr.tf

/def -i tr = \
    /let _old=%;\
    /let _new=%;\
    /let _tail=%;\
    /test _old:={1}%;\
    /test _new:={2}%;\
    /test _tail:={-2}%;\
    /let _dest=%;\
    /while /let _i=$[strchr(_tail, _old)]%; /@test _i >= 0%; /do \
        /let _j=$[strchr(_old, substr(_tail, _i, 1))]%;\
        /test _dest:=strcat(_dest, substr(_tail,0,_i), substr(_new, _j, 1))%;\
        /test _tail:=substr(_tail,_i+1)%;\
    /done%;\
    /result strcat(_dest, _tail)