Optimizing a string search in binary data

jsuareza

New Member
Hi,

I have a MEMPTR variable containing binary data (including null characters) and I want to find a certain string inside this data. The problem is that I cant use GET-STRING AND INDEX because of the null characters. As you can see in the code below if I use INDEX I get a 0 position for my searched string. I have solved the problem using this SearchData function, but it is terribly slow when trying to handle big amounts of data, seems that doing many GET-STRINGs delays the execution time a lot. Any ideas of how can I do a faster search (binary search,APIs... whatever)?

FUNCTION SearchData RETURNS INTEGER
(msource AS MEMPTR,ctarget AS CHAR, istarting AS INTEGER):
DEF VAR i AS INTEGER.
REPEAT i = istarting TO GET-SIZE(msource) - LENGTH(ctarget):
IF GET-STRING(msource,i, LENGTH(ctarget)) = ctarget THEN DO:
RETURN i.
END.
END.
RETURN 0.
END FUNCTION.

DEFINE VAR m AS MEMPTR.
DEFINE VAR cString AS CHARACTER.

SET-SIZE(m) = 10.
PUT-STRING(m,1) = "ABCD".
PUT-BYTE(m,5) = 0.
PUT-STRING(m,6) = "EDFG".

cString = GET-STRING(m,1).

DISPLAY INDEX(cString,"FG",1).
DISPLAY SearchData(m,"FG",1).

SET-SIZE(m) = 0.

Thanks for your time
 
Back
Top