Page 1 of 1

FindString for memory block

Posted: Mon Feb 22, 2010 3:12 am
by UserOfPure
Would be GREAT if there was a command to find a string in a memory block (perhaps called "FindStringInMemory"?), with the result being the offset from the start of the memory block. It would work just like FindString but without dying when it finds a 0-byte.

So if my memory block looked like this (hex bytes shown):

Code: Select all

41 42 43 00 44 45 46 00 47 48 49 ; ABC DEF GHI
Then the following command would return 5 because "DEF" is at offset 5:

Code: Select all

Debug FindStringInMemory(memory,"DEF",1)

Re: FindString for memory block

Posted: Mon Feb 22, 2010 6:43 am
by Demivec
UserOfPure wrote:Would be GREAT if there was a command to find a string in a memory block (perhaps called "FindStringInMemory"?), with the result being the offset from the start of the memory block. It would work just like FindString but without dying when it finds a 0-byte.

So if my memory block looked like this (hex bytes shown):

Code: Select all

41 42 43 00 44 45 46 00 47 48 49 ; ABC DEF GHI
Then the following command would return 5 because "DEF" is at offset 5:

Code: Select all

Debug FindStringInMemory(memory,"DEF",1)
How would the command know when to stop searching if it continued past a 0-byte? Wouldn't that cause IMA's each time the string was not found?

Re: FindString for memory block

Posted: Mon Feb 22, 2010 8:41 am
by UserOfPure
Demivec wrote:How would the command know when to stop searching
Since every allocated memory block has a set size, then obviously the search stops at the end of it. An optional "length" parameter could be used, too.

Re: FindString for memory block

Posted: Thu Feb 25, 2010 1:45 pm
by Trond
You can code this in just 8 lines:

Code: Select all

Procedure FindStringInMemory(String.s, Memory, MemoryLength)
  Protected L = Len(String)
  For I = 0 To MemoryLength-L
    If CompareMemory(@String, Memory+I, L)
      ProcedureReturn I+1
    EndIf
  Next
EndProcedure

Code: Select all

Memory.s = "ABC DEF GHI"

Debug FindStringInMemory("DEF", @Memory, Len(Memory))

Re: FindString for memory block

Posted: Thu Feb 25, 2010 1:50 pm
by gnozal
Some alternatives (QuickSearch ...) here : http://www.purebasic.fr/english/viewtop ... 12&t=27908

Re: FindString for memory block

Posted: Thu Feb 25, 2010 2:08 pm
by UserOfPure
Trond, but is that just for searching the memory of strings? I want to search a memory block that was created with AllocateMemory, which has 0 bytes in it where the required text to find is.

gnozal, thanks for the link, but I already have a custom routine written to do the job, but it's slow and I know a native command by Fred and Co would be 100% faster and more efficient. :)

Re: FindString for memory block

Posted: Thu Feb 25, 2010 2:21 pm
by Trond
Trond, but is that just for searching the memory of strings?
All memory are equally good memory. It really doesn't matter whether your memory area is part of a string or not. I just used a string in the example. The first parameter is always a string, which is what you asked for.