[comp.os.vms] tpu searching on same line

MSIEWEKE@GTRI01.BITNET (Mike Sieweke) (09/21/87)

 >    My goal is to find a target string if if it exists in the current
 > line.  To do so, I set up the following TPU code:
 >
 ;     target := 'xyz';    ! In real life this is variable
 ;     pat1   := target | LINE_END;
 ;     pat2   := LINE_END;
 ;     target_range := search (pat1, FORWARD, EXACT);
 ;     eol_range    := search (pat2, FORWARD, EXACT);
 ;
 ;     if target_range = eol_range then
 ;         message ("String not found");
 ;     else
 ;         message ("Target found");
 ;     endif;

 > What I can't figure out is why the only response that I can get from that
 > code is "String not found" when the two ranges are distinctly different.
 > I've also tried setting "pat1 := '' & (target | LINE_END);", that doesn't
 > do what I want either.

This is almost correct.  You don't want to compare the ranges returned by
search.  You want to compare the _beginning_ of the ranges.
;   if beginning_of(target_range) = beginning_of(eol_range) then
If target is found, target_range will start with target.  If target is not
found, target_range will begin with line_end.  (Only true if using incremental
search.  See below.)

Another little tidbit:  pat1:=''&(target|line_end)  is the correct way to
do this.  TPU has 2 methods of searching: incremental and seek.  The default
method is seek.  Prepending ''& to the beginning of your pattern forces
incremental search.

In seek mode, TPU takes the first part of your pattern and searches the whole
file for it.  If the first part is not found, TPU backs up and searches for the
next part.  Thus in your code, if "target" existed beyond the current line,
search would return a range containing "target" wherever it existed.  It would
_not_ stop at line_end since it has to fail searching for "target" before it
searches for line_end.

In incremental search mode, TPU searches like you would expect: compare the
current position to the _whole_ pattern, then move if not found.

Seek mode is the faster one.  But in your case you only want to look at the
current line, and speed is not quite as important.

Hope this helps...

Mike Sieweke <msieweke@gtri01.bitnet>
Georgia Tech Research Institute
Atlanta, Georgia