home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pstrings / part01 / matchS.p < prev    next >
Encoding:
Text File  |  1991-08-07  |  818 b   |  33 lines

  1.  
  2.  
  3.  
  4.  
  5. # include "strings.h"
  6.  
  7. function matchS{(s, pat: String):Nat0};
  8. {
  9. * Returns position of pat in s or 0 if not present.
  10. * Empty strings are not considered present!
  11. }
  12.   var diff, lens, lenp, start, next: Nat0;  nomatch: Boolean;
  13. begin
  14.   lens := lengthS(s);  lenp := lengthS(pat);
  15.   if (lens = 0) or (lenp = 0) or (lenp > lens) then
  16.     matchS := 0
  17.   else begin
  18.     start := 0;
  19.     diff := lens - lenp;
  20.     repeat
  21.         start := start+1;
  22.         next := 0;
  23.         repeat
  24.                next := next+1;
  25.                nomatch := indexS(pat, next) <> indexS(s, start+next-1)
  26.                 until nomatch or (next = lenp);
  27.     until not nomatch or (start > diff);
  28.     if nomatch then matchS := 0 else matchS := start
  29.   end;
  30.   { -- possible that function called with constant string for pat }
  31.   if pat <> nil then if pat^.REFS = 0 then disposeS(pat)
  32. end{ -- matchS};
  33.