Replace Sub String (macro): Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author and download links, and changed format of some pages.)
m (Updated author links.)
Line 1: Line 1:
{|align=right
|<small>Author: [[{{ns:2}}:redgoblin|redgoblin]] ([[{{ns:3}}:redgoblin|talk]], [[{{ns:-1}}:Contributions/redgoblin|contrib]])</small>
|}
<br style="clear:both;">
== Description ==
== Description ==
Hello, I wrote this script for the newbies like me. This script is an alternative to the function StrRep written by dirtydingus.
Hello, I wrote this script for the newbies like me. This script is an alternative to the function StrRep written by dirtydingus.
Line 87: Line 91:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
Page author: [[User:redgoblin|redgoblin]]

Revision as of 03:02, 30 April 2005

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/archive/nsisweb.php?page=155&instances=0,122
 
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