StrRep: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
No edit summary |
Dandaman32 (talk | contribs) m (Fixed 'dandaman32' user link) |
||
Line 119: | Line 119: | ||
== Credits == | == Credits == | ||
Version 3.x - Dan Fuhry ([[User:dandaman32]]).<br> | Version 3.x - Dan Fuhry ([[User:dandaman32|dandaman32]]).<br> | ||
Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br> | Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br> | ||
Version 1.x [[User:Afrow UK|Afrow UK]] / Diego Pedroso ([[User:deguix|deguix]]).<br> | Version 1.x [[User:Afrow UK|Afrow UK]] / Diego Pedroso ([[User:deguix|deguix]]).<br> |
Revision as of 07:31, 18 March 2006
Author: dandaman32 (talk, contrib) |
Description
Version: 3.0
This function searches and replaces all occurrences of a substring in a string.
How To Use
Syntax
- Single command
- This is by far the easiest way to use StrReplace.
${StrReplace} '$0' '\' '_' 'C:\Documents and Settings\Dan\Desktop\PSCP Frontend.exe' MessageBox MB_OK $0 ; will be C:_Documents and Settings_Dan_Desktop_PSCP Frontend.exe
- Push/function call
- For when you're used to NSIS :)
Push "old" Push "brand new" Push "Shame on you, you broke your old needle!" Call StrReplace Pop $0 MessageBox MB_OK $0 ; will be "Shame on you, you broke your brand new needle!"
- Usage
- ${StrReplace} "$result_var" "SubString" "RepString" "String"
Parameters
- $result_var
- Variable where resulting operation of the replacement is returned. If SubString is not found, the value is the same as String.
- String
- String where to search for SubString.
- SubString
- String to search in String and to be replaced by RepString.
- RepString
- String to replace all occurrences of SubString inside String.
Example
${StrReplace} $0 "just " "" "This is just an example" ;$0 = "This is an example"
Function Code
; StrReplace ; Replaces all ocurrences of a given needle within a haystack with another string ; Written by dandaman32 Var STR_REPLACE_VAR_0 Var STR_REPLACE_VAR_1 Var STR_REPLACE_VAR_2 Var STR_REPLACE_VAR_3 Var STR_REPLACE_VAR_4 Var STR_REPLACE_VAR_5 Var STR_REPLACE_VAR_6 Var STR_REPLACE_VAR_7 Var STR_REPLACE_VAR_8 Function StrReplace Exch $STR_REPLACE_VAR_2 Exch 1 Exch $STR_REPLACE_VAR_1 Exch 2 Exch $STR_REPLACE_VAR_0 StrCpy $STR_REPLACE_VAR_3 -1 StrLen $STR_REPLACE_VAR_4 $STR_REPLACE_VAR_1 StrLen $STR_REPLACE_VAR_6 $STR_REPLACE_VAR_0 loop: IntOp $STR_REPLACE_VAR_3 $STR_REPLACE_VAR_3 + 1 StrCpy $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_4 $STR_REPLACE_VAR_3 StrCmp $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_1 found StrCmp $STR_REPLACE_VAR_3 $STR_REPLACE_VAR_6 done Goto loop found: StrCpy $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_3 IntOp $STR_REPLACE_VAR_8 $STR_REPLACE_VAR_3 + $STR_REPLACE_VAR_4 StrCpy $STR_REPLACE_VAR_7 $STR_REPLACE_VAR_0 "" $STR_REPLACE_VAR_8 StrCpy $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_5$STR_REPLACE_VAR_2$STR_REPLACE_VAR_7 StrLen $STR_REPLACE_VAR_6 $STR_REPLACE_VAR_0 Goto loop done: Pop $STR_REPLACE_VAR_1 ; Prevent "invalid opcode" errors and keep the Pop $STR_REPLACE_VAR_1 ; stack as it was before the function was called Exch $STR_REPLACE_VAR_0 FunctionEnd !macro _strReplaceConstructor OUT NEEDLE NEEDLE2 HAYSTACK Push "${NEEDLE}" Push "${NEEDLE2}" Push "${HAYSTACK}" Call StrReplace Pop "${OUT}" !macroend !define StrReplace '!insertmacro "_strReplaceConstructor"'
Versions History
- 3.0
- Updated by dandaman32 - uses less code and doesn't depend on LogicLib
- Also: changed macro syntax to work like the PHP eqivalent
- 2.0.1
- Fixed stack problems.
Credits
Version 3.x - Dan Fuhry (dandaman32).
Version 2.x - Diego Pedroso (deguix).
Version 1.x Afrow UK / Diego Pedroso (deguix).
Version 1.0 - Hendri Adriaens (Smile2Me)