Replace Sub String (macro): Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
mNo edit summary
 
(2 intermediate revisions by the same user not shown)
Line 89: Line 89:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
== Safe version ==
I've found that this macros is unsare.
Let me suggest another version:
<highlight-nsis>
!macro ReplaceSubStr OLD_STR SUB_STR REPLACEMENT_STR
    Push $R0
    Push "${OLD_STR}" ;String to do replacement in (haystack)
Push "${SUB_STR}" ;String to replace (needle)
Push "${REPLACEMENT_STR}" ; Replacement
Call StrRep
Pop $R0 ;result
    StrCpy ${OLD_STR} $R0
   
    Pop $R0
!macroend
</highlight-nsis>
With that you need not use $MODIFIED_STR - you just replace substring in OLD_STR argument.
And there is all ok with $R0 - it doesn't change.


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

Latest revision as of 18:18, 19 February 2015

Author: redgoblin (talk, contrib)


Description

Hello, I wrote this script for the newbies like me. This script is an alternative to the function StrRep written by dirtydingus.

Usage

Save this script below as ReplaceSubStr.nsh and put it in the NSIS\include\ directory.

In your script :

!include "ReplaceSubStr.nsh"
 
;[...]
 
Function "blabla"
 
   !insertmacro ReplaceSubStr StrToModified SubStrToReplace SubStrReplacement
   MessageBox MB_OK $MODIFIED_STR ; String modified
 
FunctionEnd

The Macro and Function

; This script is derived of a script Written by dirtydingus :
; "Another String Replace (and Slash/BackSlash Converter)" 
;
; for more information please see :
; http://nsis.sourceforge.net/Another_String_Replace_(and_Slash/BackSlash_Converter)
 
Var MODIFIED_STR
 
!macro ReplaceSubStr OLD_STR SUB_STR REPLACEMENT_STR
 
	Push "${OLD_STR}" ;String to do replacement in (haystack)
	Push "${SUB_STR}" ;String to replace (needle)
	Push "${REPLACEMENT_STR}" ; Replacement
	Call StrRep
	Pop $R0 ;result
	StrCpy $MODIFIED_STR $R0
 
!macroend
 
Function StrRep
 
  ;Written by dirtydingus 2003-02-20 04:30:09 
  ; USAGE
  ;Push String to do replacement in (haystack)
  ;Push String to replace (needle)
  ;Push Replacement
  ;Call StrRep
  ;Pop $R0 result	
 
  Exch $R4 ; $R4 = Replacement String
  Exch
  Exch $R3 ; $R3 = String to replace (needle)
  Exch 2
  Exch $R1 ; $R1 = String to do replacement in (haystack)
  Push $R2 ; Replaced haystack
  Push $R5 ; Len (needle)
  Push $R6 ; len (haystack)
  Push $R7 ; Scratch reg
  StrCpy $R2 ""
  StrLen $R5 $R3
  StrLen $R6 $R1
loop:
  StrCpy $R7 $R1 $R5
  StrCmp $R7 $R3 found
  StrCpy $R7 $R1 1 ; - optimization can be removed if U know len needle=1
  StrCpy $R2 "$R2$R7"
  StrCpy $R1 $R1 $R6 1
  StrCmp $R1 "" done loop
found:
  StrCpy $R2 "$R2$R4"
  StrCpy $R1 $R1 $R6 $R5
  StrCmp $R1 "" done loop
done:
  StrCpy $R3 $R2
  Pop $R7
  Pop $R6
  Pop $R5
  Pop $R2
  Pop $R1
  Pop $R4
  Exch $R3
 
FunctionEnd

Safe version

I've found that this macros is unsare. Let me suggest another version:

!macro ReplaceSubStr OLD_STR SUB_STR REPLACEMENT_STR
    Push $R0
 
    Push "${OLD_STR}" ;String to do replacement in (haystack)
	Push "${SUB_STR}" ;String to replace (needle)
	Push "${REPLACEMENT_STR}" ; Replacement
	Call StrRep
	Pop $R0 ;result
    StrCpy ${OLD_STR} $R0
 
    Pop $R0
!macroend

With that you need not use $MODIFIED_STR - you just replace substring in OLD_STR argument. And there is all ok with $R0 - it doesn't change.