StrReplace v4: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
(Fixed value of ${Var1} not being restored)
 
(2 intermediate revisions by the same user not shown)
Line 21: 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 31: Line 41:


Function StrReplaceV4
Function StrReplaceV4
Exch $R0 #in
Exch ${Var0} #in
Exch 1
Exch 1
Exch $R1 #with
Exch ${Var1} #with
Exch 2
Exch 2
Exch $R2 #replace
Exch ${Var2} #replace
Push $R3
Push ${Var3}
Push $R4
Push ${Var4}
Push $R5
Push ${Var5}
Push $R6
Push ${Var6}
Push $R7
Push ${Var7}
Push $R8
Push ${Var8}


  StrCpy $R3 -1
  StrCpy ${Var3} -1
  StrLen $R5 $R0
  StrLen ${Var5} ${Var0}
  StrLen $R6 $R1
  StrLen ${Var6} ${Var1}
  StrLen $R7 $R2
  StrLen ${Var7} ${Var2}
  Loop:
  Loop:
   IntOp $R3 $R3 + 1
   IntOp ${Var3} ${Var3} + 1
   StrCpy $R4 $R0 $R7 $R3
   StrCpy ${Var4} ${Var0} ${Var7} ${Var3}
   StrCmp $R3 $R5 End
   StrCmp ${Var3} ${Var5} End
   StrCmp $R4 $R2 0 Loop
   StrCmp ${Var4} ${Var2} 0 Loop


   StrCpy $R4 $R0 $R3
   StrCpy ${Var4} ${Var0} ${Var3}
   IntOp $R8 $R3 + $R7
   IntOp ${Var8} ${Var3} + ${Var7}
   StrCpy $R8 $R0 "" $R8
   StrCpy ${Var8} ${Var0} "" ${Var8}
   StrCpy $R0 $R4$R1$R8
   StrCpy ${Var0} ${Var4}${Var1}${Var8}
   IntOp $R3 $R3 + $R6
   IntOp ${Var3} ${Var3} + ${Var6}
   IntOp $R3 $R3 - 1
   IntOp ${Var3} ${Var3} - 1
   IntOp $R5 $R5 - $R7
   IntOp ${Var5} ${Var5} - ${Var7}
   IntOp $R5 $R5 + $R6
   IntOp ${Var5} ${Var5} + ${Var6}


  Goto Loop
  Goto Loop
  End:
  End:


Pop $R8
Pop ${Var8}
Pop $R7
Pop ${Var7}
Pop $R6
Pop ${Var6}
Pop $R5
Pop ${Var5}
Pop $R4
Pop ${Var4}
Pop $R3
Pop ${Var3}
Pop $R2
Pop ${Var2}
Pop $R1
Exch
Exch $R0 #out
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