FindString for memory block

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

FindString for memory block

Post 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)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: FindString for memory block

Post 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?
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: FindString for memory block

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: FindString for memory block

Post 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))
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: FindString for memory block

Post by gnozal »

Some alternatives (QuickSearch ...) here : http://www.purebasic.fr/english/viewtop ... 12&t=27908
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: FindString for memory block

Post 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. :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: FindString for memory block

Post 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.
Post Reply