home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / STRINGS.ICN < prev    next >
Text File  |  1991-07-13  |  2KB  |  84 lines

  1. ############################################################################
  2. #
  3. #    Name:    strings.icn
  4. #
  5. #    Title:    String utilities
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    May 26, 1989
  10. #
  11. ############################################################################
  12. #  
  13. #  These procedures perform simple operations on strings.
  14. #  
  15. #       compress(s,c)      Compress consecutive occurrences of charac-
  16. #                          ters in c that occur in s.
  17. #  
  18. #       omit(s,c)          Omit all occurrences of characters in c
  19. #                          that occur in s.
  20. #
  21. #       replace(s1,s2,s3)  In s1, replace all occurrences of s2 by s3.
  22. #  
  23. #       rotate(s,i)        Rotate s i characters to the left (negative i
  24. #                          produces rotation to the right); the default
  25. #                          value of i is 1.
  26. #  
  27. ############################################################################
  28.  
  29. procedure compress(s,c)
  30.    local result, s1
  31.  
  32.    result := ""
  33.  
  34.    s ? {
  35.       while result ||:= tab(upto(c)) do {
  36.          result ||:= (s1 := move(1))
  37.          tab(many(s1))
  38.          }
  39.       return result || tab(0)
  40.       }
  41. end
  42.  
  43. #  omit characters
  44. #
  45. procedure omit(s,c)
  46.    local result, s1
  47.  
  48.    result := ""
  49.  
  50.    s ? {
  51.       while result ||:= tab(upto(c)) do {
  52.          s1 := move(1)
  53.          tab(many(s1))
  54.          }
  55.       return result || tab(0)
  56.       }
  57. end
  58.  
  59. #  replace string
  60. #
  61. procedure replace(s1,s2,s3)
  62.    local result, i
  63.  
  64.    result := ""
  65.    i := *s2
  66.  
  67.    s1 ? {
  68.       while result ||:= tab(find(s2)) do {
  69.          result ||:= s3
  70.          move(i)
  71.          }
  72.       return result || tab(0)
  73.       }
  74. end
  75.  
  76. #  rotate string
  77. #
  78. procedure rotate(s,i)
  79.    /i := 1
  80.    if i <= 0 then i +:= *s
  81.    i %:= *s
  82.    return s[i + 1:0] || s[1:i + 1]
  83. end
  84.