module FINDSTRING use TEXTIO export FIND_STRING export CONVERT_TO_UPPER %DEFINE DEBUG %SET DEBUG = false function MATCH param MY_Unknown_Buffer : in ref packed array [1..?N] of char MY_Unknown_Length : in ref integer Test_Point : in ref integer MY_Ref_Buffer : in ref packed array [1..?M] of char MY_Ref_Length : in ref integer endparam returns Matched : boolean initially false for I := 1 to MY_Ref_Length do declare Unknown_Char : char Ref_Char : char enddeclare Unknown_Char := MY_Unknown_Buffer [(Test_Point + I - 1)] Ref_Char := MY_Ref_Buffer [I] %IF DEBUG out_string (TTY, "Unknown = ") out_Character (TTY, Unknown_Char) out_string (TTY, " Ref = ") out_character (TTY, Ref_Char) out_record (TTY) %ENDIF if Unknown_Char <> Ref_Char do Matched := false RETURN endif endfor Matched := true RETURN endfunction procedure CONVERT_TO_UPPER param Buffer : inout ref packed array [1..?N] of char Length : in ref integer endparam for C := 1 to Length do select Buffer [C] from case $a..$z: declare (Log is 8 bit logical) force log (Buffer [C]) *= AND (NOT 8#40) endselect endfor endprocedure //------------------------------------------------------------------------- // FIND A STRING WITHIN ANOTHER STRING //------------------------------------------------------------------------- // Find the LOOK_FOR_STRING within the LOOK_IN_STRING starting // at POSITION // procedure FIND_STRING param Look_In_String : in ref packed array [1..?Look_In_Length] of char Look_For_String : in ref packed array [1..?Look_For_Length] of char Found_it : out ref boolean Position : inout ref integer endparam Found_It := false declare String_Length = 132 MY_Look_In_String : packed array [1..String_Length] of char MY_Look_In_Length : integer initially Look_In_Length MY_Look_for_String : packed array [1..String_Length] of char MY_Look_For_Length : integer initially Look_for_Length Test_Point : integer enddeclare // Determine actual used lengths of input strings by loping off spaces // at the end of the input strings LOOK_IN_LOOP: block for I := Look_In_Length downto 1 do if Look_In_String [I] <> $ do MY_Look_In_Length := I break LOOK_IN_LOOP endif endfor endblock {LOOK_IN_LOOP} LOOK_FOR_LOOP: block for I := Look_for_Length downto 1 do if Look_For_String [I] <> $ do MY_Look_For_Length := I break LOOK_FOR_LOOP endif endfor endblock {LOOK_FOR_LOOP} // get a local copy of both the Look_In string and the look_for string // and convert them to all upper case for I := 1 to MY_Look_In_Length do My_Look_In_String [I] := Look_In_String [I] endfor CONVERT_TO_UPPER (MY_Look_In_String, MY_Look_In_Length) for I := 1 to MY_Look_For_Length do My_Look_For_String [I] := Look_For_String [I] endfor CONVERT_TO_UPPER (MY_Look_For_String, MY_Look_For_Length) %IF DEBUG out_string (TTY, "FIND_STRING:") out_string (TTY, MY_Look_for_String, MY_Look_for_Length) out_string (TTy, " length = ") out_integer (TTY, My_Look_for_length) out_record (TTY) out_line (TTY, "Within :") out_string (TTY, MY_Look_In_String, MY_Look_In_Length) out_string (TTy, " length = ") out_integer (TTY, My_Look_in_length) out_record (TTY) out_string (TTY, "Starting at position = ") out_integer (TTY, Position) out_record (TTY) %ENDIF Test_Point := Position while ((Test_Point + MY_Look_for_Length -1 <= MY_Look_In_Length) AND NOT Found_It) do if MATCH (MY_Look_In_String, MY_Look_In_Length, Test_Point, MY_Look_for_String, MY_Look_for_Length) do %IF DEBUG out_string (TTY, "FIND_STRING Found it, Position = ") out_integer (TTY, Test_Point) out_record (TTY) %ENDIF Position := Test_Point + MY_Look_for_Length Found_It := true otherwise Test_Point *= + 1 endif endwhile %IF DEBUG if not Found_IT do out_line (TTY, "String not found") endif %ENDIF RETURN endprocedure endmodule