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

  1.  
  2.  
  3.  
  4.  
  5. # include "strings.h"
  6.  
  7. function compare{(left, right: String):StrCmpResult};
  8.    var lenl, lenr: Nat0;  ltail, rtail: stringtail;
  9.        state: (GoOn, Less, Greater, Stop);
  10. begin
  11.   lenl  := lengthS(left); lenr  := lengthS(right);
  12.   { -- Do trivial cases first }
  13.   if lenl = 0 then
  14.     if lenr = 0 then compare := eq else compare := lt
  15.   else    if lenr = 0 then compare := gt else begin
  16.    { -- Non-trivial cases - both left and right are non empty }
  17.    ltail := left^.TAIL;    rtail := right^.TAIL;
  18.    if left^.HEAD < right^.HEAD then state := Less else
  19.    if left^.HEAD > right^.HEAD then state := Greater else
  20.    if (ltail = nil) or (rtail = nil)
  21.    then state := Stop
  22.    else state := GoOn;
  23.    { -- Check tails if necessary }
  24.    while state = GoOn do
  25.     if ltail^.MORE < rtail^.MORE then state := Less else
  26.     if ltail^.MORE > rtail^.MORE then state := Greater else
  27.     if (ltail^.REST = nil) or (rtail^.REST = nil)
  28.     then state := Stop
  29.     else
  30.      begin ltail := ltail^.REST; rtail := rtail^.REST end;
  31.    { -- Final check for differing lengths (etc.) }
  32.    case state of
  33.     Less:     compare := lt;
  34.     Greater: compare := gt;
  35.     Stop:     if lenl < lenr then compare := lt else
  36.          if lenl > lenr then compare := gt
  37.          else compare := eq
  38.    end
  39.   end;
  40.   { -- comparison may have involved constant strings }
  41.   if left  <> nil then if  left^.REFS = 0 then disposeS(left);
  42.   if right <> nil then if right^.REFS = 0 then disposeS(right)
  43. end{ -- compare};
  44.