StrContains: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Replaced ${StrContain} by ${StrContains} in the example.)
m (changed push calls to add support for usage with " sign)
 
Line 107: Line 107:
   
   
!macro _StrContainsConstructor OUT NEEDLE HAYSTACK
!macro _StrContainsConstructor OUT NEEDLE HAYSTACK
   Push "${HAYSTACK}"
   Push `${HAYSTACK}`
   Push "${NEEDLE}"
   Push `${NEEDLE}`
   Call StrContains
   Call StrContains
   Pop "${OUT}"
   Pop `${OUT}`
!macroend
!macroend
   
   

Latest revision as of 17:33, 29 August 2013

Author: kenglish_hi (talk, contrib)


Description

Version: 1.0

This function does a case sensitive search for an occurrence of a substring in a string. It returns the substring if it is found. Otherwise it returns null(""). It was created by slightly modifying StrRep.

How To Use

Syntax

Single command
This is by far the easiest way to use StrContains.
${StrContains} $0 "Dirt" "Dirty deeds done dirt cheap"
StrCmp $0 "" notfound
  MessageBox MB_OK 'Found string $0'
  Goto done
notfound:
  MessageBox MB_OK 'Did not find string'
done:
Push/function call
For when you're used to NSIS :)
Push "It's a long way to the top (if you wanna rock n' roll)"
Push "long"
Call StrContains
Pop $0
StrCmp $0 "" notfound
  MessageBox MB_OK 'Found string $0'
  Goto done
notfound:
  MessageBox MB_OK 'Did not find string'
done:
Usage
${StrContains} "$result_var" "Needle" "Haystack"

Parameters

$result_var
Variable will contain the "needle" value if the needle is found in the haystack. Otherwise, it will contain null ("").
Needle
String to search for.
Haystack
String to search.

Example

${StrContains} $0 "just " "This is just an example"
;$0 = "just"
${StrContains} $0 "missing " "This is another example"
;$0 = ""

Function Code

; StrContains
; This function does a case sensitive searches for an occurrence of a substring in a string. 
; It returns the substring if it is found. 
; Otherwise it returns null(""). 
; Written by kenglish_hi
; Adapted from StrReplace written by dandaman32
 
 
Var STR_HAYSTACK
Var STR_NEEDLE
Var STR_CONTAINS_VAR_1
Var STR_CONTAINS_VAR_2
Var STR_CONTAINS_VAR_3
Var STR_CONTAINS_VAR_4
Var STR_RETURN_VAR
 
Function StrContains
  Exch $STR_NEEDLE
  Exch 1
  Exch $STR_HAYSTACK
  ; Uncomment to debug
  ;MessageBox MB_OK 'STR_NEEDLE = $STR_NEEDLE STR_HAYSTACK = $STR_HAYSTACK '
    StrCpy $STR_RETURN_VAR ""
    StrCpy $STR_CONTAINS_VAR_1 -1
    StrLen $STR_CONTAINS_VAR_2 $STR_NEEDLE
    StrLen $STR_CONTAINS_VAR_4 $STR_HAYSTACK
    loop:
      IntOp $STR_CONTAINS_VAR_1 $STR_CONTAINS_VAR_1 + 1
      StrCpy $STR_CONTAINS_VAR_3 $STR_HAYSTACK $STR_CONTAINS_VAR_2 $STR_CONTAINS_VAR_1
      StrCmp $STR_CONTAINS_VAR_3 $STR_NEEDLE found
      StrCmp $STR_CONTAINS_VAR_1 $STR_CONTAINS_VAR_4 done
      Goto loop
    found:
      StrCpy $STR_RETURN_VAR $STR_NEEDLE
      Goto done
    done:
   Pop $STR_NEEDLE ;Prevent "invalid opcode" errors and keep the
   Exch $STR_RETURN_VAR  
FunctionEnd
 
!macro _StrContainsConstructor OUT NEEDLE HAYSTACK
  Push `${HAYSTACK}`
  Push `${NEEDLE}`
  Call StrContains
  Pop `${OUT}`
!macroend
 
!define StrContains '!insertmacro "_StrContainsConstructor"'

Versions History

1.0
Submitted by (kenglish_hi), please contact me if you have any problems or know of any improvements.

Credits

Version 1.0 Kevin English (Hawaii). (kenglish_hi)