StrReplace v4: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
No edit summary |
(Fixed value of ${Var1} not being restored) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
== Description == | == Description == | ||
This is my own version of the StrReplace function. | <p>This is my own version of the StrReplace function, which replaces a string with another string within a string.</p> | ||
I was not happy using the last version by | |||
<p>I was not happy using the last version by <i>dandaman32</i> as it declares 9 new variables when it could use 9 predefined NSIS variables ($R0-$R9 or $0-$9). Most would not be worried about this, but my project already had many new variables defined and I wanted to cut down on as much memory use as possible.</p> | |||
== Usage == | == Usage == | ||
Line 20: | Line 21: | ||
== The Function == | == The Function == | ||
<highlight-nsis> | <highlight-nsis> | ||
!define Var0 $R0 | |||
!define Var1 $R1 | |||
!define Var2 $R2 | |||
!define Var3 $R3 | |||
!define Var4 $R4 | |||
!define Var5 $R5 | |||
!define Var6 $R6 | |||
!define Var7 $R7 | |||
!define Var8 $R8 | |||
!macro StrReplaceV4 Var Replace With In | !macro StrReplaceV4 Var Replace With In | ||
Push `${Replace}` | Push `${Replace}` | ||
Line 30: | Line 41: | ||
Function StrReplaceV4 | Function StrReplaceV4 | ||
Exch $ | Exch ${Var0} #in | ||
Exch 1 | Exch 1 | ||
Exch $ | Exch ${Var1} #with | ||
Exch 2 | Exch 2 | ||
Exch $ | Exch ${Var2} #replace | ||
Push $ | Push ${Var3} | ||
Push $ | Push ${Var4} | ||
Push $ | Push ${Var5} | ||
Push $ | Push ${Var6} | ||
Push $ | Push ${Var7} | ||
Push $ | Push ${Var8} | ||
StrCpy $ | StrCpy ${Var3} -1 | ||
StrLen $ | StrLen ${Var5} ${Var0} | ||
StrLen $ | StrLen ${Var6} ${Var1} | ||
StrLen $ | StrLen ${Var7} ${Var2} | ||
Loop: | Loop: | ||
IntOp $ | IntOp ${Var3} ${Var3} + 1 | ||
StrCpy $ | StrCpy ${Var4} ${Var0} ${Var7} ${Var3} | ||
StrCmp $ | StrCmp ${Var3} ${Var5} End | ||
StrCmp $ | StrCmp ${Var4} ${Var2} 0 Loop | ||
StrCpy $ | StrCpy ${Var4} ${Var0} ${Var3} | ||
IntOp $ | IntOp ${Var8} ${Var3} + ${Var7} | ||
StrCpy $ | StrCpy ${Var8} ${Var0} "" ${Var8} | ||
StrCpy $ | StrCpy ${Var0} ${Var4}${Var1}${Var8} | ||
IntOp $ | IntOp ${Var3} ${Var3} + ${Var6} | ||
IntOp $ | IntOp ${Var3} ${Var3} - 1 | ||
IntOp ${Var5} ${Var5} - ${Var7} | |||
IntOp ${Var5} ${Var5} + ${Var6} | |||
Goto Loop | Goto Loop | ||
End: | End: | ||
Pop $ | Pop ${Var8} | ||
Pop $ | Pop ${Var7} | ||
Pop $ | Pop ${Var6} | ||
Pop $ | Pop ${Var5} | ||
Pop $ | Pop ${Var4} | ||
Pop $ | Pop ${Var3} | ||
Pop $ | Pop ${Var2} | ||
Pop $ | Exch | ||
Exch $ | Pop ${Var1} | ||
Exch ${Var0} #out | |||
FunctionEnd | FunctionEnd | ||
!undef Var8 | |||
!undef Var7 | |||
!undef Var6 | |||
!undef Var5 | |||
!undef Var4 | |||
!undef Var3 | |||
!undef Var2 | |||
!undef Var1 | |||
!undef Var0 | |||
</highlight-nsis> | </highlight-nsis> | ||
Latest revision as of 11:13, 28 October 2014
Author: Afrow UK (talk, contrib) |
Description
This is my own version of the StrReplace function, which replaces a string with another string within a string.
I was not happy using the last version by dandaman32 as it declares 9 new variables when it could use 9 predefined NSIS variables ($R0-$R9 or $0-$9). Most would not be worried about this, but my project already had many new variables defined and I wanted to cut down on as much memory use as possible.
Usage
${StrReplaceV4} $Var "replace" "with" "in string" # $Var contains the new string.
Example
${StrReplaceV4} $R0 "like" "don't like" "I like cheese a lot!" # $R0 == I don't like cheese a lot! ${StrReplaceV4} $R0 "a lot!" "very much..." "$R0" # $R0 == I don't like cheese very much...
The Function
!define Var0 $R0 !define Var1 $R1 !define Var2 $R2 !define Var3 $R3 !define Var4 $R4 !define Var5 $R5 !define Var6 $R6 !define Var7 $R7 !define Var8 $R8 !macro StrReplaceV4 Var Replace With In Push `${Replace}` Push `${With}` Push `${In}` Call StrReplaceV4 Pop `${Var}` !macroend !define StrReplaceV4 `!insertmacro StrReplaceV4` Function StrReplaceV4 Exch ${Var0} #in Exch 1 Exch ${Var1} #with Exch 2 Exch ${Var2} #replace Push ${Var3} Push ${Var4} Push ${Var5} Push ${Var6} Push ${Var7} Push ${Var8} StrCpy ${Var3} -1 StrLen ${Var5} ${Var0} StrLen ${Var6} ${Var1} StrLen ${Var7} ${Var2} Loop: IntOp ${Var3} ${Var3} + 1 StrCpy ${Var4} ${Var0} ${Var7} ${Var3} StrCmp ${Var3} ${Var5} End StrCmp ${Var4} ${Var2} 0 Loop StrCpy ${Var4} ${Var0} ${Var3} IntOp ${Var8} ${Var3} + ${Var7} StrCpy ${Var8} ${Var0} "" ${Var8} StrCpy ${Var0} ${Var4}${Var1}${Var8} IntOp ${Var3} ${Var3} + ${Var6} IntOp ${Var3} ${Var3} - 1 IntOp ${Var5} ${Var5} - ${Var7} IntOp ${Var5} ${Var5} + ${Var6} Goto Loop End: Pop ${Var8} Pop ${Var7} Pop ${Var6} Pop ${Var5} Pop ${Var4} Pop ${Var3} Pop ${Var2} Exch Pop ${Var1} Exch ${Var0} #out FunctionEnd !undef Var8 !undef Var7 !undef Var6 !undef Var5 !undef Var4 !undef Var3 !undef Var2 !undef Var1 !undef Var0
-Stu