4.3BSD/usr/contrib/icon/src/lib/escape.icn

#	ESCAPE(2)
#
#	Interpret Icon literal strings
#
#	William H. Mitchell
#
#	Last modified 8/14/84
#

procedure escape(s)
   static digits
   initial digits := '0123456789'
   ns := ""
   s ? while c := move(1) do {
      ns ||:=
         if c == "\\" then
         case c2 := (move(1) | "") of {
            "b": "\b"
            "d": "\d"
            "e": "\e"
            "f": "\f"
            "l": "\l"
            "n": "\n"
            "r": "\r"
            "t": "\t"
            "v": "\v"
            "\'": "\'"
            "\"": "\""
            "x": hexcode(move(2))
            "^": ctrlcode(move(1))
            any(digits,c2) & c2: octcode(c2 || move(2))
            "": c
            default: {move(-1); c}
            }
      else c
      }
   return ns
end

procedure hexcode(s)
   return &cset[integer("16r" || s) + 1]
end

procedure octcode(s)
   return &cset[integer("8r" || s) + 1]
end

procedure ctrlcode(s)
   return &cset[find(map(s),&lcase) + 1]
end