StrContains.nsh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ; StrContains
  2. ; This function does a case sensitive searches for an occurrence of a substring in a string.
  3. ; It returns the substring if it is found.
  4. ; Otherwise it returns null("").
  5. ; Written by kenglish_hi
  6. ; Adapted from StrReplace written by dandaman32
  7. Var STR_HAYSTACK
  8. Var STR_NEEDLE
  9. Var STR_CONTAINS_VAR_1
  10. Var STR_CONTAINS_VAR_2
  11. Var STR_CONTAINS_VAR_3
  12. Var STR_CONTAINS_VAR_4
  13. Var STR_RETURN_VAR
  14. Function StrContains
  15. Exch $STR_NEEDLE
  16. Exch 1
  17. Exch $STR_HAYSTACK
  18. ; Uncomment to debug
  19. ;MessageBox MB_OK 'STR_NEEDLE = $STR_NEEDLE STR_HAYSTACK = $STR_HAYSTACK '
  20. StrCpy $STR_RETURN_VAR ""
  21. StrCpy $STR_CONTAINS_VAR_1 -1
  22. StrLen $STR_CONTAINS_VAR_2 $STR_NEEDLE
  23. StrLen $STR_CONTAINS_VAR_4 $STR_HAYSTACK
  24. loop:
  25. IntOp $STR_CONTAINS_VAR_1 $STR_CONTAINS_VAR_1 + 1
  26. StrCpy $STR_CONTAINS_VAR_3 $STR_HAYSTACK $STR_CONTAINS_VAR_2 $STR_CONTAINS_VAR_1
  27. StrCmp $STR_CONTAINS_VAR_3 $STR_NEEDLE found
  28. StrCmp $STR_CONTAINS_VAR_1 $STR_CONTAINS_VAR_4 done
  29. Goto loop
  30. found:
  31. StrCpy $STR_RETURN_VAR $STR_NEEDLE
  32. Goto done
  33. done:
  34. Pop $STR_NEEDLE ;Prevent "invalid opcode" errors and keep the
  35. Exch $STR_RETURN_VAR
  36. FunctionEnd
  37. !macro _StrContainsConstructor OUT NEEDLE HAYSTACK
  38. Push `${HAYSTACK}`
  39. Push `${NEEDLE}`
  40. Call StrContains
  41. Pop `${OUT}`
  42. !macroend
  43. !define StrContains '!insertmacro "_StrContainsConstructor"'