procedure match(pattern:line_string; var first,last:integer; upper,word:boolean);
var i,l:integer;
begin
l:=length(pattern); i:=l-1;
if first<last then
    begin
    first:=first+1;
    while (first<=last-l+1) and (i<l) do
        begin
        i:=0;
        if upper then
            while (i<l) and (UpCase(text[first+i])=UpCase(pattern[1+i])) do
                i:=i+1
        else while (i<l) and (text[first+i]=pattern[1+i]) do i:=i+1;
        if (i=l) and word then
            if ((first>1) and (text[first-1] in letters)) or
               ((first+l<=text_length) and (text[first+l] in letters)) then
               i:=l-1;
        if i<l then first:=first+1;
        end;
    end
else if first>last then
    begin
    first:=first-1;
    while (first>=last+l-1) and (i<l) do
        begin
        i:=0;
        if upper then
            while (i<l) and (UpCase(text[first-i])=UpCase(pattern[l-i])) do
                i:=i+1
        else while (i<l) and (text[first-i]=pattern[l-i]) do i:=i+1;
        if (i=l) and word then
            if ((first<text_length) and (text[first+1] in letters)) or
               ((first-l>=1) and (text[first-l] in letters)) then
               i:=l-1;
        if i<l then first:=first-1 else first:=first-l+1;
        end;
    end;
if i<l then first:=0;
end; { match }
