StrReplace v4: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 59: Line 59:
   IntOp $R3 $R3 + $R6
   IntOp $R3 $R3 + $R6
   IntOp $R3 $R3 - 1
   IntOp $R3 $R3 - 1
  IntOp $R5 $R5 - $R7
  IntOp $R5 $R5 + $R6


  Goto Loop
  Goto Loop

Revision as of 12:48, 11 April 2006

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

!macro StrReplaceV4 Var Replace With In
 Push `${Replace}`
 Push `${With}`
 Push `${In}`
  Call StrReplaceV4
 Pop `${Var}`
!macroend
!define StrReplaceV4 `!insertmacro StrReplaceV4`
 
Function StrReplaceV4
Exch $R0 #in
Exch 1
Exch $R1 #with
Exch 2
Exch $R2 #replace
Push $R3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
 
 StrCpy $R3 -1
 StrLen $R5 $R0
 StrLen $R6 $R1
 StrLen $R7 $R2
 Loop:
  IntOp $R3 $R3 + 1
  StrCpy $R4 $R0 $R7 $R3
  StrCmp $R3 $R5 End
  StrCmp $R4 $R2 0 Loop
 
   StrCpy $R4 $R0 $R3
   IntOp $R8 $R3 + $R7
   StrCpy $R8 $R0 "" $R8
   StrCpy $R0 $R4$R1$R8
   IntOp $R3 $R3 + $R6
   IntOp $R3 $R3 - 1
   IntOp $R5 $R5 - $R7
   IntOp $R5 $R5 + $R6
 
 Goto Loop
 End:
 
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0 #out
FunctionEnd

-Stu