Talk:StrContains

From NSIS Wiki
Jump to navigationJump to search

Here's a modified version of your code that uses registers and returns index of match.


Function un.StrContains ;returns -1 if not found, idx if found

 Exch $R0 ; searchFor (Needle)
 Exch
 Exch $R1 ; searchIn (Haystack)
 Push $R2 ; return var
 Push $R3 ; idx
 Push $R4 ; needle length
 Push $R5 ; substr @ idx
 Push $R6 ; haystack length
 StrCpy $R2 ""     ; reset ret
 StrCpy $R3 -1     ; var1 = -1
 StrLen $R4 $R0    ; var2 = needle length
 StrLen $R6 $R1    ; var4 = haystack length
 loop:
   IntOp $R3 $R3 + 1         ; idx ++
   StrCpy $R5 $R1 $R4 $R3    ; substr = haystack.substring(idx, needle.length
   StrCmp $R5 $R0 found      ; if (substr == needle) -> found
   StrCmp $R3 $R6 done       ; if (idx == haystack.length -> done
   Goto loop
 done:
   StrCpy $R3 -1
 found:

StrCpy $R0 $R3

 Pop $R6
 Pop $R5
 Pop $R4
 Pop $R3
 Pop $R2
 Pop $R1
 Exch $R0
 

FunctionEnd