home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / exoten / icon / escape.icn < prev    next >
Encoding:
Text File  |  1990-03-05  |  1.7 KB  |  75 lines

  1. ############################################################################
  2. #
  3. #    Name:    escape.icn
  4. #
  5. #    Title:    Interpret Icon literal escapes
  6. #
  7. #    Author:    William H. Mitchell, modified by Ralph E. Griswold
  8. #
  9. #    Date:    November 21, 1988
  10. #
  11. ############################################################################
  12. #  
  13. #  The procedure escape(s) produces a string in which Icon quoted
  14. #  literal escape conventions in s are replaced by the corresponding
  15. #  characters.  For example, escape("\\143\\141\\164") produces the
  16. #  string "cat".
  17. #  
  18. ############################################################################
  19.  
  20. procedure escape(s)
  21.    local ns, c
  22.  
  23.    ns := ""
  24.    s ? {
  25.       while ns ||:= tab(upto('\\')) do {
  26.          move(1)
  27.          ns ||:= case c := move(1 | 0) of {
  28.             "b":  "\b"
  29.             "d":  "\d"
  30.             "e":  "\e"
  31.             "f":  "\f"
  32.             "l":  "\n"
  33.             "n":  "\n"
  34.             "r":  "\r"
  35.             "t":  "\t"
  36.             "v":  "\v"
  37.             "'":  "'"
  38.             "\"":  "\""
  39.             "x":  hexcode()
  40.             "^":  ctrlcode()
  41.             !"01234567":  octcode()
  42.             default:  c
  43.             }
  44.          }
  45.       ns ||:= tab(0)
  46.       }
  47.    return ns
  48. end
  49.  
  50. procedure hexcode()
  51.    local i, s
  52.    static cdigs
  53.    initial cdigs := ~'0123456789ABCDEFabcdef'
  54.    
  55.    move(i := 2 | 1) ? s := tab(upto(cdigs) | 0)
  56.    move(*s - i)
  57.    return char("16r" || s)
  58. end
  59.  
  60. procedure octcode()
  61.    local i, s
  62.    static cdigs
  63.    initial cdigs := ~'01234567'
  64.    
  65.    move(-1)
  66.    move(i := 3 | 2 | 1) ? s := tab(upto(cdigs) | 0)
  67.    move(*s - i)
  68.    return char("8r" || s)
  69. end
  70.  
  71. procedure ctrlcode(s)
  72.    return char(upto(map(move(1)),&lcase))
  73. end
  74.  
  75.