Comment detail

文字変換表に基く文字列の変換 (Nested Flatten)
PostScript 版、 (2) 拡張版レベルまでです。ろくなエラーチェックをしていないので、置換文字列の字数が合わないと破綻します。
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%!PS

% translate characters "TR"
% 
% (Original) (Target) (InputString)  TR  (OutputString)


% ---- Procedures ----
/ScanText { % (String) ScanText [(S) (t) (r) (i) (n) (g)]
    [
        exch
        {
            dup length 0 le { exit } if
            dup 0 1 getinterval
            dup (-) eq {
                pop
                dup 1 1 getinterval 3 -1 roll
                0 get exch 0 get 1 exch
                {
                    1 string dup 0 % num () () 0
                    4 -1 roll put
                    exch
                } for
                dup length 2 exch 2 sub getinterval
            } {
                exch dup length 1 exch 1 sub getinterval
            } ifelse
        } loop
        pop
    ]
} def

/MakeDict { % [OriginalStrArray] [TargetStrArray] MakeDict -dict-
    dup length 1 sub
    100 dict
    0 1 4 -1 roll
    { % [OrgStr] [TargetStr] -dict- num
    1 index exch
    dup 5 index exch get exch % [OrgStr] [TargetStr] -dict- O[num] num 
        4 index exch get % (O) (T) -dict- (O[num]) (T[num])
        put
    } for
    3 1 roll pop pop
} def

/ReplaceText { % (InputString) -dict- ReplaceText (OutputString)
    1 index length 0 exch 1 exch 1 sub {
        dup
        3 index exch 1
        getinterval
        2 index exch
        2 copy known {
            get
            3 index 3 1 roll
            putinterval
        } {
            pop pop pop
        } ifelse
    } for
    pop
} def

/TR { % (Original) (Target) (InputString)  TR  (OutputString)
    3 1 roll
    ScanText
    exch ScanText exch
    MakeDict
    ReplaceText
} def

% -------------- Test Code -------------
(qwertyuiop) (QWERTYUIOP) (typewriter) TR =
(a-jklmno-z) (A-CDEFG-Z) (A quick brown fox jumps over the lazy dog.) TR =

Index

Feed

Other

Link

Pathtraq

loading...