How to Search Memory?

Just starting out? Need help? Post your questions and find answers here.
swhite
Addict
Addict
Posts: 805
Joined: Thu May 21, 2009 6:56 pm

How to Search Memory?

Post 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
Simon White
dCipher Computing
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to Search Memory?

Post by skywalk »

CompareMemoryString()
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
AZJIO
Addict
Addict
Posts: 2223
Joined: Sun May 14, 2017 1:48 am

Re: How to Search Memory?

Post 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
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How to Search Memory?

Post 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
swhite
Addict
Addict
Posts: 805
Joined: Thu May 21, 2009 6:56 pm

Re: How to Search Memory?

Post 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
Simon White
dCipher Computing
Post Reply