FindMemoryString()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Starax.

Is there any chance of a FindMemoryString() function?

FindMemoryString(*Memory1,*Memory2,Mode,[Length])

I'm guessing that it would be much faster than calling CompareMemoryString() 5000000 times.


Keep up the good work Fred.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi, you can use one of these procedure in the meanwhile. There's also an InStr_() command in the StringLibraryEx user library, available at Paul's site, which finds a string in a string (not in a memory block, as these procedures do):

Code: Select all

; Asm version adapted from Masm32 M32LIB BSEARCH.ASM routine
Procedure MyFindMemoryStringAsm(*StartAddress, *MemorySize, FindString.s)
  result = 0
    !mov eax, [esp+8]
    !mov edx, dword -1
    !dec eax
  !.findend:
    !inc eax
    !inc edx
    !cmp [eax], byte 0
    !jne .findend
    !mov edi, dword [esp+8]
    !mov al, byte [edi]       ; get 1st byte
    !mov esi, dword [esp]
    !mov ecx, dword [esp+4]
    !add esi, ecx
    !neg ecx
;    !add ecx, 0
    !dec edx
    !jmp .main_loop
  !.pre_loop:
    !pop ecx
    !inc ecx
  !.main_loop:
    !cmp al, byte [esi+ecx]
    !je .pre_sub
    !inc ecx
    !jz .nMatch
    !cmp al, byte [esi+ecx]
    !je .pre_sub
    !inc ecx
    !jnz .main_loop
    !jmp .nMatch
  !.pre_sub:
    !push ecx
    !mov edi, dword [esp+12]
    !sub edi, ecx
    !mov ebx, edx
  !.sub_loop:
    !mov ah, byte [esi+ecx+1]
    !cmp ah, byte [edi+ecx+1]
    !jne .pre_loop
    !inc ecx
    !dec ebx
    !jnz .sub_loop
    !pop eax
    !add eax, dword [esp+4]
    !jmp .Outa_Here
  !.nMatch:
    !mov eax, dword -1
  !.Outa_Here:
    !add eax, dword [esp]
    !mov [esp+12], eax
  ProcedureReturn result
EndProcedure

Procedure MyFindMemoryString(*StartAddress, *MemorySize, FindString.s)
  result = 0
  StringLength = Len(FindString)
  For Pointer = *StartAddress To *StartAddress+*MemorySize
    If PeekS(Pointer, StringLength) = FindString
      result = Pointer
    EndIf
  Next Pointer
  ProcedureReturn result
EndProcedure

MyString.s = "This is a test"
BlockSize = 256
MemoryBlock = AllocateMemory(0, BlockSize, 0)
PokeS(MemoryBlock, MyString)
FindStringAddressAsm = MyFindMemoryStringAsm(MemoryBlock, BlockSize, "test")
FindStringAddress = MyFindMemoryString(MemoryBlock, BlockSize, "test")
MessageRequester("Results:", Str(FindStringAddressAsm)+Chr(10)+Str(FindStringAddress)+Chr(10), 0)
But it would be useful if integrated in the Memory library. What do you think?

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by El_Choni

Hi, you can use one of these procedure in the meanwhile. There's also an InStr_() command in the StringLibraryEx user library
I would not recommend using that library as the RSet$ command clashes with the PB RSet command. Same for LSet. Not sure if Fred has fixed that yet (commands ending with $ I guess).



--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + all updates, PB3.50)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

i also miss a function like that, i asked for it some time ago.

choni, thanks for the procedure.

there is also the strstr api, but i think it needs something better than W95.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.
Originally posted by Starax

Is there any chance of a FindMemoryString() function?

FindMemoryString(*Memory1,*Memory2,Mode,[Length])

I'm guessing that it would be much faster than calling CompareMemoryString() 5000000 times.


Keep up the good work Fred.
Yes, i would like that one very much....



Regards,

Berikco

http://www.benny.zeb.be/purebasic.htm
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Starax.

Wow, thanks Choni.
Post Reply