Page 1 of 1

How to Search Memory?

Posted: Wed Feb 19, 2025 10:13 pm
by swhite
Hi

I would like to find a very fast method to search memory for a particular ascii value something like the following.

Code: Select all

    #Colon = 58
    #DQ = 34
    *ptr=Ascii(~"\"AUID\":\"523552525\",\"NAME\":\"Smith, Jack\",\"AGE\":\"38\"")
    *ptr = MemFind(*ptr,#Colon) ; The pointer will now point to the first ":"
    *ptr = MemFind(*ptr,#Colon) ; The pointer will now point to the second ":"
    *ptr = MemFind(*ptr,#DQ) ; The pointer will now point to the quote before Smith
    
Any suggestion would be appreciated.

Thanks,
Simon

Re: How to Search Memory?

Posted: Wed Feb 19, 2025 10:52 pm
by skywalk
CompareMemoryString()

Re: How to Search Memory?

Posted: Wed Feb 19, 2025 11:15 pm
by AZJIO
https://www.purebasic.fr/english/viewto ... 97#p525097

Code: Select all

#Colon = 58
#DQ = 34
string$ = ~"\"AUID\":\"523552525\",\"NAME\":\"Smith, Jack\",\"AGE\":\"38\""
Debug string$
*p = Ascii(string$)

Procedure MemFind(*a.Ascii, n)
	If *a = 0 Or *a\a = 0 Or n = 0
		ProcedureReturn 0
	EndIf

	Repeat
		If *a\a = n
			ProcedureReturn *a
		EndIf
		*a + SizeOf(Ascii)
	Until Not *a\a

	ProcedureReturn 0
EndProcedure

*pStart = *p
Debug *p
*p = MemFind(*p, #Colon) ; The pointer will now point to the first ":"
Debug *p - *pStart
*p = MemFind(*p + 1, #Colon) ; The pointer will now point to the second ":"
Debug *p - *pStart
*p = MemFind(*p, #DQ) ; The pointer will now point to the quote before Smith
Debug *p - *pStart

Re: How to Search Memory?

Posted: Thu Feb 20, 2025 1:28 am
by ChrisR
or try this one as well

Code: Select all

#Colon = 58
#DQ = 34

Procedure MemFind(*AsciiBuffer.ascii, AsciiValue.a)
  If *AsciiBuffer = 0 Or AsciiValue = 0 : ProcedureReturn : EndIf
  Static *PreviousBuffer, PreviousValue.a

  ShowMemoryViewer(*AsciiBuffer, 60)
  If *AsciiBuffer = *PreviousBuffer And AsciiValue = PreviousValue
    *AsciiBuffer+1
  Else
    *PreviousBuffer = *AsciiBuffer
    PreviousValue = AsciiValue
  EndIf
  While *AsciiBuffer\a <> 0
    If *AsciiBuffer\a = AsciiValue
      *PreviousBuffer = *AsciiBuffer
      Break
    EndIf
    *AsciiBuffer+1
  Wend
  ProcedureReturn *PreviousBuffer
EndProcedure     

*ptr = Ascii(~"\"AUID\"):\"523552525\",\"NAME\":\"Smith, Jack\",\"AGE\":\"38\"") 
*ptr = MemFind(*ptr,#Colon) ; The pointer will now point to the first ":"
*ptr = MemFind(*ptr,#Colon) ; The pointer will now point to the second ":"
*ptr = MemFind(*ptr,#DQ)    ; The pointer will now point to the quote before Smith
*ptr = MemFind(*ptr,#Colon) ; The pointer will now point to the third ":"
*ptr = MemFind(*ptr,#DQ)    ; The pointer will now point to the quote before 38
*ptr = MemFind(*ptr,#DQ)    ; The pointer will now point to the last quote, last char
*ptr = MemFind(*ptr,#DQ)    ; The pointer remains pointed to the last quote, last char

Re: How to Search Memory?

Posted: Thu Feb 20, 2025 3:59 am
by swhite
Hi

Thanks for the suggestions and pointing me to Wilbert's post. I had forgotten about it but I know I have used it in the past so I give it a try again.

Simon