StrStr: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Wikipedia python library)
 
m (Corrected link to LogicLib header file.)
 
(16 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{PageAuthor|deguix}}
== Description ==
== Description ==
This function searches for a substring on a string, and returns itself plus the remaining of the string. Doesn't work with Unicode.


== The Function ==
'''Version:''' 2.0.1.
<highlight-nsis>; StrStr
 
; input, top of stack = string to search for
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.
;       top of stack-1 = string to search in
 
; output, top of stack (replaces with the portion of the string remaining)
== How To Use ==
; modifies no other variables.
 
;
=== Syntax ===
; Usage:
 
;  Push "this is a long ass string"
<highlight-nsis>
;   Push "ass"
${StrStr} "ResultVar" "String" "SubString"
;   Call StrStr
</highlight-nsis>
Pop $R0
or
; ($R0 at this point is "ass string")
<highlight-nsis>
Push "String"
Push "SubString"
Call StrStr
Pop "ResultVar"
</highlight-nsis>
 
=== 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 ===
 
<highlight-nsis>
${StrStr} $0 "This is just an example" "just"
;$0 = "just an example"
</highlight-nsis>
 
== Function Code (requires [[LogicLib header file]]) ==
 
<highlight-nsis>
!define StrStr "!insertmacro StrStr"
 
!macro StrStr ResultVar String SubString
  Push `${String}`
  Push `${SubString}`
  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
</highlight-nsis>
 
== Function Code (does not require [[LogicLib header file]]) ==
 
<highlight-nsis>
!define StrStr "!insertmacro StrStr"
 
!macro StrStr ResultVar String SubString
  Push `${String}`
  Push `${SubString}`
  Call StrStr
  Pop `${ResultVar}`
!macroend


Function StrStr
Function StrStr
   Exch $R1 ; st=haystack,old$R1, $R1=needle
/*After this point:
   Exch    ; st=old$R1,haystack
  ------------------------------------------
   Exch $R2 ; st=old$R1,old$R2, $R2=haystack
  $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 $R3
   Push $R4
   Push $R4
   Push $R5
   Push $R5
  ;Get "String" and "SubString" length
  StrLen $R2 $R0
   StrLen $R3 $R1
   StrLen $R3 $R1
  ;Start "StartCharPos" counter
   StrCpy $R4 0
   StrCpy $R4 0
  ; $R1=needle
 
  ; $R2=haystack
   ;Loop until "SubString" is found or "String" reaches its end
  ; $R3=len(needle)
  ; $R4=cnt
   ; $R5=tmp
   loop:
   loop:
     StrCpy $R5 $R2 $R3 $R4
    ;Remove everything before and after the searched part ("TempStr")
     StrCmp $R5 $R1 done
     StrCpy $R5 $R1 $R2 $R4
     StrCmp $R5 "" done
 
    ;Compare "TempStr" with "SubString"
     StrCmp $R5 $R0 done
     ;If not "SubString", this could be "String"'s end
    IntCmp $R4 $R3 done 0 done
    ;If not, continue the loop
     IntOp $R4 $R4 + 1
     IntOp $R4 $R4 + 1
     Goto loop
     Goto loop
   done:
   done:
   StrCpy $R1 $R2 "" $R4
 
/*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 $R5
   Pop $R4
   Pop $R4
   Pop $R3
   Pop $R3
   Pop $R2
   Pop $R2
   Exch $R1
  Pop $R1
   Exch $R0
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>


Page author: sunjammer
== Versions History ==
 
;2.0.1
:Added support for Unicode strings (not tested by the author).
 
== Credits ==
Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br>
Version 1.x - [[User:mgentry|mgentry]] / Amir Szekely ([[User:kichik|kichik]]) / Ximon Eighteen ([[User:sunjammer|sunjammer]]) / Diego Pedroso ([[User:deguix|deguix]]).
 
[[Category:String Functions]]

Latest revision as of 06:23, 3 December 2005

Author: deguix (talk, contrib)


Description

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 "String"
Push "SubString"
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 (requires LogicLib header file)

!define StrStr "!insertmacro StrStr"
 
!macro StrStr ResultVar String SubString
  Push `${String}`
  Push `${SubString}`
  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

Function Code (does not require LogicLib header file)

!define StrStr "!insertmacro StrStr"
 
!macro StrStr ResultVar String SubString
  Push `${String}`
  Push `${SubString}`
  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
  loop:
    ;Remove everything before and after the searched part ("TempStr")
    StrCpy $R5 $R1 $R2 $R4
 
    ;Compare "TempStr" with "SubString"
    StrCmp $R5 $R0 done
    ;If not "SubString", this could be "String"'s end
    IntCmp $R4 $R3 done 0 done
    ;If not, continue the loop
    IntOp $R4 $R4 + 1
    Goto loop
  done:
 
/*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 (not tested by the author).

Credits

Version 2.x - Diego Pedroso (deguix).
Version 1.x - mgentry / Amir Szekely (kichik) / Ximon Eighteen (sunjammer) / Diego Pedroso (deguix).