| 1 |
############################################################################ |
| 2 |
# |
| 3 |
# File: escape.icn |
| 4 |
# |
| 5 |
# Subject: Procedures to interpret Icon literal escapes |
| 6 |
# |
| 7 |
# Authors: William H. Mitchell; modified by Ralph E. Griswold and |
| 8 |
# Alan Beale |
| 9 |
# |
| 10 |
# Date: April 16, 1993 |
| 11 |
# |
| 12 |
############################################################################ |
| 13 |
# |
| 14 |
# The procedure escape(s) produces a string in which Icon quoted |
| 15 |
# literal escape conventions in s are replaced by the corresponding |
| 16 |
# characters. For example, escape("\\143\\141\\164") produces the |
| 17 |
# string "cat". |
| 18 |
# |
| 19 |
############################################################################ |
| 20 |
# |
| 21 |
# Links: ebcdic |
| 22 |
# |
| 23 |
############################################################################ |
| 24 |
|
| 25 |
link ebcdic |
| 26 |
|
| 27 |
procedure escape(s) |
| 28 |
local ns, c |
| 29 |
|
| 30 |
ns := "" |
| 31 |
s ? { |
| 32 |
while ns ||:= tab(upto('\\')) do { |
| 33 |
move(1) |
| 34 |
ns ||:= case map(c := move(1)) | fail of { # trailing \ illegal |
| 35 |
"b": "\b" |
| 36 |
"d": "\d" |
| 37 |
"e": "\e" |
| 38 |
"f": "\f" |
| 39 |
"l": "\n" |
| 40 |
"n": "\n" |
| 41 |
"r": "\r" |
| 42 |
"t": "\t" |
| 43 |
"v": "\v" |
| 44 |
"x": hexcode() |
| 45 |
"^": ctrlcode() |
| 46 |
!"01234567": octcode() |
| 47 |
default: c # takes care of ", ', and \ |
| 48 |
} |
| 49 |
} |
| 50 |
return ns || tab(0) |
| 51 |
} |
| 52 |
|
| 53 |
end |
| 54 |
|
| 55 |
procedure hexcode() |
| 56 |
local i, s |
| 57 |
|
| 58 |
s := tab(many('0123456789ABCDEFabcdef')) | "" # get hex digits |
| 59 |
|
| 60 |
if (i := *s) > 2 then { # if too many digits, back off |
| 61 |
s := s[1:3] |
| 62 |
move(*s - i) |
| 63 |
} |
| 64 |
|
| 65 |
return char("16r" || s) |
| 66 |
|
| 67 |
end |
| 68 |
|
| 69 |
procedure octcode() |
| 70 |
local i, s |
| 71 |
|
| 72 |
move(-1) # put back first octal digit |
| 73 |
s := tab(many('01234567')) | "" # get octal digits |
| 74 |
|
| 75 |
i := *s |
| 76 |
if (i := *s) > 3 then { # back off if too large |
| 77 |
s := s[1:4] |
| 78 |
move(*s - i) |
| 79 |
} |
| 80 |
if s > 377 then { # still could be too large |
| 81 |
s := s[1:3] |
| 82 |
move(-1) |
| 83 |
} |
| 84 |
|
| 85 |
return char("8r" || s) |
| 86 |
|
| 87 |
end |
| 88 |
|
| 89 |
procedure ctrlcode(s) |
| 90 |
|
| 91 |
return Control(move(1)) |
| 92 |
|
| 93 |
end |