summaryrefslogtreecommitdiff
path: root/ipl/procs/escape.icn
blob: 0a6ea6fa33445f50b9278f1cca620350bf9da51c (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
############################################################################
#
#	File:     escape.icn
#
#	Subject:  Procedures to interpret Icon literal escapes
#
#	Authors:  William H. Mitchell
#
#	Date:     April 16, 1993
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#	Contributors:  Ralph E. Griswold and Alan Beale
#
############################################################################
#
#  The procedure escape(s) produces a string in which Icon quoted
#  literal escape conventions in s are replaced by the corresponding
#  characters.  For example, escape("\\143\\141\\164") produces the
#  string "cat".
#
############################################################################
#
#  Links: ebcdic
#
############################################################################

link ebcdic

procedure escape(s)
   local ns, c

   ns := ""
   s ? {
      while ns ||:= tab(upto('\\')) do {
         move(1)
         ns ||:= case map(c := move(1)) | fail of {	# trailing \ illegal
            "b":  "\b"
            "d":  "\d"
            "e":  "\e"
            "f":  "\f"
            "l":  "\n"
            "n":  "\n"
            "r":  "\r"
            "t":  "\t"
            "v":  "\v"
            "x":  hexcode()
            "^":  ctrlcode()
            !"01234567":  octcode()
            default:  c				# takes care of ", ', and \
            }
         }
      return ns || tab(0)
      }

end

procedure hexcode()
   local i, s

   s := tab(many('0123456789ABCDEFabcdef')) | ""	# get hex digits

   if (i := *s) > 2 then {		# if too many digits, back off
      s := s[1:3]
      move(*s - i)
      }
   
   return char("16r" || s)

end

procedure octcode()
   local i, s

   move(-1)				# put back first octal digit
   s := tab(many('01234567')) | ""	# get octal digits

   i := *s
   if (i := *s) > 3 then {		# back off if too large
      s := s[1:4]
      move(*s - i)
      }
   if s > 377 then {			# still could be too large
      s := s[1:3]
      move(-1)
      }

   return char("8r" || s)

end

procedure ctrlcode(s)

   return Control(move(1))

end