home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / ESCAPE.ICN < prev    next >
Text File  |  1991-09-05  |  2KB  |  90 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, and
  8. #       Alan Beale
  9. #
  10. #       Date:   July 26, 1991
  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 ||:= {
  35.             c := map(move(1)) | break
  36.             case c of {
  37.                "b":  "\b"
  38.                "d":  "\d"
  39.                "e":  "\e"
  40.                "f":  "\f"
  41.                "l":  "\n"
  42.                "n":  "\n"
  43.                "r":  "\r"
  44.                "t":  "\t"
  45.                "v":  "\v"
  46.                "'":  "'"
  47.                "\"":  "\""
  48.                "x":  hexcode()
  49.                "^":  ctrlcode()
  50.                !"01234567":  octcode()
  51.                default:  c
  52.                }
  53.             }
  54.          }
  55.       ns ||:= tab(0)
  56.       }
  57.    return ns
  58. end
  59.  
  60. procedure hexcode()
  61.    local i, s
  62.    static hdigits
  63.  
  64.    initial hdigits := ~'0123456789ABCDEFabcdef'
  65.    
  66.    move(i := 2 | 1) ? s := tab(upto(hdigits) | 0)
  67.    move(*s - i)
  68.    return char("16r" || s)
  69. end
  70.  
  71. procedure octcode()
  72.    local i, s
  73.    static odigits
  74.  
  75.    initial odigits := ~'01234567'
  76.    
  77.    move(-1)
  78.    move(i := 3 | 2 | 1) ? s := tab(upto(odigits) | 0)
  79.    move(*s - i)
  80.    if s > 377 then {    # back off if too large
  81.       s := s[1:3]
  82.       move(-1)
  83.       }
  84.    return char("8r" || s)
  85. end
  86.  
  87. procedure ctrlcode(s)
  88.    return Control(move(1))
  89. end
  90.