StrStr: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Version: 2.0.1. Version number "2.0" was wrongly assigned to this version, so nothing was changed except by the addition of "Versions History" header.)
("OutVar" -> "ResultVar".)
Line 94: Line 94:
/*After this point:
/*After this point:
   ------------------------------------------
   ------------------------------------------
   $R0 = OutVar (output)*/
   $R0 = ResultVar (output)*/


   ;Remove part before "SubString" on "String" (if there has one)
   ;Remove part before "SubString" on "String" (if there has one)

Revision as of 06:00, 10 July 2005

Author: deguix (talk, contrib)


Description

Requires: LogicLib.

Version: 2.0.1.

This function searches for a substring on a string, and returns itself plus the remaining part of the string on the right of the substring.

How To Use

Syntax

${StrStr} "ResultVar" "String" "SubString"

or

Push "SubString"
Push "String"
Call StrStr
Pop "ResultVar"

Parameters

ResultVar
Variable where the part on the right and including the SubString found on String is returned. If SubString is not found, an empty value is returned.
String
String where to search for SubString.
SubString
String to search in String.

Example

${StrStr} $0 "This is just an example" "just"
;$0 = "just an example"

Function Code

!define StrStr "!insertmacro StrStr"
 
!macro StrStr ResultVar String SubString
  Push "${SubString}"
  Push "${String}"
  Call StrStr
  Pop "${ResultVar}"
!macroend
 
Function StrStr
/*After this point:
  ------------------------------------------
  $R0 = SubString (input)
  $R1 = String (input)
  $R2 = SubStringLen (temp)
  $R3 = StrLen (temp)
  $R4 = StartCharPos (temp)
  $R5 = TempStr (temp)*/
 
  ;Get input from user
  Exch $R0
  Exch
  Exch $R1
  Push $R2
  Push $R3
  Push $R4
  Push $R5
 
  ;Get "String" and "SubString" length
  StrLen $R2 $R0
  StrLen $R3 $R1
  ;Start "StartCharPos" counter
  StrCpy $R4 0
 
  ;Loop until "SubString" is found or "String" reaches its end
  ${Do}
    ;Remove everything before and after the searched part ("TempStr")
    StrCpy $R5 $R1 $R2 $R4
 
    ;Compare "TempStr" with "SubString"
    ${IfThen} $R5 == $R0 ${|} ${ExitDo} ${|}
    ;If not "SubString", this could be "String"'s end
    ${IfThen} $R4 >= $R3 ${|} ${ExitDo} ${|}
    ;If not, continue the loop
    IntOp $R4 $R4 + 1
  ${Loop}
 
/*After this point:
  ------------------------------------------
  $R0 = ResultVar (output)*/
 
  ;Remove part before "SubString" on "String" (if there has one)
  StrCpy $R0 $R1 `` $R4
 
  ;Return output to user
  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
  Pop $R1
  Exch $R0
FunctionEnd

Versions History

2.0.1
Added support for Unicode strings support (not tested by the author).

Credits

Version 2.x - Diego Pedroso (deguix).
Version 1.x - Ximon Eighteen (sunjammer).