StrLoc: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Use italics instead of quotes for parameter names.)
No edit summary
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{PageAuthor|deguix}}
== Description ==
== Description ==


'''Requires:''' [[LogicLib]].
'''Requires:''' [[LogicLib header file]].


'''Version:''' 2.0.
'''Version:''' 2.0.


This function outputs the relative index position from the start or end of a string, where a substring is located.
This function outputs the relative index position from the start or end of a string, where a substring is located. (Case insensitive)


== How To Use ==
== How To Use ==
Line 12: Line 14:


<highlight-nsis>
<highlight-nsis>
${StrLoc} "ResultVar" "String" "SubString" "StartPoint(>|<)"
${StrLoc} "ResultVar" "String" "SubString" "StartPoint"
</highlight-nsis>
</highlight-nsis>
or
or
<highlight-nsis>
<highlight-nsis>
Push "StartPoint(>|<)"
Push "String"
Push "SubString"
Push "SubString"
Push "String"
Push "StartPoint"
Call StrLoc
Call StrLoc
Pop "ResultVar"
Pop "ResultVar"
Line 26: Line 28:


; ResultVar
; ResultVar
: Buffer (variable) where the relative position index of ''SubString'' inside ''String'' is returned, according to ''StartPoint''. This value is always smaller or equal than ''String'''s length. If ''SubString'' is not found, an empty value is returned.
: Variable where the relative position index of ''SubString'' inside ''String'' is returned, according to ''StartPoint''. This value is always smaller or equal than ''String''&#39;s length. If ''SubString'' is not found, an empty value is returned.


; String
; String
Line 35: Line 37:


; StartPoint(>|<)
; StartPoint(>|<)
: Start position where the search begins and the counter starts. '''>''' = ''String'''s start, '''<''' = ''String'''s end. Default is '''>'''.
: Start position where the search begins and the counter starts. '''>''' = ''String''&#39;s start, '''<''' = ''String''&#39;s end. Default is '''>'''.


=== Example ===
=== Example ===
Line 50: Line 52:


!macro StrLoc ResultVar String SubString StartPoint
!macro StrLoc ResultVar String SubString StartPoint
  Push "${String}"
  Push "${SubString}"
   Push "${StartPoint}"
   Push "${StartPoint}"
  Push "${SubString}"
  Push "${String}"
   Call StrLoc
   Call StrLoc
   Pop "${ResultVar}"
   Pop "${ResultVar}"
Line 120: Line 122:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
== Credits ==
Author: Diego Pedroso ([[User:deguix|deguix]]).<br>
Based on [[StrStr]] function.


[[Category:String Functions]]
[[Category:String Functions]]

Latest revision as of 10:05, 3 February 2012

Author: deguix (talk, contrib)


Description

Requires: LogicLib header file.

Version: 2.0.

This function outputs the relative index position from the start or end of a string, where a substring is located. (Case insensitive)

How To Use

Syntax

${StrLoc} "ResultVar" "String" "SubString" "StartPoint"

or

Push "String"
Push "SubString"
Push "StartPoint"
Call StrLoc
Pop "ResultVar"

Parameters

ResultVar
Variable where the relative position index of SubString inside String is returned, according to StartPoint. This value is always smaller or equal than String's length. If SubString is not found, an empty value is returned.
String
String where to search for SubString.
SubString
String to search in String.
StartPoint(>|<)
Start position where the search begins and the counter starts. > = String's start, < = String's end. Default is >.

Example

${StrLoc} $0 "This is just an example" "just" "<"
;$0 = "11"

Function Code

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

Credits

Author: Diego Pedroso (deguix).
Based on StrStr function.