home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / max130.zip / TRIM.MAX < prev    next >
Text File  |  1988-10-30  |  3KB  |  124 lines

  1.  
  2. ; Trim any space, tab or newline characters from the left
  3. ; side of a string.
  4.  
  5. Function ltrim
  6.     Begin
  7.  
  8.         If ne (argc () 1)
  9.             Then
  10.                 put ("usage ltrim (string)")
  11.                 return ("")
  12.             EndThen
  13.         EndIf
  14.  
  15.         set (pd
  16.             patcre (
  17.                 patgrp (" \t\n" 0 100)
  18.                 patset (string patrem ())
  19.             )
  20.         )
  21.  
  22.         set (found patmatch (pd argv (1)))
  23.         patfre (pd)
  24.  
  25.         If found
  26.             Then
  27.                 return (string)
  28.             EndThen
  29.             Else
  30.                 return ("")
  31.             EndElse
  32.         EndIf
  33.  
  34. EndFunction
  35.  
  36. ; Trim any space, tab or newline characters from the right
  37. ; side of a string.
  38.  
  39. Function rtrim
  40.     Begin
  41.  
  42.         If ne (argc () 1)
  43.             Then
  44.                 put ("usage rtrim (string)")
  45.                 return ("")
  46.             EndThen
  47.         EndIf
  48.  
  49.         set (string argv (1))
  50.  
  51.         ; Note: do not use
  52.         ;       patset (result patarb (1 100))
  53.         ;       patgrp (" \t\n" 0 100)
  54.         ; because patgrp will always return true on a match
  55.         ; of zero characters and patarb will only match the
  56.         ; minimum. To get patarb to advance and re-evaluate
  57.         ; requires that the remainder of the pattern does
  58.         ; not return true until it matches something.
  59.         ; Therefore it should not be used after a patarb because
  60.         ; the patarb will match the minimum.
  61.  
  62.         ; Another problem with:
  63.         ;       patset (result patarb (1 100))
  64.         ;       patgrp (" \t\n" 1 100)
  65.         ; is that if there is more than one leading space then
  66.         ; the patgrp will match the leading spaces and not the
  67.         ; trailing ones.
  68.  
  69.         set (pd
  70.             patcre (
  71.                 patset (result patarb ())
  72.                 patgrp (" \t\n" 1 100) pateol ()
  73.             )
  74.         )
  75.  
  76.         set (found patmatch (pd string))
  77.         patfre (pd)
  78.  
  79.         If found
  80.             Then
  81.                 return (result)
  82.             EndThen
  83.             Else
  84.                 return (string)
  85.             EndElse
  86.         EndIf
  87.  
  88. EndFunction
  89.  
  90. ; Trim any space, tab or newline characters from both
  91. ; left and right sides of a string.
  92.  
  93. Function trim
  94.     Begin
  95.  
  96.         If ne (argc () 1)
  97.             Then
  98.                 put ("usage: trim (string)")
  99.                 return ("")
  100.             EndThen
  101.         EndIf
  102.  
  103.         set (string argv (1))
  104.         set (result "")
  105.  
  106.         set (pd
  107.             patcre (
  108.                 patgrp (" \t\n" 0 100)
  109.                 patset (result patarb ())
  110.                 patalt (
  111.                     patpat (patgrp (" \t\n" 1 100) pateol ())
  112.                     pateol ()
  113.                 )
  114.             )
  115.         )
  116.  
  117.         set (found patmatch (pd string))
  118.         patfre (pd)
  119.  
  120.         return (result)
  121.  
  122. EndFunction
  123.  
  124.